It is difficult for us to ensure that the programs we write will not cause errors, so we need some error handling mechanisms to help us better solve exceptions and handle errors. When you browse a webpage, if the accessed page does not exist, you will receive an HTTP 404 error message. This problem may occur because the user entered the wrong URL, but more often it is because the server link has an error and the page has been moved or deleted, but forget to modify the original link. Another situation is that the Web application we developed has bugs and exceptions during runtime.
Therefore, this requires us to handle possible errors or exceptions in advance when developing Web applications. Even if errors are inevitable, we should provide some friendly information. In servlet, there are two types of server processing mechanisms: declarative Exception Handling and programmatic exception handling ).
Declarative Exception Handling
This exception is handled by declaring the methods for handling various exceptions in the web. xml file. The format is as follows:
<?xml version='1.0' encoding='utf-8'?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" metadata-complete="true"><error-page><error-code>404</error-code><location>/FileNotFound.html</location></error-page></web-app>
As you can see, <error-code> specifies the HTTP Error code and <location> specifies the page path when an error occurs. Another format is as follows:
<?xml version='1.0' encoding='utf-8'?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" metadata-complete="true"><error-page><error-type>java.io.FileNotFoundException</error-type><location>/ExceptionHandle</location></error-page></web-app>
<Error-type> specifies the full qualified name of the Java exception class, and <location> specifies the processing class (servlet class) for exception handling ). In the preceding format, <location> can also be specified.
For more information about common HTTP Error Codes, see http resolution.
The detailed code for an exception handling class is as follows:
Package COM. shan. web; import Java. io. *; import javax. servlet. *; import javax. servlet. HTTP. *; public class httperrorhandleservlet extends httpservlet {public void Service (httpservletrequest request, httpservletresponse response) throws ioexception, servletexception {request. setcharacterencoding ("UTF-8"); response. setcontenttype ("text/html; charset = UTF-8"); integer status_code = (integer) request. getattribute ("Javax. servlet. error. status_code "); out. println ("<HTML>
To help the servlet analyze the problem for error handling and generate a detailed response, the servlet container sets some useful attributes in the request before forwarding the request to the error page, as shown in table 1:
Attribute name |
Attribute type |
Attribute description |
Javax. servlet. Error. status_code |
Java. Lang. Integer |
HTTP status code |
Javax. servlet. Error. exception_type |
Java. Lang. Class |
The object of the class with no exception captured |
Javax. servlet. Error. Message |
Java. Lang. String |
The message passed to the senderror () method, or the message that is not caught in the exception |
Javax. servlet. Error. Exception |
Java. Lang. throwable |
Uncaptured exception on the call error page |
Javax. servlet. Error. request_uri |
Java. Lang. String |
Uri of the current request |
Javax. servlet. Error. servlet_name |
Java. Lang. String |
The name of the servlet that causes the error page to be called |
Table 1 attributes set by the servlet container in the request object
The complete Web. xml configuration for the above error handling class is as follows:
<?xml version='1.0' encoding='utf-8'?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" metadata-complete="true"><servlet><servlet-name>HttpErrorHandleServlet</servlet-name><servlet-class>com.shan.web.HttpErrorHandleServlet</servlet-class></servlet><servlet-mapping><servlet-name>HttpErrorHandleServlet</servlet-name><url-pattern>/ExceptionHandle</url-pattern></servlet-mapping><error-page><error-code>401</error-code><location>/ExceptionHandle</location></error-page><error-page><error-code>404</error-code><location>/ExceptionHandle</location></error-page></web-app>
Procedural Exception Handling
This exception handling method uses try-catch to capture exceptions and process the caught exceptions accordingly. You can use the following statement in the catch block to output exception information to the log.
Getservletcontext (). log ("servletcontext. log (): exception information "+ ex. tostring (); log ("genericservlet. log (): exception information "+ ex. tostring ());
The two statements can record the exception information to the log. The difference is that if you call the log () method of the genericservlet class, it adds the servlet name before the log message, and calls the log () method of servletcontext to only record the message itself.
You can also use the senderror () method of response to send the error message to the error page.
Use requestdispatcher to handle exceptions
We have introduced the page forwarding mechanism before, so we can encapsulate exceptions into a request, and then use servletcontext. Log () to forward an exception handling servlet for unified processing.
Reprinted please indicate the source: http://blog.csdn.net/iAm333