Connecting Tech Pros Worldwide Forums | Help | Site Map

How do you access application root static pages or WEB-INF jars in a non-servlet clas

Newbie
 
Join Date: Oct 2007
Posts: 25
#1: Feb 19 '08
In a general servlet class after deployment, you can access the application root as servletContext.getRealPath("/") to access any of the properties files in the "WEB-INF" directory or anywhere else in the /webapp-root/ directory.

How can we get the equivalent servletContext.getRealPath("/") in a non-servlet where you are interested to access a property file located in "/webapp-root/WEB-INF" or probably a text file stored in a static folder pages in the /webapp-root/pages/ folder.

Expert
 
Join Date: Nov 2007
Location: Germany
Posts: 294
#2: Feb 19 '08

re: How do you access application root static pages or WEB-INF jars in a non-servlet clas


Quote:

Originally Posted by krishna81m

In a general servlet class after deployment, you can access the application root as servletContext.getRealPath("/") to access any of the properties files in the "WEB-INF" directory or anywhere else in the /webapp-root/ directory.

How can we get the equivalent servletContext.getRealPath("/") in a non-servlet where you are interested to access a property file located in "/webapp-root/WEB-INF" or probably a text file stored in a static folder pages in the /webapp-root/pages/ folder.

1.) dynamic read
get the windows environment variable for the webserver directory (e.g tomcat) and add ""/webapp-root/WEB-INF" to it. (or parse the tomcat configuration file where you have mapped the web-directory to a real path on your harddisk )

2.) hardcode read
- or you can just make a configuration file, which will be read from your non-servlet program, and put the whole path there inside,
- or you can pass the folder as a parameter to java when you start your non-servlet program
- or you can set thepath inside a (windows) environment variable and read it from there.

3.) database read
store the path inside a database table when you run the servlet. Then read this path when you run your non-servlet app.

4.) remote read:
use RMI, Corba or other ways (Sockets, pipes, shared memory) to pass the
path from one program to another.
Newbie
 
Join Date: Oct 2007
Posts: 25
#3: Feb 21 '08

re: How do you access application root static pages or WEB-INF jars in a non-servlet clas


I ended up hard coding.. I hate this.

Wish someone could teach me how to use context listener to initialize the variable as string from a servlet's servletContext() method and save to application scope and then be able to access this?

Or pass servletContext object itself to the non-servlet.

Help requested.
Reply