How to write a file in the root of my webapp

S

SPG

Hi,
I have a servlet application running on tomcat.
I want to write a default config file in the root directory of the
application:

tomcat
+webapps
+myapp
+myConfig.txt

If I use new File(./myConfig.txt), then the file goes to the root of tomcat.
Ither there an easy way to find out the directory of the running app?

Cheers,

Steve
 
A

Anton Spaans

SPG said:
Hi,
I have a servlet application running on tomcat.
I want to write a default config file in the root directory of the
application:

tomcat
+webapps
+myapp
+myConfig.txt

If I use new File(./myConfig.txt), then the file goes to the root of tomcat.
Ither there an easy way to find out the directory of the running app?

Cheers,

Steve

You can use the ServletContext.getRealPath("/myConfig.txt") to figure out
what the actual path on your filesystem is.
But, your web-application can be deployed as a WAR file. Then this method
will return null (since you can not write into a WAR file like this).
So, this means that your code may not always work. If you need to write a
configuration file, do it outside your root-directory.

-- Anton Spaans.
 
W

Wendy S

SPG said:
Hi,
I have a servlet application running on tomcat.
I want to write a default config file in the root directory of the
application:

There's no guarantee that there is a root directory-- it's possible to run
webapps from the .war file without unpacking. For writing files, the
Servlet Spec requires the container to provide a temporary directory for you
to use. See SRV 3.7.1 of the 2.4 Spec.

But if it's a _default_ config, wouldn't you know that when you pack up the
..war file? I put that sort of file under 'properties' and use
getResourceAsStream() to read them in.

If you do need to write persistent files from within the webapp, maybe take
a look at the struts-example webapp. They use an xml file as a database,
and when you enter information, it's saved in 'database.xml' in
struts-example/WEB-INF. Not exactly where you wanted it-- but do you really
want it accessible from a browser, as it would be if you placed it in the
root directory of the webapp? (Barring any Filters, etc.)
 
J

John C. Bollinger

Wendy said:
There's no guarantee that there is a root directory-- it's possible to run
webapps from the .war file without unpacking. For writing files, the
Servlet Spec requires the container to provide a temporary directory for you
to use. See SRV 3.7.1 of the 2.4 Spec.

But if it's a _default_ config, wouldn't you know that when you pack up the
.war file? I put that sort of file under 'properties' and use
getResourceAsStream() to read them in.

If you do need to write persistent files from within the webapp, maybe take
a look at the struts-example webapp. They use an xml file as a database,
and when you enter information, it's saved in 'database.xml' in
struts-example/WEB-INF. Not exactly where you wanted it-- but do you really
want it accessible from a browser, as it would be if you placed it in the
root directory of the webapp? (Barring any Filters, etc.)

You may also found it equally or even more convenient to put default
configuration parameters into your webapp's deployment descriptor as
context parameters. That avoids having to maintain a seperate
configuration file at all, and gives you a ready-made API for retrieving
the parameters.


John Bollinger
(e-mail address removed)
 
W

Wendy S

John C. Bollinger said:
You may also found it equally or even more convenient to put default
configuration parameters into your webapp's deployment descriptor as
context parameters. That avoids having to maintain a seperate
configuration file at all, and gives you a ready-made API for retrieving
the parameters.

Sure, if it's webapp-specific. I use context params for things like the URL
of the university's login page, which my webapp may need to redirect to.
The properties file I was thinking of is for the database connection factory
to use, and that doesn't belong in the deployment descriptor.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top