• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to get the web server directory from a servlet

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might try with environment variables like CATALINA_HOME in your servlet.
 
Ranch Hand
Posts: 171
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 171
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic