wood burning stoves 2.0*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes getContextPath() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "getContextPath()" Watch "getContextPath()" New topic
Author

getContextPath()

Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

Can anyone explain what getContextPath() does??
And what interfaces/classes it is defined in?


===>SCJP 1.5(72%)<===
==>SCWCD1.5(76%)<===
Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
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.


Omar Al Kababji - Electrical & Computer Engineer
[SCJP - 90% - Story] [SCWCD - 94% - Story] [SCBCD - 80% - Story] | My Blog
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
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


SCJP 6
Jonathan Elkharrat
Ranch Hand

Joined: Dec 31, 2008
Posts: 170

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


SCJP 5, SCWCD 5, SCBCD 5
Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

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

Joined: Oct 16, 2008
Posts: 952
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

Joined: Nov 09, 2008
Posts: 598

Okay got it!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

OK so then the Context path is the root of the currently executing web app?
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Duran Harris wrote:OK so then the Context path is the root of the currently executing web app?


yes
Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

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??
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getContextPath()