2

I am using a subclass of HttpServletRequestWrapper to do some translations on the request parameters, and I cache the translated values the first time they are requested. For example, the first time getQueryString() is called, I call super.getQueryString() and calculate the result that I want and keep it in a field, and then return it. Next times, I just use the cached result.

This method works like a charm unless there's some "forwarding". When a request is forwarded, Tomcat replaces the original request, so my cached query string is not changed, and the forwarded page gets the original query string, not the one that is forwarded to.

Overriding the setRequest() method to clear the cache doesn't help either, as if the request is wrapped twice, it calls the setRequest on the inner wrapper (which is not mine), and I have no way to know when it happens.

I'm looking for a way to be notified when there is a change in the wrapped request hierarchy, so that I can clear the cache, when there is a "forward".

Iravanchi
  • 5,139
  • 9
  • 40
  • 56
  • 1
    Why don't just use a Filter which modifies (and processes and stores your cache value) the request data just before the request is processed? This would be more like doing the specs think you should do such things. – HefferWolf Apr 11 '11 at 14:12
  • I'm wrapping the request in a filter, but I guess the specs say that you should not modify the original request values, rather wrap requests. See this: http://stackoverflow.com/questions/1413129/modify-request-parameter-with-servlet-filter – Iravanchi Apr 11 '11 at 14:34

1 Answers1

12

The original request URI is available as request attribute with the key RequestDispatcher.FORWARD_REQUEST_URI.

String originalRequestURI = request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI);

if (originalRequestURI != null) {
    // It was forwarded. Now get the query string as follows.
    String originalQueryString = request.getAttribute(RequestDispatcher.FORWARD_QUERY_STRING);
}

Note: in older Servlet API versions you need to hardcode the key instead.

String originalRequestURI = request.getAttribute("javax.servlet.forward.request_uri");
// ...
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555