This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Doubt about getContextPath(), getServletPath, getPathInfo() 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 "Doubt about getContextPath(), getServletPath, getPathInfo()" Watch "Doubt about getContextPath(), getServletPath, getPathInfo()" New topic
Author

Doubt about getContextPath(), getServletPath, getPathInfo()

Fabian Angy
Ranch Hand

Joined: Oct 27, 2008
Posts: 90
Hi,

I have a doubt about getContextPath(), getServletPath and getPathInfo()
If we use a ".jsp", getPathInfo() will be always null ?

http://localhost:8080/myApp/SelectStuff.do
=> using of "result.jsp"

getContextPath(): /myApp
getServletPath(): /result.jsp
getPathInfo(): null

http://localhost:8080/myApp/xxx/xxx/SelectStuff.do
=> using of "result.jsp"

getContextPath(): /myApp
getServletPath(): /xxx/xxx/result.jsp
getPathInfo(): null

http://localhost:8080/myApp/xxx/
=> map to "result.jsp"

getContextPath(): /myApp
getServletPath(): /xxx/
getPathInfo(): null


But if we use anything else, by example a ".html"

http://localhost:8080/myApp/index.html

getContextPath(): /myApp
getServletPath(): null
getPathInfo(): /index.html


http://localhost:8080/myApp/xxx/index.html

getContextPath(): /myApp
getServletPath(): /xxx
getPathInfo(): /index.html

http://localhost:8080/myApp/xxx/

getContextPath(): /myApp
getServletPath(): null
getPathInfo(): /xxx/

Am I right or not ?

Thanks in advance !

SCJP 5
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper

Joined: Apr 07, 2010
Posts: 2007
    
  39

Hi Angy,

If we use a ".jsp", getPathInfo() will be always null ?

Not always: it depends on your servlet-mapping.

For example:

When you now use the following URL:
http://localhost:8080/myApp/A/hallo?q=a

getServletPath() /A
getContextPath() /myApp
getPathInfo() /hallo
getQueryString() q=a

or http://localhost:8080/myApp/A/B/C/hallo?q=a
getServletPath() /A
getContextPath() /myApp
getPathInfo() /B/C/hallo
getQueryString() q=a


You can use a simple jsp like this to verify:

An HTML file is always part of the getPathInfo()

Regards,
Frits
Fabian Angy
Ranch Hand

Joined: Oct 27, 2008
Posts: 90
Perfect !

Thank you ! :-)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt about getContextPath(), getServletPath, getPathInfo()
 
jQuery in Action, 3rd edition