|
|
|
| |||||||||
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Writing to a text file
I need to know how i can capture the user input and then write to a text file. Currently, the form page is saved as Adhoc.jsp, which means that the form page uses jsp to retrieve some information from the database.
*Note: i just need to know how i can capture the user input and then write to a text file. Pls help asap........... |
|
#2
|
|||
|
|||
|
look at getParameter and FileOutputStream
|
|
#3
|
|||
|
|||
|
Use a bean to do the file writing as in:
<%@ page language="java" contentType="text/html" import="java.util.*" %> <%@ page errorPage="errorPage.jsp" %> <html> <!-- RequestWriter.jsp executed on <%= (new java.util.Date()) %> --> <head> <title>RequestWriter</title> </head> <body bgcolor="#669966" > <jsp:useBean id="writer" scope="page" class="utilities.RequestWriter" /> <jsp:setProperty name="writer" property="*"/> <% String password = request.getParameter("password"); if (writer.isCorrectPassword(password)) { writer.save(); out.println("<h2 align=\"center\">Changes saved</h2>"); }else{ out.println("<h2 align=\"center\">Incorrect Password</h2>"); } %> <form action="index.jsp" method="get"> <center><input type="submit" value="Go to Home Page"/></center> </form> </body> </html> *************************** /* RequestWriter.java * @author: Charles Bell * @version: August 24, 2002 */ package utilities; import java.io.*; import java.util.*; /** */ public class RequestWriter extends Object implements Serializable { private String id = ""; private String item = ""; private String responsibility = ""; private String type = ""; private String notes = ""; private String completionDate = ""; private String status = ""; private String root = "/usr/local/etc/httpd/sites/abc.com/htdocs/WEB-INF/requestfolder"; public String getID(){ return id; } public String getItem(){ return item; } public String getResponsibility(){ return responsibility; } public String getType(){ return type; } public String getNotes(){ return notes; } public String getCompletionDate(){ return completionDate; } public String getStatus(){ return status; } public void setID(String s){ id = s; } public void setItem(String s){ item = s; } public void setResponsibility(String s){ responsibility = s; } public void setType(String s){ type = s; } public void setNotes(String s){ notes = s; } public void setCompletionDate(String s){ completionDate = s; } public void setStatus(String s){ status = s; } /** Returns a complete string description of this Bug. */ public String toString(){ return "ID: " + getID() + "\n" + "Item: " + getItem() + "\n" + "Responsibility: " + getResponsibility() + "\n" + "Type: " + getType() + "\n" + "Notes: " + getNotes() + "\n" + "CompletionDate: " + getCompletionDate() + "\n" + "Status: " + getStatus(); } /** Wrtes this Request Data to a text file in the requestfolder folder. */ public void save(){ try{ File file = new File(root, getID() + ".txt"); FileWriter fw = new FileWriter(file); fw.write(toString()); fw.close(); }catch(FileNotFoundException fnfe){ System.err.println("FileNotFoundException: " + fnfe.getMessage()); }catch(IOException ioe){ System.err.println("IOException: " + ioe.getMessage()); } } /** Returns the current date-time String. */ private String getTimeString(){ return Calendar.getInstance().getTime().toString(); } public boolean isCorrectPassword(String password){ return (password.compareToIgnoreCase("changeisgood") == 0); } } |
| Viewing: Dev Shed Forums > Programming Languages > Java Help > Writing to a text file |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|