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 getPathInfo() and 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 "getPathInfo() and "extra path"" Watch "getPathInfo() and "extra path"" New topic
Author

getPathInfo() and "extra path"

Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
Hi, I have a question about exactly does HttpServletRequest.getPathInfo() returns. The API doc says the "extra path" between url and the query string, but I have no idea what that may be. Can someone please shed some light on this?
Thanks
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Cameron Park:
[...] "extra path" between url and the query string, but I have no idea what that may be.

Say you have a servlet "myservlet" mapped to http://mysite/myservlet. Then, a request for http://mysite/myservlet/extrapath?argument=bla will still map to "myservlet", but there will be extra path info "extrapath".
Two examples where this can be useful. Say, you have a section of your site which is members-only. You can include a check in every JSP, of course. A cleaner way to implement it is to map "/memberarea" to a MemberCheck servlet. This servlet would ensure that the member had logged in, then use the extra path info to forward to the real JSP. The URL for your members-only JSPs would be something like "/memberarea/foo/bar.jsp". No coding in the JSPs necessary. (If you have a servlet 2.3 container, you would use filters to do this, by the way).
Or, say, you have documents in a database. Then what you can do is have a servlet "DatabaseDocument" mapped to "/db". In your JSPs, you can simply link to "/db/foo/bar.doc" and the "DatabaseDocument" servlet would use the extra path info to retrieve "foo/bar.doc" from the database.
- Peter
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
Thanks Peter.
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
Hi, I am still unclear about where this "extra path" is specified in the HTML form. Also, if this "extra path" is arbitrary, then does getPathTranslated() reflect the real path to the file being accessed by "extra path"?
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Cameron Park:
Hi, I am still unclear about where this "extra path" is specified in the HTML form.

It is part of your request path. Take my earlier example of the DatabaseDocument servlet creating a virtual directory under "/db", relative to the root of your web-application. To this end, you would configure the servlet engine to map "/db" to the DatabaseDocument servlet.
When a HTTP request comes in for "/mywebapplication/db/foo/bar.doc?argument=value", the servlet engine splits this path up in four parts.

  • The first is the root of your web-application, "/mywebapplication". This is the value returned by HttpServletRequest.getContextPath().
  • The second part is the path that caused the request to be mapped to the DatabaseDocument servlet. This is "/db" -- the path you mapped the servlet to. It is the value returned by HttpRequest.getServletPath(). (You should never hardcode the path into the servlet, but use getServletPath() instead).
  • The third part is any path information which comes after the servlet path "/db". The servlet engine makes no attempt to interpret this; it is up to the servlet itself to retrieve it by calling HttpRequest.getPathInfo() and do with it whatever it needs to do with it. The DatabaseDocument servlet would use it to determine which database record to access. Its value is "/foo/bar.doc".
  • The fourth part is the query string "?argument=value". You can retrieve it using HttpRequest.getQueryString(), but usually you will let the servlet engine interpret the query string and call HttpServletRequest.getParameter() instead.

  • Also, if this "extra path" is arbitrary, then does getPathTranslated() reflect the real path to the file being accessed by "extra path"?

    Yes. This only makes sense if the extra path information should represent a filesystem path in the first place, of course -- in the example of the DatabaseDocument servlet, getPathTranslated() would not serve any useful purpose. (BTW, getPathTranslated() is directly related to ServletContext.getRealPath()).
    - Peter

    [This message has been edited by Peter den Haan (edited April 28, 2001).]
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
Thank you so much Peter.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getPathInfo() and "extra path"
 
Similar Threads
Command-line problems
JFaceDbc and MySQL
request.getPathInfo()
getPathInfo() and getPathTranslated()
what is request.getPathInfo()