调度程序不会抛出异常
我想让调度程序在我想要转发到不存在的资源时抛出异常,这是我
String page = (String) request.getAttribute("page"); //page to be forwarded form servlet to jsp
if (page == null) {
page = request.getParameter("page");//page to be forwarded form jsp to servlet
}
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/InstitutionPages/" + page + ".jsp");
try {
dispatcher.forward(request, response);
} catch (IOException ex) {
ex.printStackTrace();
LogoutServlet.redirectToLoginPage(request, response);
} catch (javax.servlet.ServletException e) {
e.printStackTrace();
Logger.getLogger(RegistrarManagementServlet.class.getName()).log(Level.SEVERE, null, e);
LogoutServlet.redirectToLoginPage(request, response);
} catch (java.lang.IllegalArgumentException e) {
e.printStackTrace();
LogoutServlet.redirectToLoginPage(request, response);
}
在页面中的代码,我发送无效的页面名称,但此错误发生在控制台上
SEVERE: PWC6117: File "D:\versions\v30\OnlineQuerySystem_New\build\web\WEB-INF\InstitutionPages\Registerkk.jsp" not found
没有打印任何堆栈跟踪!
I want to make the dispatcher throws an exception when I want to forward to non-existing resource, here's my code
String page = (String) request.getAttribute("page"); //page to be forwarded form servlet to jsp
if (page == null) {
page = request.getParameter("page");//page to be forwarded form jsp to servlet
}
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/InstitutionPages/" + page + ".jsp");
try {
dispatcher.forward(request, response);
} catch (IOException ex) {
ex.printStackTrace();
LogoutServlet.redirectToLoginPage(request, response);
} catch (javax.servlet.ServletException e) {
e.printStackTrace();
Logger.getLogger(RegistrarManagementServlet.class.getName()).log(Level.SEVERE, null, e);
LogoutServlet.redirectToLoginPage(request, response);
} catch (java.lang.IllegalArgumentException e) {
e.printStackTrace();
LogoutServlet.redirectToLoginPage(request, response);
}
in page, I send invalid page name, but this error occurs on console
SEVERE: PWC6117: File "D:\versions\v30\OnlineQuerySystem_New\build\web\WEB-INF\InstitutionPages\Registerkk.jsp" not found
No one of Stack traces is printed !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 servlet 可能如下所示:
另外,不要捕获 servlet 方法抛出的 ServletException 或 IOException,如果它们发生在您的应用程序中,并且您正在发生一些非常糟糕的事情不应该像在代码中那样吞掉这些异常。这些异常应该保持原样,并且容器应该捕获它们。您应该记录它们,而不是尝试打印堆栈跟踪,因为这将在 err 流中打印,并且在生产服务器上不可见。
Here's how your servlet could look like:
Also, do not catch the ServletException or IOException thrown by the servlet methods, if they happened something really bad is happening in your application and you should not swallow these exceptions as you are in your code. These exceptions should be be left as they are and the container should catch them. You should log them and not try to print the stack traces, as this is going to print at the err stream and will not be visible at a production server.