[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Writing and reading a file in a servlet.



I do it this way.

First get the SevletContext.   I override the init method.

private ServletContext servContext = null; //this is used so we can get the real path

//override to get the servlet context
public void init(ServletConfig serConf)
{
servContext = serConf.getServletContext();
}

Then you can use the ServletContext like this to get the path

String sc = servContext.getRealPath("");

On my Tomcat installation it returns:

/usr/local/tomcat4/webapps/MyAppContext

Hope this helps!!

-Jason



On Tue, 2003-06-17 at 14:19, Lee Chalupa wrote:
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