使用错误页面的Servlet中的异常处理无法正常工作

时间:2015-11-05 20:30:31

标签: java servlets exception-handling

我添加了一个异常处理servlet,并使用error-page标签在web.xml中对其进行了配置。但是当发生异常时,它不会调用该servlet。请建议。

Example 
   -src
     -com.journaldev.servlet.exception
        -MyExceptionServlet.java
        -AppExceptionHandler.java
  -WebContent
     -WEB-INF
        -web.xml
     -index.html

MyExceptionServlet.java

    @WebServlet("/MyExceptionServlet")
    public class MyExceptionServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;

        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            throw new ServletException("GET method is not supported.");
        }

    }

AppExceptionHandler.java

 @WebServlet("/AppExceptionHandler")
    public class AppExceptionHandler extends HttpServlet {
        private static final long serialVersionUID = 1L;

        protected void doGet(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException {
            processError(request, response);
        }

        protected void doPost(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException {
            processError(request, response);
        }

        private void processError(HttpServletRequest request,
                HttpServletResponse response) throws IOException {
            // Analyze the servlet exception
            Throwable throwable = (Throwable) request
                    .getAttribute("javax.servlet.error.exception");
            Integer statusCode = (Integer) request
                   .getAttribute("javax.servlet.error.status_code");
            String servletName = (String) request
                    .getAttribute("javax.servlet.error.servlet_name");
            if (servletName == null) {
                servletName = "Unknown";
            }
            String requestUri = (String) request
                    .getAttribute("javax.servlet.error.request_uri");
            if (requestUri == null) {
                requestUri = "Unknown";
            }
         // Set response content type
              response.setContentType("text/html");

              PrintWriter out = response.getWriter();
              out.write("<html><head><title>Exception/Error Details</title></head><body>");
              if(statusCode != 500){
                  out.write("<h3>Error Details</h3>");
                  out.write("<strong>Status Code</strong>:"+statusCode+"<br>");
                  out.write("<strong>Requested URI</strong>:"+requestUri);
              }else{
                  out.write("<h3>Exception Details</h3>");
                  out.write("<ul><li>Servlet Name:"+servletName+"</li>");
                  out.write("<li>Exception Name:"+throwable.getClass().getName()+"</li>");
                  out.write("<li>Requested URI:"+requestUri+"</li>");
                  out.write("<li>Exception Message:"+throwable.getMessage()+"</li>");
                  out.write("</ul>");
              }

              out.write("<br><br>");
              out.write("<a href=\"index.html\">Home Page</a>");
              out.write("</body></html>");
        }
    }

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>ServletExceptionHandling</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  <error-page>
    <error-code>404</error-code>
    <location>/AppExceptionHandler</location>
  </error-page>

  <error-page>
  <exception-type>javax.servlet.ServletException</exception-type>
  <location>/AppExceptionHandler</location>
  </error-page>
</web-app>

的index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome Pager</title>
</head>
<body>
    Welcome To HTML
</body>
</html>

0 个答案:

没有答案