I have a web application where the context name is different from my root name. For example my root Dir name is myapp
and the context path is app
. Then how to get the root name (myapp) from the Java web application?
Asked
Active
Viewed 7,860 times
1
-
Why do you care about that? What if the app is deployed as a war file? There wouldn't even be a root directory. – JB Nizet Dec 29 '12 at 13:28
-
You shouldn't assume it's exploded. What do you really want to do? – JB Nizet Dec 29 '12 at 13:33
1 Answers
5
If the application is not deployed with a war file you can use this code that will return the whole address where the application is running.
//request variable is the Java HTTP servlet request object received from an action
request.getSession().getServletContext().getRealPath("/");
The code I had given is the one I used in one of my Java EE projects well I didn't deploy my project as a war file due to some reasons
and getting the web application location can be used to get your root directory name

Netorica
- 18,523
- 17
- 73
- 108
-
OK lets give my side pre-conditions i have a webapplication that used some what context name in logic where root name and context name both are same but now context name is changed to root but dependency with context name. – AnilHoney Dec 29 '12 at 13:52