Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Get Context Root From Servlet Code

 
Andrew Cly
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ServletContext.getServletContextName() gets the display-name of that context. It may not be the real context path.

Use Request.getContextPath() method
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic