English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
online tool-When a Servlet throws an exception, the Web container uses the exception type element web.xml
You must use the configuration in web.xml to search for the matching configuration for the type of exception thrown. error-page element to specify the page for a specificException or HTTP status code make the corresponding Servlet call.
Assuming there is a ErrorHandler The Servlet is called when any defined exception or error occurs. The following items will be created in web.xml.
<!-- servlet definition --> <servlet> <servlet-name>ErrorHandler</servlet-name> <servlet-class>ErrorHandler</servlet-class> </servlet> <!-- servlet mapping --> <servlet-mapping> <servlet-name>ErrorHandler</servlet-name> <url-pattern>/ErrorHandler</url-pattern> </servlet-mapping> <!-- error-error page related to code --> <error-page> <error-code>404</error-code> <location>/ErrorHandler</location> </error-page> <error-page> <error-code>403</error-code> <location>/ErrorHandler</location> </error-page> <!-- exception-error page related to 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>
If you want to have a general error handler for all exceptions, you should define the following error-page, rather than defining a separate error for each exception-page element:
<error-page> <exception-type>java.lang.Throwable</exception-type > <location>/ErrorHandler</location> </error-page>
The following are points to note about the exception handling in the above web.xml:
Servlet ErrorHandler is defined in the same way as other Servlets and is configured in web.xml.
if there is an error status code, regardless of 404(Not Found 未找到)or 403(Forbidden 禁止),it will call the ErrorHandler Servlet.
If the Web application throws ServletException or IOExceptionthen the Web container will call the ErrorHandler Servlet.
You can define different error handlers to handle different types of errors or exceptions. The example above is very general, and it is hoped that you can understand the basic concepts through the example.
The following is a list of request attributes that the Servlet for error handling can access, used for analyzing errors/The nature of the exception.
Serial Number | Property & Description |
---|---|
1 | javax.servlet.error.status_code This property provides the status code, and the status code can be stored for analysis after being stored as a java.lang.Integer data type. |
2 | javax.servlet.error.exception_type This property provides information about the type of the exception, and the exception type can be stored for analysis after being stored as a java.lang.Class data type. |
3 | javax.servlet.error.message This property provides information about the exact error message, and the information can be stored for analysis after being stored as a java.lang.String data type. |
4 | javax.servlet.error.request_uri This property provides information about the URL that called the Servlet, and the information can be stored for analysis after being stored as a java.lang.String data type. |
5 | javax.servlet.error.exception This property provides information about the exception that occurred, and the information can be stored for analysis after being stored as a java.lang.Throwable data type. |
6 | javax.servlet.error.servlet_name This attribute provides the name of the Servlet, which can be stored and analyzed after being stored as a java.lang.String data type. |
The following is a Servlet example that will handle the error handler for any error or exception you define.
This example gives you a basic understanding of exception handling in Servlets, you can use the same concepts to write more complex exception handling applications:
//Import necessary java libraries import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; //Extend the HttpServlet class public class ErrorHandler extends HttpServlet { // Method to handle GET method requests public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 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 the response content type response.setContentType("text"/html;charset=UTF-8-8"); PrintWriter out = response.getWriter(); String title = "Basic Tutorial Website Error/Exception Information"; String docType = "<!DOCTYPE html>\n"; out.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" + "<body bgcolor=\"#f0f0f0\">\n"); out.println("<h1>Basic Tutorial Website Exception Information Example Demonstration</h1"> if (throwable == null && statusCode == null) { out.println("<h2>Missing Error Information</h2"> out.println("Please return <a href=\"" + response.encodeURL("http://localhost:8080/)") + "\">Home Page</a>."); } else if (statusCode != null) { out.println("Error code: ", + statusCode); } out.println("<h2>Error Information</h2"> out.println("Servlet Name: ", + servletName + "</br></br>"); out.println("Exception type: ", + throwable.getClass().getName()) + "</br></br>"); out.println("Request URI: ", + requestUri + "<br><br>"); out.println("Exception information: ", + throwable.getMessage()); } out.println("</body>"); out.println("</html>"); } // method to handle POST method requests public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
compile in the usual way ErrorHandler.javaPlace your class files into <Tomcat-installation-directory>/webapps/ROOT/WEB-INF/in the classes.
Let's add the following configuration to the web.xml file to handle exceptions:
<?xml version="1.0" encoding="UTF-8"?> <web-app> <servlet> <servlet-name>ErrorHandler</servlet-name> <servlet-class>com.w3codebox.test.ErrorHandler</servlet-class> </servlet> <!-- servlet mappings --> <servlet-mapping> <servlet-name>ErrorHandler</servlet-name> <url-pattern>/TomcatTest/ErrorHandler</url-pattern> </servlet-mapping> <error-page> <error-code>404</error-code> <location>/TomcatTest/ErrorHandler</location> </error-page> <error-page> <exception-type>java.lang.Throwable</exception-type > <location>/ErrorHandler</location> </error-page> </web-app>
Now, try to use a Servlet that will throw an exception, or enter an incorrect URL, which will trigger the Web container to call ErrorHandler servlet, and display the appropriate message. For example, if you enter an incorrect URL (such as: http://localhost:8080/TomcatTest/If the page (UnKonwPage) is not found, it will display the following result:
Error Code:404