4

I have a servlet that looks something like this:

public class ExampleServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().println(request.getPathInfo());
    }
}

with a web.xml mapping like:

<servlet>
    <servlet-name>example</servlet-name>
    <servlet-class>com.example.ExampleServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>example</servlet-name>
    <url-pattern>/example/*</url-pattern>
</servlet-mapping>

and it gives me exactly what I expect... If I go to http://localhost:8080/example/foo it prints "/foo". However, if I change the servlet to forward to a JSP file:

public class ExampleServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // do something here to check the value of request.getPathInfo()
        request.getRequestDispatcher("whatever.jsp").forward(request, response);
    }
}

then when I check the value of getPathInfo() it now reports "whatever.jsp" instead of "foo".

  1. Why has this changed before it's been forwarded to the JSP?
  2. How can I detect what URL the user's looking for?

EDIT: Just in case it matters this is on Google App Engine. Don't think it should though.

Jeremy Logan
  • 47,151
  • 38
  • 123
  • 143
  • At the second case, are you checking the getPathInfo before or after the forward request? Or you do that in the jsp file? – kgiannakakis Jun 23 '09 at 06:14
  • I'm doing it before, where I put the comment in. (// do something here to check the value of request.getPathInfo()) – Jeremy Logan Jun 23 '09 at 06:28

2 Answers2

16

The question is vague and ambiguous (is the servlet calling itself on every forward again?), but it much sounds like that you need request.getAttribute("javax.servlet.forward.request_uri").

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
2

The URL the user (browser) requested can be accessed from the request by:

request.getRequestURL()

Alternatively the request has a whole bunch of accessors to get the various pieces of the URL as well as those on ServletRequest.

To redirect to a different URL change the response rather than the request:

response.sendRedirect(theURLToRedirectTo)
Tom
  • 43,583
  • 4
  • 41
  • 61
  • Well, I'm actually using one of those above, getPathInfo(). The docs for it say: "Returns any extra path information associated with the URL the client sent when it made this request." but that clearly isn't what's happening. – Jeremy Logan Jun 23 '09 at 06:30
  • Why not? Was "/foo" not the extra path? or was "whatever.jsp" not the extra path. Both of these were the extra paths in your case. – Adeel Ansari Jun 23 '09 at 07:50
  • It was showing the JSP as the path, not foo. I don't see how it COULD be the JSP since I'm asking for it in the servlet, before I'm sending the forward to the JSP. – Jeremy Logan Jun 23 '09 at 07:52