It's not a secret anymore!
The moose likes Servlets and the fly likes Web Application Configuration Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Web Application Configuration Question" Watch "Web Application Configuration Question" New topic

Web Application Configuration Question

Darren Wilkinson
Ranch Hand

Joined: May 22, 2007
Posts: 35

I have been trying to find out how to configure a web application but the more I read, the more confused I am. I can't believe it is so complicated to configure a web application, so I must just be misunderstanding everything. Let me explain what I'm failing to achieve.

I am developing locally using Netbeans 6 and deploying via Netbeans onto a local Tomcat 6 installation.

I am deploying remotely to Tomcat 6 on the server.

I have built a simple web app that allows the user to browse the contents of csv files stored in a directory. Initially, this directory was inside the web app just for testing and everthing worked great.

Then I moved it outside the web app as later, this web app, will allow people to upload new csv files into the csv file folder. This way later deployments of the web app do not overwrite the existing csv files - must be untouched by any deployment of the web app.

Now, from within the web application code, I need to get a path to the csv files folder that is outside the web application root.

The csv files folder (on both my local tomcat and the remote tomcat) is currently in the webapps directory.

I can set the path as a context-param however, the absolute path on my local system is completely different from that on the remote server - which is no surprise but makes things tricky.

When running locally, my web app needs to access the csv files from [local_tomcat]webapps/csvFiles but when running remotely, my web app needs to access the csv files from [remote_tomcat]webapps/csvFiles.

Everything I try has failed. Everything I read just makes this task seem increasingly complicated.

I'm sure there must be a straight forward solution to this as it must be a very common requirement.

Can anyone help me please?

Thanks for reading :-)
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
Naturally this is a common problem and it has a solution built into the servlet API. Look at the methods of javax.servlet.ServletContext which give you access to files in terms of the application context.

Particuarly look at methods like getRealPath, getResource etc.

Darren Wilkinson
Ranch Hand

Joined: May 22, 2007
Posts: 35
Thanks for the quick reply.

I have looked at the ServletContext object and methods but it isn't clear to me how I reference a file outside of the web application context. Maybe I am just really dim and I am missing something but it isn't clear to me how to get a reference to the csv files I need to reference using the ServletContext object and any of its methods - especially when the real path to the csv files directory is going to be different when running the web application locally and then remotely: my local filesystem starts with C: and the remote starts with D: so I thought I need to use relative paths - fine within the web app context - but once trying to go outside... I just don't understand how...

Sorry - hit post too soon - for example I thought getResource() was what I needed as the API says

Returns a URL to the resource that is mapped to a specified path. The path must begin with a "/" and is interpreted as relative to the current context root.
This method allows the servlet container to make a resource available to servlets from any source. Resources can be located on a local or remote file system, in a database, or in a .war file.

but I can't find any examples where this is used to get a file externally - only examples getting a file from within the web app context!
[ June 02, 2008: Message edited by: Darren Wilkinson ]
Darren Wilkinson
Ranch Hand

Joined: May 22, 2007
Posts: 35

This is what has happened on the other forums - the replies sort of dry up because the problem is that I need to access files outside of the web app's context ;-)

One of the tech support guys at gave me this advice (which I have not tried yet but it is next on my list).
There isn't really an easy way to get a context-param evaluated and have it different for different environments, but there is something you can to help make this easier.

Set a system property pointing to the location for your csv files. You could call that something like CSVPATH. Then you would set that on the command line when starting Tocmat. Maybe something like:


You would then reference that value in your code when retrieving the files. For more info, please reference the Java core API docs in regards info on system properties. That should do the trick.

What I have done is created some context params inside the web apps web.xml file as follows:

So the remote <param-value> is commented out. After deploying using the Tomcat manager app, I swap the comments around - bit tedious but...

What puzzles me is that the second <context-param> which provides the path to an xml file works on both the remote and local version, whereas the third <context-param> that provides the path to the csv files works on the local version but not the remote???

I am wondering if there is a problem with the remote path to the csv files. It is definitely the correct path to the csv files directory but maybe Java doesn't like it whereas it's ok with the xml file path?

There are no exceptions displayed in D:\Tomcat\logs\stderr and the System.out calls in my code just display the following:

which is all fine and indicates that the remote version hangs within FeedLoader.initCsvLoader() method... but not when running on my local tomcat!

Really puzzled as to how to get to the bottom of this - any thoughts?
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63869

You cannot directly serve files that are not part of a web application.

What most people would do in this case -- and which I do all the time -- is create a servlet that can access the files via File I/O (path to files obtained from a context param) and serve them to the response. Request params to the servlet can identify which file is to be served.

You're really making this much harder than it needs to be.

[Asking smart questions] [About Bear] [Books by Bear]
Darren Wilkinson
Ranch Hand

Joined: May 22, 2007
Posts: 35
Thanks for replying Bear.

Your comment is interesting as yes, I may well be going about this all wrong.

The project requirement is:

1. Create a system that allows admin to upload csv files into a directory on the server.

2. Create a system that:

2a. Populates a drop down menu with the csv files in the directory.
2b. When a file is selected in the drop down menu, displays the selected csv files data in a table.

3. The csv files must not be stored in the web application as, when deploying newer versions of the system, the files in the csv file directory must not be overwritten - these files must be untouched by deployment/re-deployment of the web application.

Now aside from the little puzzling glitch whereby the path to the csv file directory isn't working yet the path to the xml file does, I have a system on my local dev box that meets all of the above requirements.

I'm really not trying to start an argument - I just don't understand how I am going about meeting the above requirements incorrectly and I really would like to learn what a simpler solution would be.

I'm sure it would be of use to other people in the future as well.

Again Bear, thank you for your reply. Maybe you can spare the time to outline a better solution to the above requirements :-)
Darren Wilkinson
Ranch Hand

Joined: May 22, 2007
Posts: 35
Well... I mirrored the csv directory and xml directory outside of the web application and deployed to my VPS with and after ftp'ing the xml file and the csv files up - it works!

So the issue must have been with the set up of Tomcat on the other remote server. Still don't know why that server is wrong but at least I now know I am not insane.

Thanks to everyone who replied and offered their thoughts.
I agree. Here's the link:
subject: Web Application Configuration Question
It's not a secret anymore!