Link to home
Start Free TrialLog in
Avatar of sumitshah123
sumitshah123

asked on

Store Html form values submitted by user into a text file on the server.

I want the JSP code to save the values submitted by the user by filling the html form into a text file on the unix solaris server. Please give me just a short sample code of how it is done. I will award the full points immediately.

Survey is a seven question HTML form with a jsp submission.

All questions are required. On submission form should write a tab or pipe (|) delimited file to the /formdata directory located on the root.  Form will reside on the root.  

If a user chooses "Other" on questions 2,3 or 5 they must fill in the blank after "Other".  For users selecting "Yes" on question 6 an e-mail address is optional.

Below is the actual form :

1.      How often do you log in to our Web Commerce site? (Select One)
      a.      At least once a day
      b.      At least once a week
      c.      At least once a month

2.      What is your position within your company? (Select One)
      a.      Clerical
      b.      Management
      c.      Field Sales
      d.      Inside Sales
      e.      Shipping & Receiving
      f.      Other ______________________

3.      Are there other individuals within your organization using this tool? (Select All That Apply)
      a.      Clerical
      b.      Management
      c.      Field Sales
      d.      Inside Sales
      e.      Shipping & Receiving
      f.      Other ______________________

4.      What additional tools could we add to improve the Web Commerce site? Please rank the following tools in importance. One being the most important. (Rank 1 - 10)
      a.      New Housing Data
      b.      Product Information
      c.      Statement & Invoice Payment
      d.      Sales & Commission Reports
      e.      Product Availability
      f.      Regional Industry Data
      g.      National Industry Data
      h.      Sales & Revenue Analysis Tools
      i.      Selling Tools and Tips
      j.      Business Management Tips

5.      Do you use a Web Commerce site with other companies? (Select One)
      a.      Kohler
      b.      Moen
      c.      Delta
      d.      Sears
      e.      Other ______________________________________
      f.      No

6.      Are there other individuals within your organization the In-Sink-Erator Web Commerce site would be helpful too? (Select One)
      a.      Yes (please supply e-mail address)
      b.      No

7.      Additional Comments -
      (Open Text Field)

Rgds,
Sumit Shah.
Avatar of Kuldeepchaturvedi
Kuldeepchaturvedi
Flag of United States of America image

This should not be a tough task...
try this code below...

File f = new file("/formdata/whateverfilename.txt");
FileOutputStream fo = new FileOutputStream(f);
String ans1=request.getParameter("q1");
ans1+="|";
fo.write(ans1.getBytes());
String ans2=request.getParameter("q2");
ans2+="|";
fo.write(ans2.getBytes());
........ and so on..

at last
fo.close();

As far as mandetory fields goes.. you can use javascript or can put jsp validations before you come for writing the file.

Hope this helps
You'll want to stick a new line character on the end and open the FileOutputStream in append mode http://java.sun.com/j2se/1.4.2/docs/api/java/io/FileOutputStream.html#FileOutputStream(java.io.File,%20boolean) if you want to collect information from more than one person.
Avatar of sumitshah123
sumitshah123

ASKER

i made the html form but i want to loop through the entire form in one loop( like for each in asp) and save all input type and form values to the text file.

any help..???

Sumit.
this should help you:

<%@ page import="java.io.*,java.util.*" %>

<%

BufferedWriter bw = new BufferedWriter(new FileWriter("c:/temp/out.txt"));
Enumeration enum = request.getParameterNames();
String line = "";
while (enum.hasMoreElements()) {
      String paramName = (String)enum.nextElement();
      line = paramName + ": ";
      String[] values = request.getParameterValues(paramName);
      for (int i = 0; i < values.length; i++) {
            line += values[i];
            if (i != (values.length - 1)) line += ", ";
      }
      bw.write(line, 0, line.length());
      bw.newLine();
}
bw.close();

%>
hi,
i m geting this below error when i tried the above good. Any help ???

500 Servlet Exception
java.io.FileNotFoundException: out.txt (Permission denied)
      at java.io.FileOutputStream.open(Native Method)
      at java.io.FileOutputStream.<init>(FileOutputStream.java:176)
      at java.io.FileOutputStream.<init>(FileOutputStream.java:70)
      at java.io.FileWriter.<init>(FileWriter.java:46)
      at __27esumitshah4u._submit__jsp._jspService(/~sumitshah4u/submit.jsp:5)
      at com.caucho.jsp.JavaPage.service(JavaPage.java:74)
      at com.caucho.jsp.Page.subservice(Page.java:485)
      at com.caucho.server.http.FilterChainPage.doFilter(FilterChainPage.java:181)
      at com.caucho.server.http.Invocation.service(Invocation.java:291)
      at com.caucho.server.http.RunnerRequest.handleRequest(RunnerRequest.java:341)
      at com.caucho.server.http.RunnerRequest.handleConnection(RunnerRequest.java:268)
      at com.caucho.server.TcpConnection.run(TcpConnection.java:136)
      at java.lang.Thread.run(Thread.java:543)



your web server is not having permission to write on disk.... under what user profile your web server is running??
Check the permissions for your user profile and see if you have file write permission.

Regards
if you are on solaris and storing to /var/tmp or something then do something like this just to get up and running

chmod 777 -R /var/tmp
i have using free jsp hosting on www.mycgiserver.com for testing. i have given 777 permision to the output file.

any clues??
Hmm... that server won't give you permission of c:\temp\ as this directory is not under your control...
try just using "out.txt"... it should create the text file in your webapp folder where your should have autority to write.

regards
login to the console of you service provider.  go to the root of your webapplication.  make the file "out.txt" then type:

chmod 777 out.txt

next type:  pwd

copy this path and put it here:

BufferedWriter bw = new BufferedWriter(new FileWriter("path/out.txt"));

replace path with that full thing
i created a directory 'outt' manually.

and the code is :
<%@ page import="java.io.*,java.util.*" %>

<%

BufferedWriter bw = new BufferedWriter(new FileWriter("/outt/out.txt"));
Enumeration enum = request.getParameterNames();
String line = "";
while (enum.hasMoreElements()) {
    String paramName = (String)enum.nextElement();
    line = paramName + ": ";
    String[] values = request.getParameterValues(paramName);
    for (int i = 0; i < values.length; i++) {
         line += values[i];
         if (i != (values.length - 1)) line += ", ";
    }
    bw.write(line, 0, line.length());
    bw.newLine();
}
bw.close();

response.setContentType("text/html");
out.println("<html>");
out.println("<br><head></head><br><body><br>We have received your reponse.<br>");
out.println("</form><br></body><br></html>");
out.close();



%>

it still give me below error :

500 Servlet Exception
java.io.FileNotFoundException: /outt/out.txt (No such file or directory)
      at java.io.FileOutputStream.open(Native Method)
      at java.io.FileOutputStream.<init>(FileOutputStream.java:176)
      at java.io.FileOutputStream.<init>(FileOutputStream.java:70)
      at java.io.FileWriter.<init>(FileWriter.java:46)
      at __27esumitshah4u._submit__jsp._jspService(/~sumitshah4u/submit.jsp:5)
      at com.caucho.jsp.JavaPage.service(JavaPage.java:74)
      at com.caucho.jsp.Page.subservice(Page.java:485)
      at com.caucho.server.http.FilterChainPage.doFilter(FilterChainPage.java:181)
      at com.caucho.server.http.Invocation.service(Invocation.java:291)
      at com.caucho.server.http.RunnerRequest.handleRequest(RunnerRequest.java:341)
      at com.caucho.server.http.RunnerRequest.handleConnection(RunnerRequest.java:268)
      at com.caucho.server.TcpConnection.run(TcpConnection.java:136)
      at java.lang.Thread.run(Thread.java:543)



/outt/out.txt   is not correct..

use "pwd" to find the correct path
hey, i m using cuteftp...how do i get the path??
ASKER CERTIFIED SOLUTION
Avatar of tbone343
tbone343

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
toooooo much..............
I appreciate your efforts tbone......:)

thanks Kuldeepchaturvedi... i only signed up because it sounded like a cool free service... you should check it out.  but no doubt, seriously too much on this one... hahaha.