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

web application root

 
rex johnson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Bill
 
rex johnson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ServletContext sc = getServletContext();
sc.getRealPath("/WEB-INF");

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
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic