Two Laptop Bag*
The moose likes Servlets and the fly likes How to get the web server directory from a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to get the web server directory from a servlet" Watch "How to get the web server directory from a servlet" New topic
Author

How to get the web server directory from a servlet

Ayan Biswas
Ranch Hand

Joined: Jul 10, 2010
Posts: 104
I need to know how can I get to know the root directory of the web server from a currently deployed web- app.
getservletcontext().getrealpath("/") gave me fthe following location

Here 'Test' is the workspace of my ide and Simpletest is the name of the web app
but I want to get the root of the web server.


AyanBiswas
Piyush Mangal
Ranch Hand

Joined: Jan 22, 2007
Posts: 196
You might try with environment variables like CATALINA_HOME in your servlet.
Anurag Verma
Ranch Hand

Joined: Mar 30, 2012
Posts: 124

when you will run your application on Server without eclipse, getServletContext().getRealPath("/") will return you the context path of your application.
To get the Folder of your web server, you can try -



In the path that you are getting, wtpwebapps is the deployment folder of eclipse.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15952
    
  19

Webapps shouldn't know nor care where the web server is located. You can obtain and read a webapp's resources using the HttpServletRequest getResource() and getResourceAsStream methods. You should not attempt to access other webapp's resources, and you should NEVER attempt to write within a deployed webapp's directories. Always use external directories.

Likewise, you shouldn't attempt to access any of the webapp server's files or directories within a webapp.

There is, of course, one exception to the above, and that's the case where the webapp in question is a server control webapp such as the Tomcat Manager webapp. But for normal business apps, no.


Customer surveys are for companies who didn't pay proper attention to begin with.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60780
    
  65

I agree with Tim. This is a huge red flag.

What are you really trying to accomplish?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Anurag Verma
Ranch Hand

Joined: Mar 30, 2012
Posts: 124

+1 for comment of Tim. Doing this should be avoided upto the maximum possible limit. If you have something like file upload, better use some other location/machine for storage. Avoid using deployment/server folder, even for temporary storage purposes.
 
Consider Paul's rocket mass heater.
 
subject: How to get the web server directory from a servlet
 
Similar Threads
forwarding to a different webApp
Help needed JVM crashes while running the following program
advice regarding URL for a servlet
why is finally not being called?
EJB specifications