Servlet——异常处理
当 servlet 抛出异常时,Web 容器会在使用 exception-type 元素的 web.xml 配置中查找与抛出的异常类型匹配的内容。要定义 servlet 调用以响应特定错误或 HTTP 状态代码,您必须利用 web.xml 中的 error-page 元素。
异常处理是将系统错误消息转换为用户友好的错误消息的过程。
- 程序异常处理技术:这种在Java编程中使用 try 和 catch 块来管理异常的方法被称为程序异常处理机制。
- 一种管理声明性异常的机制:声明性异常处理技术是指使用 web.xml 文件中的 XML 元素管理异常的方法。如果在多个 servlet 应用程序中发生错误,此方法很方便。
要点
- 如果 Web 应用程序抛出 ServletException 或 IOException,Web 容器将调用 /ErrorHandler servlet。
- 可以定义不同的错误处理程序来处理不同类型的错误或异常。
- ErrorHandler servlet 在 web.xml 文件中指定,其定义方式与任何其他 servlet 相同。
- 如果遇到状态码为 404INot Found) 或 403 的错误,则会调用 ErrorHandler servlet (Forbidden)
请求属性 – 错误/异常 Attributes Descriptionstatus_code Returns a status code that may be saved and studied after being saved in java.lang variable. exception_type Provides information about the exception type that may be kept and studied after being saved in java.lang file. message Provides information about the actual error message that may be saved and studied after being saved in java.lang file request_uri Provides information about the URL used to call the servlet and may be kept and studied after being stored in java.lang variable. exception Provides information about the raised exception, which may be preserved and investigated. servlet_name Returns a list of servlet names that may be saved and studied after being saved in java.lang file. The data type is a string.
web.xml的配置
可以在 web.xml 文件中配置错误代码相关的错误页面。
句法:
< error - page >
< error - code > code < / error - code >
/ url < / location >
< / error - page >
例子:
< error - page >
< error - code > 404 < / error - code >
/ ErrorHandler < / location >
< / error - page >
异常类型相关的错误页面
句法:
< error - page >
< exception - type > exception type < / exception - type >
/ url < / location >
< / error - page >
例子:
< error - page >
< exception - type > javax.servlet.ServletException < / exception - type >
/ ErrorHandler < / location >
< / error - page >
执行:
Java
// Java Program to Illustrate ExceptionHandler
// Import required java libraries
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
// Extend HttpServlet class
public class ExceptionHandler extends HttpServlet {
// Method
// To handle GET method request
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// Analyze the servlet exception
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");
PrintWriter out = response.getWriter();
String title = "Error/Exception Information";
String docType
= "\n";
out.println(docType + "\n"
+ "" + title
+ " \n"
+ "\n");
if (throwable == null && statusCode == null) {
out.println(
"Error information not found
");
out.println("Let's go back to Home Page.");
}
else if (statusCode != null) {
out.println("The status code of an error is : "
+ statusCode);
}
else {
out.println("Error information
");
out.println("Servlet Name : " + servletName
+ "");
out.println("Exception Type : "
+ throwable.getClass().getName()
+ "");
out.println("The request URI: " + requestUri
+ "
");
out.println("The exception message: "
+ throwable.getMessage());
}
out.println("");
out.println("");
}
// Method
// To handle POST method request.
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response);
}
}
XML
ExceptionHandler
ExceptionHandler
ExceptionHandler
/ExceptionHandler
404
/ExceptionHandler
java.lang.Throwable
/ExceptionHandler
文件: web.xml
XML
ExceptionHandler
ExceptionHandler
ExceptionHandler
/ExceptionHandler
404
/ExceptionHandler
java.lang.Throwable
/ExceptionHandler
输出:运行您的 ExceptionHandler。Java代码
现在尝试输入一些不同的 URL,预期的输出将如下: