简体   繁体   中英

Custom Java Servlet Filter

I'm writing my first ever javax.servlet.Filter impl and am trying to write the portion of the doFilter method where I prevent the request from going any further in the chain:

@Override
public void doFilter(ServletRequest request, ServletResponse response)
        FilterChain chain) throws IOException, ServletException {
    // Check for some stuff in the request...
    boolean passesInspection = inspect(request);

    if(!passesInspection)
        // How do I prevent the request from going any further?
        // I don't want it even getting to the servlet at this point!
}

How do I "block" the request from even making it to the listening servlet? Thanks in advance.

Simply don't call chain.doFilter() . The doFilter() call is what progresses the call. Don't call this and the processing stalls. This is not a good design however. You need to at least inform the caller

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