servlet处理异常的方式可以自定义一个servlet进行处理异常,例如自定义ErrorServlt处理异常。
ErrorServlet在web.xml的配置方式与其他servlet相同,如下代码:
<servlet>
<servlet-name>ErrorHandler</servlet-name>
<servlet-class>ErrorHandler</servlet-class> //具体到工程中,要写清楚具体的包名
</servlet>
<servlet-mapping>
<servlet-name>ErrorHandler</servlet-name>
<url-pattern>/ErrorHandler</url-pattern>
</servlet-mapping>
//错误页面 无论什么错误都交于ErrorHandler处理
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/ErrorHandler</location>
</error-page>
在此也可自定义处理各种错误类型的页面
<!--error-code-->
<error-page>
<error-code>404</error-code>
<location>/ErrorHandler</location>
</error-apage>
<error-page>
<error-code>403</error-code>
<location>/ErrorHandler</location>
</error-apage>
<!--exception-type-->
<error-page>
<exception-type>
javax.servlet.ServletException
</exception-type>
<location>/ErrorHandler</location>
</error-page>
<error-page>
<exception-type>
java.io.IOException
</exception-type>
<location>/ErrorHandler</location>
</error-page>
servlet异常的属性主要有以下几种:status_code(Integer),exception_type(java.lang.Class),message(String),reuest_uri(String),exception(Throwable),servlet_name(String)
其中Throwable 主要包括error异常和Exception异常。
程序实例如下:
import java.io.*;import javax.servlet.*;import javax.servlet.http.*;import java.util.*;// 扩展 HttpServlet 类public class ErrorHandler extends HttpServlet { // 处理 GET 方法请求的方法 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 分析 Servlet 异常 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"; } // 设置响应内容类型 response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Error/Exception Information"; String docType = "<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n"; out.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" + "<body bgcolor=\"#f0f0f0\">\n"); if (throwable == null && statusCode == null){ out.println("<h2>Error information is missing</h2>"); out.println("Please return to the <a href=\"" + response.encodeURL("http://localhost:8080/") + "\">Home Page</a>."); }else if (statusCode != null){ out.println("The status code : " + statusCode); }else{ out.println("<h2 class="tutheader">Error information</h2>"); out.println("Servlet Name : " + servletName + "</br></br>"); out.println("Exception Type : " + throwable.getClass( ).getName( ) + "</br></br>"); out.println("The request URI: " + requestUri + "<br><br>"); out.println("The exception message: " + throwable.getMessage( )); } out.println("</body>"); out.println("</html>"); } // 处理 POST 方法请求的方法 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}