• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

request.getPathInfo() vs request.getServletPath()

 
Ranch Hand
Posts: 89
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The javadoc can be useful

http://download.oracle.com/javaee/1.3/api/javax/servlet/http/HttpServletRequest.html
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They each give different parts of the URL. And yes, you can use both. Why would you not be able to?
 
Alexander Sales
Ranch Hand
Posts: 89
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Could you hold this kitten for a sec? I need to adjust this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic