Thanks for your response Craig. My comments are embedded in our discussion.
At 04:21 PM 4/24/99 +0000, you wrote:
>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.
>
The current working directory of the operating system is then "/" (or the
root directory according to what I've experienced.
If "user.dir" is set by the JVM, do you know if other JVMs that run on
Linux set user.dir to someplace else? I'm using the blackdown.org port of
jdk1.2.
>>
>> 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.
>
This, unfortunately, will not work for me as my ISP starts the servlet
engine in automatic mode and I have no say in this decision, especially
when I'm one of many people who will potentially use servlets.
>>
>> 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:
>
I understand your solution, but unfortunately, I'm in a situation where I
do not have control over my zone. My ISP has setup a directory in my
account where I can place servlets, just as if I was writing Perl CGI scripts.
I would like that directory be the place where I write my log files,
database files, etc... If I was writing Perl scripts, writing to a
relative file name (without path info) would write a file to the same
directory the Perl script is installed at.
How do most ISPs handle servlets? I can't imagine that they allow their
users/clients access to the setup files (jserv.properties, and maybe even
the zone properties set up for each user/client).
I guess the fact that Java Web Server and JRun (run on an NT machine, so
maybe the JVM is setting things differently there) return the directory
where the servlets were installed when one queries "user.dir" make me think
that this method isn't so portable after all.
So, the big question to me is what does a user/web-creator do when they
want to use servlets and keep logfiles, but do not have the ability (based
on ISP constraints) to (1) start JServ in manual mode and (2) change the
init parameters in the zone properties file?
>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]
>
>
Regards,
Pete
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Pete Willemsen <[EMAIL PROTECTED]>
Dept. of Computer Science, Univ. of Iowa
----------------------------------------------------------------
To subscribe: [EMAIL PROTECTED]
To unsubscribe: [EMAIL PROTECTED]
READ THE FAQ!!!! <http://java.apache.org/faq/>
Archives and Other: <http://java.apache.org/main/mail.html/>
Problems?: [EMAIL PROTECTED]