The one major problem with what you ask is: What happens if the
application is run out of a WAR file? As in, the JSP server does not expand
the WAR to run it? This is a possiblity according to the spec, although I
don't know of any JSP environment that works in this way.
To set an Application's default directory, use
System.setProperty("USER.DIR", "Path Here"). Be warned, however, that this
will affect the default directory for every class that is running in the
JVM, including Tomcat and any other webapps. You probably don't want to do
this.
You can use ServletContext.getRealPath("relative path") to get the
address of some file in your webapp and then somehow mangle this if you need
to write files that don't yet exist. Be careful, though, because this might
the be address to a file inside of a WAR file.
If you only need to read files, you could use
ClassLoader.getResource/getResourceAsStream or
ServletContext.getResource/getResourceAsStream. These methods find a file
relative to your classpath (for ClassLoader) or webapp root (for
ServletContext). These methods only work for reading files that already
exist.
Another option is to store the path in a configuration file, like
web.xml. Accessing this depends on how you set it up. A side not here,
though, Tomcat only loads the web.xml file at the context load time (start
in Tomcat 3, context load in Tomcat 4). This means that you will have to
force the Tomcat configuration to be loaded if you change the path.
The last option that I can think of right now is to hard code the
path into your application. This can either be relative to the startup
directory (TOMCAT_HOME/bin) or an absolute path. Both have downsides,
relative to /bin makes you at the mercy of other applications that choose to
mess with the USER.DIR propery (see first option) and the absolute path is
almost always a bad idea.
Randy
-----Original Message-----
From: Alexander Batzios [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, March 14, 2001 6:26 AM
To: [EMAIL PROTECTED]
Subject: Set an application's working path
Hello.
I am using a JSP file to call a class, say myClass, which is a pure java
application. From that class, I then need to call a method, say myMethod.
The
part of the JSP file that does the above is like:
myClass mc = new myClass(parameters);
mc.myMethod();
Part of myMethod() functionality is reading/writting files. Well, the thing
is
that it only works in the /bin directory of tomcat (where the
startup,shutdown
scripts are). That is, it expects to find files there, and it creates files
there. How can I change it's working path to a virtual path of the
webserver?
Or to where the JSP file that calls it lives?
Any help will be greatly appreciated.
Thanks,
alex
------------------------------------------------------------
Get your FREE web-based e-mail and newsgroup access at:
http://MailAndNews.com
Create a new mailbox, or access your existing IMAP4 or
POP3 mailbox from anywhere with just a web browser.
------------------------------------------------------------
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, email: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, email: [EMAIL PROTECTED]