• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Writing to a text file using servlet

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
rubbery bacon. crispy tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic