Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getContextPath()

 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone explain what getContextPath() does??
And what interfaces/classes it is defined in?
 
Omar Al Kababji
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that a little search in the specifications of the servlets would answer your questions, don't be afraid of google its not a monster.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Duran Harris wrote:Can anyone explain what getContextPath() does??
And what interfaces/classes it is defined in?


getContextPath() returns the context of the request, if your web application is running on http://localhost:8080/DuranWebapps/Email.do than calling this getContextPath will return /DuranWebapps as you have request Email.do so whatever servlet mapped by you in DD for Email.do will have ContextPath /DuranWebapps that you deploy under your tomcat/webapps directory.

You can get it using request.getContextPath().
getContextPath() is declared in HttpServletRequest.java interface and
defined in HttpServletRequestWrapper.java
 
Jonathan Elkharrat
Ranch Hand
Posts: 170
Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it gives you the name of the application.
usually the name of the .WAR

for example: (assume the servlet is mapped for /ch1/*)
http://localhost:8088/SCWCD/ch1/info?param=value
Context Path
Servlet Path
Path Info
Query String
 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Punit..
So the context is relevant only to the request...

In HFSJ chapter 5 Q14 It says that getContextPath() is defined in ServletContext as well....


@ Jonathon
it gives you the name of the application.
usually the name of the .WAR
for example: (assume the servlet is mapped for /ch1/*)
http://localhost:8088/SCWCD/ch1/info?param=value
Context Path
Servlet Path
Path Info
Query String


i dont know if you didn't finish typing but I didn't quite get your point....Or maybe my browser is missing some plugins
Did you mean to say that getContextPath() would return '/info'..?
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jonathan is saying getContextPath() will return /SCWCD.

You can call getContextPath() another way

This will also return the same thing as request.getContextPath();
 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay got it!
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jonathan put some nice colors which apparently you can't see. So here's the breakdown:
http://localhost:8088/SCWCD/ch1/info?param=value
Context Path - /SCWCD
Servlet Path - /ch1
Path Info - /info
Query String - param=value
 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK so then the Context path is the root of the currently executing web app?
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Duran Harris wrote:OK so then the Context path is the root of the currently executing web app?


yes
 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So even if I used request dispatchers the <url-patterns> still map my requests??
And to summarise :

Path-Info=Basically just the resource's physical name??
Context-path=Application root
Query-String=GET parameters
Servlet-Path=context-path+every single directory up to and including the directory containing the resource to which the request is forwarded.

The what is the request-uri attribute??
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic