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(); %>