简体   繁体   中英

Servlet filter URL pattern - how to match all servlets that a particular servlet dispatches the request too

I currently have a servlet that acts as the central point for all incoming AJAX requests. Let's call this servlet AJAXHandler .

AJAX requests can be bundled together into one big request and then the AJAXHandler servlet seperates each request and calls 'RequestDispatcher.include' for each. It then bundles up the response for each servlet into one large response and sends it back to the client.

I also have a filter which I use for caching servlet response XML. I want to know how to specify a servlet filter URL-pattern that applies to any servlet that receives a request via the AJAXDispatcher servlet. Is this possible?

I am not able to use a pattern of '/*' as this will match servlets that are not called via the AJAXDispatcher servlet.

EDIT - The filter is used for caching the response. I need to cache the responses of the individual servlets that the AJAXHandler forwards to, NOT the bundled response that is eventually returned by the AJAXHandler itself.

A simple enough way to do this is to set a request attribute in your Servlet and check for it when returning in the Filter.

public class AjaxHandler extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse resp) {
        req.setAttribute("cache", "true");
        ...
    }
}

In Filter

@Override
public void doFilter(ServletRequest request, ServletResponse response,
        FilterChain chain) throws IOException, ServletException {
    chain.doFilter(request, response)
    if (request.getAttribute("cache") != null) {
        // do your thing
    }

}

Your filter can be mapped to all /* .

Obviously, use an attribute key that is unique to this use case.

Filters can be applied to only match requests which have been forwarded or included via RequestDispatcher . This corresponds to the DispatcherType enumeration, and ServletRequest.getDispatcherType() . By default, filters only match REQUEST , which is the dispatcher type used for the original request into the application from a client.

Once in the filter, you can use the various RequestDispatcher.INCLUDE_* attributes to retrieve information about the including servlet. That should allow you to further only apply the filter when the AJAXHandler is the source.

web.xml

Include <dispatcher> elements within the <filter-mapping> .

<filter-mapping>
    <url-pattern>/*</url-pattern>
    <dispatcher>INCLUDED</dispatcher>
</filter-mapping>

FilterRegistration

FilterRegistration.addMappingForServletNames(...) and FilterRegistration.addMappingForUrlPatterns(...) both take an EnumSet<DispatcherType> as the first parameter.

final FilterRegistration registration = servletContext.addFilter(...);
registration.addMappingForUrlPatterns(
    EnumSet.of(DispatcherType.INCLUDE), true, "/*");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM