• Post Reply Bookmark Topic Watch Topic
  • New Topic

need object persistence using file in Servlet

 
Lee Chalupa
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to figure out how to serialize an object that is used in a servlet I developed. I'm trying to store the object in a file in the application directory such as
web-app/contactmanager/files/<here>
I can use the ServletContext.getResource() method
that return a URL object but I can't find what I need to write objects to this resource. For discussion purposes let's say the file name is ContactList.dat and the object I want to serialize is named ContactList.
Any suggestions?
Thanks.
lee
 
Lionel Port
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To serialize an object, the object must implement the interface java.io.Serializable. There are no methods that need to be implemented. For example..



You can than write the serializable objects to an object output stream. For example..

If the contact list contains other objects, they must also be serializable. If you need control over how an object is serialized, you can implement the methods.

For more information, go here.
http://java.sun.com/j2se/1.4.1/docs/guide/serialization/
 
Lee Chalupa
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand how to serialize an object. That is not what I'm asking for help with. I'm trying to figure out how to write the object to a specific file in the webapp subdirectory on the server without hardcoding an absolute url in the code. For example: webapp/ContactList/files/<here>.
 
Timo Hahn
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try getServletContext().getRealPath("/") to get the real path to the webapp root directory, then choose a location you like.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!