调度程序不会抛出异常

发布于 2024-11-27 01:21:51 字数 1168 浏览 3 评论 0原文

我想让调度程序在我想要转发到不存在的资源时抛出异常,这是我

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜中书 2024-12-04 01:21:51

您的 servlet 可能如下所示:

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 ));
    }

}

}

另外,不要捕获 servlet 方法抛出的 ServletExceptionIOException,如果它们发生在您的应用程序中,并且您正在发生一些非常糟糕的事情不应该像在代码中那样吞掉这些异常。这些异常应该保持原样,并且容器应该捕获它们。您应该记录它们,而不是尝试打印堆栈跟踪,因为这将在 err 流中打印,并且在生产服务器上不可见。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文