• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Writing to a text file using servlet  RSS feed

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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.

Jsp Page-Page1.jsp


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.


Regards.




 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Good luck!
 
Marshal
Posts: 67164
169
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The request.getParameter() method will let you grab the submitted request parameters. Once you have the data, writing a file is simple matter of normal Java file I/O.
 
Ranch Hand
Posts: 212
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can create a helper class that will write the name and age parameters to given file.

For example
- 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.

For How to get the real path of a file, See ServletContext.getRealPath

See how to process the html form submission

- Do not write the IO code directly in servlet
- You may want to consider thread safety issues

And, why do you need to write it to a file ? If you want to just store it and retrieve later , a database is better at storing and querying then a simple text file.
 
neha priya
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Thanks a lot for the suggestions.Its working for me now.However i have a doubt.The text file that am creating is getting stored in my eclipse workspace:-

C:\ws\eclipse-ws\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\Website1

Is creating a file to this location is same as creating a file on the Server?I am using Tomcat Server and Eclipse Helios.

Kindly reply.

Regards.
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!