提前感谢大家-
我知道关闭 jspwriter/outputstream 并返回作为停止在主上下文中进一步执行的方法。有没有人找到在主要上下文之外停止执行的方法?根据我对 jsp 是如何“编译”等的理解,我认为这是不可能的,但我想我应该看看是否有人有任何聪明的解决方案 -
谢谢,
山姆
最佳答案
我认为这是一种通常应该避免的反模式。仅仅因为 ASP.NET 允许您这样做并不意味着它是一个好的功能。也就是说,您可以通过实现 servlet 请求过滤器来模拟行为。您需要在 web.xml
中配置此过滤器,以过滤由 JSPServlet
和您希望中止的其他 servlet 处理的任何请求。
为此,您需要忽略或重新抛出任何异常处理代码 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 {
}
}
来自 JSP 的示例调用:
<% com.your.pkg.CancellableRequestFilter.endRequest(); %>
关于jsp - 在 JSP 中获取 Response.End() 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1223973/