File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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
subject: Get Context Root From Servlet Code
It's not a secret anymore!