This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach 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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "request.getPathInfo() vs request.getServletPath()" Watch "request.getPathInfo() vs request.getServletPath()" New topic

request.getPathInfo() vs request.getServletPath()

Alexander Sales
Ranch Hand

Joined: Feb 21, 2011
Posts: 89

In using the following methods.



Is it possible to get both Paths?

How does the two methods work according to your experience?

Alex Sales

ntumba lobo
Ranch Hand

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

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 60766

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'.

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

wood burning stoves
subject: request.getPathInfo() vs request.getServletPath()
Similar Threads
get servlet uri in init
forward vs include
tree like structure in jsp ??????????help me out
Grabbing the path and arguements
How does a Servlet get it's URI?