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 !
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.