Servlet Exception Handling

1.Servlet Exception Handling

scenes to be used:

When an exception is thrown when a Servlet, the Web container used exception-type element web.xml search to match the type of exception thrown configuration. web.xml using error-page element to specify a particular disorder or HTTP to the corresponding status code Servlet invocation.

 

2.web.xml Configuration

We assume that there is a ErrorHandler the Servlet is called when any abnormality has been defined or an error occurs. The following will be in web.xml entry created.

 1 <!-- servlet 定义 -->
 2 
 3 <servlet>
 4 
 5         <servlet-name>ErrorHandler</servlet-name>
 6 
 7         <servlet-class>ErrorHandler</servlet-class>
 8 
 9 </servlet>
10 
11 <!-- servlet 映射 -->
12 
13 <servlet-mapping>
14 
15         <servlet-name>ErrorHandler</servlet-name>
16 
17         <url-pattern>/ErrorHandler</url-pattern>
18 
19 </servlet-mapping>
20 
21  
22 
23 <!-- error-code 相关的错误页面 -->
24 
25 <error-page>
26 
27     <error-code>404</error-code>
28 
29     <location>/ErrorHandler</location>
30 
31 </error-page>
32 
33 <error-page>
34 
35     <error-code>403</error-code>
36 
37     <location>/ErrorHandler</location>
38 
39 </error-page>
40 
41  
42 
43 <!-- exception-type 相关的错误页面 -->
44 
45 <error-page>
46 
47     <exception-type>
48 
49           javax.servlet.ServletException
50 
51     </exception-type >
52 
53     <location>/ErrorHandler</location>
54 
55 </error-page>
56 
57  
58 
59 <error-page>
60 
61     <exception-type>java.io.IOException</exception-type >
62 
63     <location>/ErrorHandler</location>
64 
65 </error-page>

 

If you want to have a generic error handler for all exceptions, you should define the following error-page, rather than for each individual anomalies defined error-page elements:

<error-page>

    <exception-type>java.lang.Throwable</exception-type >

    <location>/ErrorHandler</location>

</error-page>

 

Add the following configuration in web.xml to handle exceptions:

 1 <?xml version="1.0" encoding="UTF-8"?>  
 2 
 3 <web-app>  
 4 
 5 <servlet>
 6 
 7         <servlet-name>ErrorHandler</servlet-name>
 8 
 9         <servlet-class>com.sxt.test.ErrorHandler</servlet-class>
10 
11 </servlet>
12 
13 <!-- servlet mappings -->
14 
15 <servlet-mapping>
16 
17         <servlet-name>ErrorHandler</servlet-name>
18 
19         <url-pattern>/TomcatTest/ErrorHandler</url-pattern>
20 
21 </servlet-mapping>
22 
23 <error-page>
24 
25     <error-code>404</error-code>
26 
27     <location>/TomcatTest/ErrorHandler</location>
28 
29 </error-page>
30 
31 <error-page>
32 
33     <exception-type>java.lang.Throwable</exception-type >
34 
35     <location>/ErrorHandler</location>
36 
37 </error-page>
38 
39 </web-app>  

Now, try to use a produce abnormal Servlet, or enter a wrong the URL of , which will trigger the Web calling container ErrorHandler the Servlet , and displays the appropriate message.

Guess you like

Origin www.cnblogs.com/qq2267711589/p/11025772.html
Recommended