I'm working on a CMS.
My code is inside the doGet() function of a servlet invoked at url "/market". I want a HttpServletRequestWrapper that would pass through all filters set for url "/page".
I expect these filters would update the request, so that an annotation processor could later inject dependencies with correct values.
I'm in a Tomcat server, so I should be able to cast to the right special object, and I don't have to be compliant to other servers.
An associated question is that using req.getRequestDispatcher(path).forward(requestWrapper, responseWrapper);, I was expecting the filters to be invoked. Should they ?
The javadoc says:
This method allows one servlet to do preliminary processing of a request
Filters are by default mapped on the
REQUESTdispatcher only. The below example of a filter mappingis implicitly equivalent to
This means, the filter is only triggered on the "raw" incoming request, not on a forwarded request.
There are three more dispatchers:
FORWARD,INCLUDEandERROR. TheRequestDispatcher#forward()triggers theFORWARDdispatcher. If you'd like to let your filter hook on that as well, then just add it:Note that you need to explicitly specify the
REQUESTdispatcher here, otherwise it would assume that you're overriding it altogether and are only interested inFORWARDdispatcher.Inside the filter, if you'd like to distinguish between a
REQUESTandFORWARD, then you can then check that by determining the presence of a request attribute keyed withRequestDispatcher#FORWARD_REQUEST_URI