File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes web application root Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "web application root" Watch "web application root" New topic

web application root

rex johnson

Joined: Feb 27, 2002
Posts: 14

We are developing a JSP/Servlets web application and cannot find how to retrieve the web application root dynamically at the Start Up of the Web Application (i.e. before any requests have been created so we cannot use methods from the request object).

We need to have this app root at the application startup, because it is widely used inside the JSP pages and the servlets of the web application. Also we would like to avoid setting it as a static parameter in the web.xml or elsewhere.

Does anyone know how to do that? Any hint or suggestion is very much appreciated!

Rex Johnson
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13035
If I understand what you are after, in the init method of a servlet you get ServletContext which has various methods such as getResourcePaths() that might help.
rex johnson

Joined: Feb 27, 2002
Posts: 14
I've retrieved and inspected the servletContext object in my Start Up servlet and although the context path can be viewed as a variable there does not seem to be a method to extract it and use it within a program.

Once a request object has been created then the getContextPath method can be used to retrive the path but we require the value before any requests have been created.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
ServletContext sc = getServletContext();

will get you the filesystem path for the WEB-INF directory of your web application.
sc.getRealPaty("/"); should get you the webroot.

Huang Jian Tian

Joined: Oct 31, 2004
Posts: 14
hi all,
I don't know whether this topic still watched by you all.

What I would like to ask is, getRealPath() will return the exact location for the file such "C:\jboss\server\default\deploy\myWebApps". Is that a method that we can use to return only "/myWebApps"?

The purpose for me to doing this is to get the context path (says '/myWebApps') and append it with my images location (eg, '/myWebApps/images/'). It would be inappropriate for me to show the exact location to my users. (eg. 'C:\jboss\server\default\deploy\myWebApps\images').

please help..thanks
I agree. Here's the link:
subject: web application root
It's not a secret anymore!