This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes JSP and the fly likes Need help regarding Server's Real Path. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Need help regarding Server Watch "Need help regarding Server New topic

Need help regarding Server's Real Path.

Bitan Biswas

Joined: Aug 05, 2011
Posts: 12
In my Java Web Application, I need to dynamically create directories and upload files. For that I need the Real Path of the container. I use the following code

these two are working fine when i ran the application in localhost. it returns /home/xen/NetBeansProject/WebApp

but when I upload the project as a war file in my Internet server, it is not working. every times it return null.

Can you give me any suggestion regarding this?? Thank You.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63873

You can't get the file path into a war file. It's war file... there's no file system!

Besides, you shouldn't be adding files into the web app itself -- how on Earth do you expect to be able to redeploy the app with changes?

[Asking smart questions] [About Bear] [Books by Bear]
Stefan Evans

Joined: Jul 06, 2005
Posts: 1580
Rather than using getRealPath, I would suggest that you find out a directory on the server that your provider lets you write to, and include it as a property in your web app.

ie rather than using the ServletContext getRealPath method, retrieve it from a parameter you have set.

Agreed, it makes the installation of your site more complicated, as you now need to configure an additional piece of information somewhere, but it does have the advantage that you can specify anywhere - not just within the application path.
Possible places to put the property:
- in a properties file under the WEB-INF directory
- in the web.xml as a context property
- in a database
I agree. Here's the link:
subject: Need help regarding Server's Real Path.
jQuery in Action, 3rd edition