Get the root directory name in Java web application if context and root names are different

AnilHoney picture AnilHoney · Dec 29, 2012 · Viewed 7.4k times · Source

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?

Answer

Netorica picture Netorica · Dec 29, 2012

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