Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get the web server directory from a servlet

 
Ayan Biswas
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Piyush Mangal
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might try with environment variables like CATALINA_HOME in your servlet.
 
Anurag Verma
Ranch Hand
Posts: 168
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18304
56
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65227
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Tim. This is a huge red flag.

What are you really trying to accomplish?
 
Anurag Verma
Ranch Hand
Posts: 168
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
+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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic