Pete Willemsen wrote:
> Hello.
>
> I'm attempting to use
>
> System.getProperty("user.dir")
>
> in my servlet to locate the a directory that is hopefully the directory
> where my servlets were installed. I need to write some persistent database
> files as the servlet runs.
>
> Here's where I'm getting confused... On other servlet engines such as JRun
> this system call returns the directory where the servlets themselves were
> installed. However, on my Apache JServ (on a Linux system) setup, this
> system call returns "/".
>
> I guess this seems strange to me and I'm not sure where I messed up (or if
> I did). If my servlet is used at a site where I don't have root access, I
> will not be able to use it since (obviously), I probably won't be able to
> write the files requried by my servlet to maintain persistence.
>
> So, a few questions that I have are
>
> 1) Is getProperty("user.dir") returning what is considered to be correct
> information (especially in JServ)?
>
The value of the "user.dir" property is set by the JVM, not the servlet
engine. It is the current working directory of the operating system process
that is running the servlet engine, which may or may not have anything at all
to do with where servlets are loaded from.
>
> 1.a) Why is there inconsistency between servlet implementations?
>
There is inconsistency because there is nothing in the servlet API spec
requiring there to be consistency. The place you load a servlet from usually
has nothing to do with the current working directory of the servlet engine.
There is always some mechanism to specify the "class path" for the servlets to
be loaded from. In the case of Apache JServ, you do this with the
"repositories" configuration parameter of the zone properties file. You can
list as many directories and ZIP/JAR files as you like.
By the way, you can change where Apache JServ is started if you start it in
"manual" mode instead of "automatic" mode. Then, the current working
directory will be wherever your startup script makes it.
>
> 2) What are other ways to get the path to the directory where the servlet
> exists?
>
Why do you insist on storing your data in the same directory that the servlets
are loaded from? What would your software do if you packaged up your servlets
in a JAR file, instead of a directory?
The typical approach to this type of problem is to define an initialization
parameter for your servlet, used to pass in the pathname of the directory in
which data files can be found. In your zone properties file, you might say:
servlet.myservlet.code=MyServlet // The name of the servlet class
servlet.myservlet.initArgs=directory.path=/storage/directory // The path to
store things in
Then, in your servlet, do the following:
public class MyServlet {
String directory = null;
public void init(ServletConfig config) throws ServletException {
super(config);
directory = config.getInitParameter("directory.path");
}
public void service(...)
throws ServletException, IOException {
... prepare the result page in the usual way ...
// Write out data to persistent storage
String pathname = directory + File.separator + "myfile.data";
FileOutputStream fos =
new FileOutputStream(pathname);
fos.write(...);
fos.close();
}
}
>
> Many thanks for any help anyone can provide.
>
> Pete Willemsen
> [EMAIL PROTECTED]
>
Craig McClanahan
----------------------------------------------------------------
To subscribe: [EMAIL PROTECTED]
To unsubscribe: [EMAIL PROTECTED]
Archives and Other: <http://java.apache.org/main/mail.html/>
Problems?: [EMAIL PROTECTED]