wood burning stoves 2.0*
The moose likes Servlets and the fly likes request.getServletPath() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "request.getServletPath()" Watch "request.getServletPath()" New topic
Author

request.getServletPath()

Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
Hi, I have a question about request's getServletPath(). I have a servlet at c:\tomcat\webapps\ch10\web-inf\classes\thisServlet, but when I called request's getServletPath() within itself, it returns http://localhost:8080/servlet/thisServlet, not http://localhost:8080/ch10/servlet/thisServlet. When I need to display the path to thisServlet using getServletPath(), the link is not found!
Can someone please help me to understand what exactly does getServletPath() do. It doesn't seem to return the servlet's path as the API says.
[This message has been edited by Cameron Park (edited May 10, 2001).]
[This message has been edited by Cameron Park (edited May 10, 2001).]
Jayson Falkner
Author
Ranch Hand

Joined: May 07, 2001
Posts: 57
The Servlet 2.2 API says:

Returns the part of this 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.

So your question was why does it return "http://localhost:8080/servlet/thisServlet" .
Because that is where you have the servlet mapped. Check the web.xml file for your continer and web application's directory. Figure out which one has the servlet-mapping for that servlet.
That mapping should correspond to what getServletPath() is returning. If you want it to have a different mapping you can change it there.
Jayson Falkner
V.P./CTO, Amberjack Software LLC
Jayson@jspinsider.com
www.jspinsider.com


Jayson Falkner<br />jayson@jspinsider.com<br />Author of <a href="http://www.jspbook.com" target="_blank" rel="nofollow">Servlets and JavaServer Pages; the J2EE Web Tier</a>
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Cameron Park:
Hi, I have a question about request's getServletPath(). I have a servlet at c:\tomcat\webapps\ch10\web-inf\classes\thisServlet, but when I called request's getServletPath() within itself, it returns http://localhost:8080/servlet/thisServlet, not http://localhost:8080/ch10/servlet/thisServlet.

I would be very amazed indeed if that was really what it returned. In your case, it should probably return "/servlet/thisServlet" (without protocol, host or port).
Let's take a look at the anatomy of a HTTP request by analysing an example. Assume that

  • You have a server running on "www.cameronsworld.com", port 8080.
  • One of your interests is gardening and you created a web-application mapped to "/gardens".
  • In that application, there is a servlet generating layouts for French gardens, mapped to "/french" (relative to the web-app root).

  • What happens when a user clicks on a link http://www.cameronsworld.com:8080/gardens/french/small?fountain=yes?

    • The browser will send a HTTP GET request to host "www.cameronsworld.com", port 8080. You can retrieve this information using ServletRequest.getScheme(), ServletRequest.getServerName() and ServletRequest.getServerPort()..
    • The servlet engine will receive this request and note that "/gardens" is mapped to your web application. "/gardens" will be the value returned by HttpServletRequest.getContextPath().
    • In your web-app, the engine attempts to match the remainder of the path ("/french/small") with the servlets, JSPs, HTML and other files on the server. Your French garden servlet "/french" exactly matches the start of this path, so that is where the request will go. "/french" will be the value returned by HttpServletRequest.getServletPath().
    • Of the original path, only "/small" is left now. The servlet engine already knows where the request needs to go and basically ignores it. The servlet itself can use this extra path information for its own purposes, for example, to create a virtual directory tree under "/french". "/small" will be the value returned by HttpServletRequest.getPathInfo().
    • Finally, there is a query string "fountain=yes". This is the value returned by HttpServletRequest.getQueryString(). You would rarely access this directly, because the servlet engine splits this up for you in parameter names and values. ServletRequest.getParameter("fountain") will return "yes".

    • Section 5.4 of the servlet spec, v2.2, gives you all the details with a different example. There are some other related methods which you may want to use.

      • You can get the entire request URI (exactly as it appeared in the HTTP request header, up to but not including the query string) using HttpServletRequest.getRequestURI(). In this case, it might return "/gardens/french/small".
      • The utility method HttpUtils.getRequestURL reconstructs the original request "http://www.cameronsworld.com:8080/gardens/french/small" (again without query string).

      • By now, you probably understand why you were not getting the result you expected. The web application your servlet lives in is mapped to "ch10". That is why "ch10" was not part of the path returned by getServletPath(): this method returns the path relative to the web-app root. You don't mention why you need this path, but HttpUtils.getRequestURL may be the method you're looking for. Or you may want to simply append it to the web-app root returned by getContextPath().
        Hope this helps
        - Peter

        [This message has been edited by Peter den Haan (edited May 10, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: request.getServletPath()
 
Similar Threads
how to arrange package in WEB-INF
how to run servlet
Tomcat
How to invoke a servlet?
Extending BodyTagSupport (HFS CH10 Q9)