aspose file tools*
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
Author

Get Context Root From Servlet Code

Andrew Cly
Greenhorn

Joined: Jun 02, 2004
Posts: 3
Hi,
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
differently.

Is there a way to do this?
Thanks,
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?


42
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Get Context Root From Servlet Code