This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Get Context Root From Servlet Code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Get Context Root From Servlet Code" Watch "Get Context Root From Servlet Code" New topic

Get Context Root From Servlet Code

Andrew Cly

Joined: Jun 02, 2004
Posts: 3
In my servlet code I try to query the (deployed) context root of my
web application. By context root I mean that usually a http request
looks like this: http://host ort/contextroot/...

I tried to use ServletContext.getServletContextName() but it does not
work on Jboss and Tomcat servers I am using.

What I would do is to limit my web app to only be deployed under
certain context root. Otherwise my servlet will serve a http request

Is there a way to do this?
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
check out the javadocs for HttpServletRequest, and in specific the getRequestURL method.

But of course you've done that already because you know that the API docs are your friend?

danny liu
Ranch Hand

Joined: Jan 22, 2004
Posts: 185
ServletContext.getServletContextName() gets the display-name of that context. It may not be the real context path.

Use Request.getContextPath() method
jQuery in Action, 2nd edition
subject: Get Context Root From Servlet Code
Similar Threads
Can a servlet know the "context root" of the application ?
Context Root be only /
tomcat 6 - how to define context root name diff than WAR file name
Need ear installation help
Can't write Context element properly