tags:

views:

179

answers:

1

Thanks to everyone in advance -

I am aware of closing the jspwriter/outputstream and returning as a method to stop further execution in the main context. Has anyone found a way to stop execution outside of the main context? From my understanding of how jsp is 'compiled' etc I do not think this is possible, but I thought I should see if anyone has any clever solutions -

Thanks,

Sam

A: 

I consider this to be an anti-pattern that should generally be avoided. Just because ASP.NET lets you do this doesn't mean it's a good feature. That being said, you can simulate the behavior by implementing a servlet request filter. You would need to configure this filter in your web.xml to filter any requests handled by JSPServlet and other servlets that you would want to abort.

For this to work, any exception handling code you have would need to ignore or rethrow ServletRequestAbortException.

public class CancellableRequestFilter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    public void destroy() {
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        try {
            chain.doFilter(request, response);
        } catch (ServletRequestAbortException e) {
            //TODO: Maybe flush/close response here.
        }
    }

    public static void endRequest() {
        throw new ServletRequestAbortException();
    }

    public static class ServletRequestAbortException extends RuntimeException {
    }
}

Example call from a JSP:

<% com.your.pkg.CancellableRequestFilter.endRequest(); %>
Chris Eldredge