HttpServletRequestWrapper, Tomcat and forwards

I’ve been working on getting the JCatapult security framework to leverage Struts actions rather than a bunch of configuration and additional custom code. In order to pull all of this off, I had to modify the HttpServletRequest in order to make it seem like the requested URI was different than the original request URI. I used a HttpServletRequestWrapper in order to pull this off like this:

final HttpServletRequest httpRequest = (HttpServletRequest) request;

HttpServletRequestWrapper wrapper = new HttpServletRequestWrapper(httpRequest) {
  @Override
  public String getRequestURI() {
    return successfulLoginURI;
  }

  @Override
  public String getServletPath() {
    return successfulLoginURI;
  }

  @Override
  public RequestDispatcher getRequestDispatcher(String uri) {
    final RequestDispatcher rd = httpRequest.getRequestDispatcher(uri);
    return new RequestDispatcher() {
      public void forward(ServletRequest servletRequest, ServletResponse servletResponse)
      throws ServletException, IOException {
        rd.forward(httpRequest, servletResponse);
      }

      public void include(ServletRequest servletRequest, ServletResponse servletResponse)
      throws ServletException, IOException {
        rd.include(httpRequest, servletResponse);
      }
    };
  }
};

As it turns out, I had to do that little tricky part at the end with the RequestDispatcher because Tomcat fails during a forward if the incoming HttpServletRequest is a wrapper.

5 thoughts on “HttpServletRequestWrapper, Tomcat and forwards

  1. How did you discover that tomcat failed on forwards when the request had been wrapped? I can’t find any information online and the workaround you are using will not work for me since I need to have the wrapped request for the resource being forwarded to.

    Like

  2. It was pretty simple. I just wrapped the request so that the request URI was something different. This was done in a filter. The request got passed down to struts and struts pulled out the new URI correctly and invoked the action. Once the action finished Struts tried to forward the request to the JSP and this is where it failed. The container was unable to perform the forward because the HttpServletRequest that was being passed to the request dispatcher had a different request URI and was a wrapper. Tomcat doesn’t like that and was throwing some strange exceptions back.

    Like

  3. Hi.

    Possibly is not exactly the correct place, but I need to resolve this stuff and your blog is related with this, so I ask you for some help.

    I’m using tomcat 5.0.x and trying to get the parameters in URL I get null.

    1 way:
    for (String requestName : requests.keySet()) {
    if (requests.get(requestName) instanceof HttpServletRequestWrapper) {
    request = (HttpServletRequest) ((HttpServletRequestWrapper) requests
    .get(requestName)).getRequest();
    retVal = request.getParameter(“area”);
    System.out.println(“retVal: ” + retVal);
    }
    }

    2 way:
    Map params = fc.getExternalContext().getRequestParameterMap();
    Iterator it = params.entrySet().iterator();
    while (it.hasNext()) {
    Map.Entry pairs = (Map.Entry) it.next();
    System.out.println(pairs.getKey() + ” = ” + pairs.getValue());
    }

    3 way:
    String area = (String) FacesContext.getCurrentInstance()
    .getExternalContext().getRequestParameterMap().get(“area”);

    4 way:
    area = fc.getCurrentInstance().getExternalContext()
    .getRequestParameterMap().get(“area”);

    and the simple url in index.jsp

    Thanks

    Like

  4. I am facing the same problem that you described: I have a subclassed HttpServletRequestWrapper which I am using to check/add parameters to the request. I am using a filter that wraps this request. While forwarding my filtered request, tomcat overrides the new request and keeps mine intact. Tomcat doesn’t throw any exceptions, just neatly replaces the original request.
    I tried your solution but it doesn’t work for me! Am I missing something?

    Like

  5. I just wanted to say thank-you for this. I know this is a >5 year old blog entry, but I have spent the last couple of days banging my head against the wall with WebLogic 10.3 and its inability to forward to a request dispatcher generated from my custom-wrapped request. In the end, I implemented your “getRequestDispatcher” code above, and it works like magic.

    Many thanks to you, sir!

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s