2.11. Servlet exception handling

发布时间 : 2025-10-25 13:33:40 UTC      

Page Views: 9 views

When a Servlet throws an exception, the Web container uses the exception-type of the element web.xml search for configurations that match the type of exception thrown in the.

You must be in the web.xml for use in error-page element to specify the corresponding Servlet call to a specific exception or HTTP status code.

2.11.1. Web.xml configuration

Hypothetically, there is one ErrorHandler is called when any defined exception or error occurs. The following will be the web.xml item created

  ErrorHandler ErrorHandler    ErrorHandler /ErrorHandler    404 /ErrorHandler   403 /ErrorHandler     javax.servlet.ServletException  /ErrorHandler   java.io.IOException /ErrorHandler  

If you want to have a common error handler for all exceptions, you should define the following error-page instead of defining a separate error-page elements:

<error-page> <exception-type>java.lang.Throwableexception-type > <location>/ErrorHandlerlocation> error-page>    

The following is about the above web.xml points to pay attention to in exception handling:

  • Servlet ErrorHandler is defined in the same way as other Servlet, and in the web.xml configure it.

  • If an error status code occurs, whether it is 404 (Not Found not found) or 403 (Forbidden prohibited), ErrorHandler’s Servlet is called.

  • If the Web application throws a ServletException or IOException ,then the Web container calls the ErrorHandler Servlet.

  • You can define different error handlers to handle different types of errors or exceptions. The above examples are very general, and I hope you can understand the basic concepts through the examples.

Request Properties-error / exception

The following is a list of request attributes that can be accessed by the error-handling Servlet to analyze the nature of the error / exception.

Serial number

Attribute & description

1

javax.servlet.error.status_code the attribute gives a status code, whichcan be stored and analyzed after being stored as a java.lang.Integer data type.

2

javax.servlet.error.exception_type this property gives information aboutthe exception type, which can be stored and parsed after it is stored as a java.lang.Class data type.

3

javax.servlet.error.message this property gives information about the exact error message, which can be stored and parsed after being stored as a java.lang.String data type.

4

javax.servlet.error.request_uri this property gives information about URL calling Servlet, which can be stored and analyzed after being stored as a java.lang.String data type.

5

javax.servlet.error.exception this attribute gives the information generated by the exception, which can be stored and analyzed after being stored as a java.lang.Throwable data type.

6

javax.servlet.error.servlet_name this attribute gives the name of the Servlet, which can be stored and parsed after being stored as a java.lang.String data type.

Servlet error Handler example

The following is an example of Servlet that will handle the error handler when any error or exception that you define occurs.

This example gives you a basic understanding of exception handling in Servlet, and you can write more complex exception handling applications using the same concepts:

//Import necessary Java libraries import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; //Extend HttpServlet Class public class ErrorHandler extends HttpServlet { // Method for handling 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 response content type response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String title = "Rookie Tutorial Error/Exception information"; String docType = "\n"; out.println(docType + "\n" + ""</span> <span class="o">+</span> <span class="n">title</span> <span class="o">+</span> <span class="s2">"\n" + "\"#f0f0f0\">\n"); out.println("

Example demonstration of abnormal information in the rookie tutorial

"
); if (throwable == null && statusCode == null){ out.println("

Error message lost

"
); out.println("Please return \"" + response.encodeURL("http://localhost:8080/") + "\">homepage."); }else if (statusCode != null) { out.println("error code : " + statusCode); }else{ out.println("

error message

"
); out.println("Servlet Name : " + servletName + ""); out.println("Exception : " + throwable.getClass( ).getName( ) + ""); out.println("request URI: " + requestUri + "

"
); out.println("abnormal information: " + throwable.getMessage( )); } out.println(""); out.println(""); } // Method for handling POST method requests public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }

Compile in the usual way ErrorHandler.java to put your class files in / webapps/ROOT/WEB-INF/classes.

Let’s do it in web.xml add the following configuration to the file to handle exceptions:

   ErrorHandler com.runoob.test.ErrorHandler    ErrorHandler /TomcatTest/ErrorHandler   404 /TomcatTest/ErrorHandler   java.lang.Throwable /ErrorHandler   

Now, try to use a Servlet that generates an exception, or enter an incorrectURL, which will trigger the Web container call ErrorHandler and display the appropriate message For example, if you enter an incorrect URL (such as: http://localhost:8080/TomcatTest/UnKonwPage ), it will display the following result:

Image0

《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。