• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can a servlet know the "context root" of the application ?  RSS feed

 
Sol Mayer-Orn
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a servlet, that needs to know the "context root" of the web application where it resides.
By "context root" i mean the address which you have to print in the browser in order to get to the web application... the word coming immediately after the hostname and the port.
For example, if all requests to the webapp start with "http://localhost:8080/hello/" then "hello" is the context root.

So, what's the easiest way for my servlet to get hold of this context root ("hello" in ma example).
I need it because I want my servlet to write back absolute links.
I thought of some un-elegant ways to get it (e.g. parse the first incoming request)... but there's got to be a more elegant way to do this..

I'm using new newest tomcat 5.5 (servlet spec 2.4).

thankx.
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

request.getContextPath() give you the name of context path.

Thanks
 
Sol Mayer-Orn
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx very much.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66208
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Narendra pointed out, the correct term is context path.

The context root is usually taken to mean the file path of where the WEB-INF for the app resides (assuming that the app is not being executed directly out of a war file).
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!