4 Replies Latest reply on Apr 20, 2006 5:37 PM by Adam Warski

    request.getRequestURI() in an include filter

    Adam Warski Master

      Hello,
      I have a servlet mapped to context path /a, and a filter mapped to /b, with set to INCLUDE and FORWARD. In the servlet, I do the following: request.getRequestDispatcher("/b").include(request, response); which should invoke the filter, and invokes it. However, when in the filter I do request.getRequestURI() I get "/a", instead of, as I would expect - "/b". If I change to request.getRequestDispatcher("/b").forward(request, response); the request URI is correct. Should it be so? If yes, how can I get the true request URI in the filter? Or is there some logical mistake I make here?

      --
      Cheers,
      Adam

        • 1. Re: request.getRequestURI() in an include filter
          chris griffith Expert

          Adam,

          I would expect this behavior. If you do not understand this then review the Java API for RequestDispatcher.

          cgriffith

          • 2. Re: request.getRequestURI() in an include filter
            Adam Warski Master

            Hello,
            I've read the javadocs, but in description of the include method there is no explition mention about how the request object is modified (as opposed to forward - "For a RequestDispatcher obtained via getRequestDispatcher(), the ServletRequest object has its path elements and parameters adjusted to match the path of the target resource."). And if I include a resource, logically, the requestURI does change.

            Also, behaviour of this is different in portlets - there, if you include a resource in exactly the same way (except that the request dispatcher is obtained with a PortletRequest, not a ServletRequest), the requestURI is changed.

            --
            Adam

            • 3. Re: request.getRequestURI() in an include filter
              chris griffith Expert

              Adam,

              I am not sure how to make you understand the difference. The API is very clear as far as I can tell. The include description does not mention changing the request because it does not change the request.

              Maybe if you consider the request/response cycle things will become more clear. When a request is processed, if an forward is called, the request is altered to new resource and the response is cleared. This would be kind of like starting the request/response cycle over only keeping any request processing in tact. If an include is called, the request is not changed. The included resource is called using same request object and the output is appended to the response.

              Does this help? cgriffith

              • 4. Re: request.getRequestURI() in an include filter
                Adam Warski Master

                Hello,

                thanks, yes, I see now that it's the way the API is designed, and in a way it is logical. My way would be logical too I guess, maybe even more, but I suppose it starts to be a matter of taste. Anyway, if it is, then I suppose there's something wrong with portlets - if invoking an include in them changes the requestURI.

                --
                Cheers,
                Adam