aspose file tools
The moose likes Servlets and the fly likes request.getPathInfo() vs request.getServletPath() Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Servlets
Reply 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: 179
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: 56529
    
  14

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


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About 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: 179
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

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: request.getPathInfo() vs request.getServletPath()
 
Similar Threads
get servlet uri in init
tree like structure in jsp ??????????help me out
How does a Servlet get it's URI?
Grabbing the path and arguements
forward vs include