[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: Writing and reading a file in a servlet.
You can use JNDI to retrieve a file path.
The 'fileName.path' can be stored in your web.xml as :
...........
<env-entry>
<env-entry-name>filename.path</env-entry-name>
<env-entry-value>c:\\temp\\SomeSerializedObject.ser</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>
..............
Here is the source to read a serialized object :
StringBuffer fileName = new StringBuffer();
try {
javax.naming.Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
fileName.append((String) envCtx.lookup("fileName.path"));
}
catch (Exception e)
{
e.printStackTrace();
System.err.println("WARNING! filename.path JNDI not found");
}
try
{
ObjectInputStream s = new ObjectInputStream(new
FileInputStream(fileName.toString()));
defaultObject = s.readObject();
}
catch (Exception e)
{
System.err.println("Couldn't find serialized file : " +
fileName.toString());
}
Nizar Bhamani
-----Original Message-----
From: Lee Chalupa [mailto:lchalupa@seelink.org]
Sent: Tuesday, June 17, 2003 2:19 PM
To: Ajug Members
Subject: Writing and reading a file in a servlet.
I need some direction:
In a servlet, I'm trying to serialize an object to a file.
I've been trying to use the same approach as I would if I was writing a
java application.
I've been trying to provide a path to FileOutputStream to create this file.
It's not working. It is having trouble finding the path.
I suspect I need to do something different within a servlet environment. I
know that ServletContext has the getResource method that will return a URL
but I don't understand if there is a way to use this URL to write to the
file.
How should I go about writing to and reading from a file within a Servlet
environment?
Thanks
Lee