通过RequestDispatcher检测JSP页面中的异常 [英] Detect exceptions in JSP page via RequestDispatcher
问题描述
我有一个JSP页面,其中指定了一个错误页面.我通过以下方式从servlet调用页面:
I have a JSP page with an error page specified. I call the page from a servlet via:
RequestDispatcher rd = ctx.getRequestDispatcher( jspPage );
rd.include( req, res );
如何检测jsp页面是否转发到错误页面?我想以不同的方式处理异常,但不会引发异常.不幸的是,我无法修改JSP页面本身或错误页面.
How can I detect if the jsp page forwarded to the error page or not? I want to handle exceptions differently but no exception is thrown. And unfortunately I can't modify the JSP page itself or the error page.
我想在include()行之后这样的事情可能有用,有什么想法吗?
I'm thinking something like this might work after the include() line, any thoughts?
Object errorServletName = req.getAttribute( "javax.servlet.error.servlet_name" );
if ( errorServletName != null )
{ there was an error in the JSP... }
推荐答案
将此添加到您的web.xml文件中:
Add to your web.xml file this:
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/error.html</location>
</error-page>
假设您具有堆栈跟踪打印输出或类似的方式来通知用户他们在error.html页面上遇到的问题.
Assuming that you have stack trace printout or similar way of notifying the user about there problem they experienced on error.html page.
这篇关于通过RequestDispatcher检测JSP页面中的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!