This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Servlets and the fly likes request.getPathInfo() vs request.getServletPath() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "request.getPathInfo() vs request.getServletPath()" Watch "request.getPathInfo() vs request.getServletPath()" New topic
Author

request.getPathInfo() vs request.getServletPath()

Alexander Sales
Ranch Hand

Joined: Feb 21, 2011
Posts: 89

In using the following methods.

request.getPathInfo()

request.getServletPath()



Is it possible to get both Paths?

How does the two methods work according to your experience?

Thanks,
Alex Sales


OCPJP 6, OCEWCD Java EE 6
ntumba lobo
Ranch Hand

Joined: Oct 21, 2008
Posts: 180
The javadoc can be useful

http://download.oracle.com/javaee/1.3/api/javax/servlet/http/HttpServletRequest.html


SCJP 5 , SCWCD 5, SCEA 5
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61456
    
  67

They each give different parts of the URL. And yes, you can use both. Why would you not be able to?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Alexander Sales
Ranch Hand

Joined: Feb 21, 2011
Posts: 89

Oh.. I had a mistake while testing them..

but I noticed...

request.getPathInfo() - becomes null when I don't use wildcards in the url pattern.

request.getServletPath() - becomes empty when you use a url pattern name of a servlet like - '/index.jsp' or '/*'.
but will have a name if it is written like this - '/anyServletName/index.jsp' or '/anyServletName/*'.
it will output - '/anyServletName'.

Regards,
Alex Sales.
ntumba lobo
Ranch Hand

Joined: Oct 21, 2008
Posts: 180
From the javadoc
getServletPath : Returns the part of the request's URL that calls the servlet. This includes either the servlet name or a path to the servlet, but does not include any extra path information or a query string.

That's why getServletPath() for '/index.jsp' or '/*' returns null, that's because these urls do not call explicitly the servlet.
On the other hand '/anyServletName/index.jsp' or '/anyServletName/* call explicitly your servlet that's why getServletPath() returns something

 
 
subject: request.getPathInfo() vs request.getServletPath()