Dispatcher doesn't throw an exception

蹲街弑〆低调 提交于 2019-12-11 05:07:57

问题


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 !


回答1:


Here's how your servlet could look like:

public class SimpleServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    // do something at the servlet here

    String page = (String) req.getAttribute("page"); // page to be forwarded
                                                        // form servlet to
                                                        // jsp
    if (page == null) {
        page = req.getParameter("page");// page to be forwarded form jsp to
                                        // servlet
    }

    this.forwardIfExists(req, resp, page);

}

protected void forwardIfExists(HttpServletRequest req,
        HttpServletResponse resp, String page) throws ServletException, IOException {

    File pagePath = new File(this.getServletContext().getRealPath(page));

    if ( pagePath.exists() ) {
        req.getRequestDispatcher( page ).forward(req, resp);
    } else {
        throw new IllegalArgumentException(String.format( "The page %s does not exist", page ));
    }

}

}

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.



来源:https://stackoverflow.com/questions/6886441/dispatcher-doesnt-throw-an-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!