简体   繁体   中英

Dispatcher doesn't throw an exception

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM