I have a jsp page that contains a form.When i click the submit button of the form i am directed to a servlet.I would like to create a text file using this servlet and write to it the form data collected from the earlier jsp page.
Kindly help me as to how do i code for the Page1Servlet so that am able to collect the passed data from jsp page,Page1.jsp,ad write it to a text file.
First you should create a class that will write data to a text file using some same data. Forget about the Servlet and the JSP page for a while, just concentrate on creating a class that can write data to a text file.
Once you have this class ready, then start to think about how you would instantiate an object of this class in the servlet code and pass the data that was entered in the HTML form of the JSP page.
The key aspect here is that the logic for writing to a text file should not be written directly in a Servlet sub-class.
You can create a helper class that will write the name and age parameters to given file.
- Create a helper class that will have a method like writeToFile which takes three parameters (path, name, age)
- The method would open a file using given path, and append the name and age
- The servlet will use the helper to write the name and age information to the file.
Is creating a file to this location is same as creating a file on the Server?I am using Tomcat Server and Eclipse Helios.
Joined: Apr 16, 2008
Sounds good. You should be able to create a file anywhere you want, and more experience with the I/O API should help.
Is creating a file to this location is same as creating a file on the Server?
You would have to clarify what "on the Server" means to you? Based on the directory you provide, it seems like the file is being created in an IDE directory. If you want it created somewhere else, you would have to change this with code.