JSP异常处理 – JSP错误页面

异常处理在JSP中通过JSP异常页面完成。

JSP中的异常处理

前段时间我写了一篇关于Servlet异常处理以及我们为什么需要它的文章。相同的解释也适用于JSP页面,这就是为什么Java EE为JSP页面提供了清晰的异常处理方法,使用JSP错误页面。要处理JSP页面抛出的异常,我们只需要一个错误页面,并在JSP中使用jsp页面指令定义错误页面。

JSP错误页面

要创建JSP错误页面,我们需要将页面指令属性isErrorPage的值设置为true,然后我们可以访问JSP中的异常jsp隐含对象,并使用它向客户端发送自定义错误消息。

JSP错误页面配置

我们需要设置页面指令errorPage属性来定义处理JSP服务方法抛出的任何异常的JSP。当将JSP错误页面转换为servlet代码时,在Tomcat中它会继承org.apache.jasper.runtime.HttpJspBase

错误页面部署描述符配置

大多数情况下,我们有一个通用的错误页面,我们希望为所有的JSP使用,因此我们可以在web.xml中使用error-page元素定义错误页面,而不是在所有的JSP中单独配置它。我们还可以配置JSP错误页面来处理其他错误代码,如404。让我们看看这些如何在web应用程序中配合。我们将创建一个简单的web应用程序JSPExceptionHandling,其项目结构将如下图所示。应用程序的入口点是index.jsp,其代码如下。

<%@ page language="java" contentType="text/html; charset=US-ASCII"
    pageEncoding="US-ASCII"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Login Page</title>
</head>
<body>
<form action="login.jsp" method="post">
<strong>User ID</strong>:<input type="text" name="id"><br>
<strong>Password</strong>:<input type="password" name="password"><br>
<input type="submit" value="Login">
</form>
</body>
</html>

当我们提交表单时,请求将被发送到login.jsp,代码如下。

<%@ page language="java" contentType="text/html; charset=US-ASCII"
    pageEncoding="US-ASCII" errorPage="error.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>User Home Page</title>
</head>
<body>
<%
	String user = request.getParameter("id");
	String pwd = request.getParameter("password");
	
	if(user == null || "".equals(user) || pwd == null || "".equals(pwd)){
		throw new ServletException("Mandatory Parameter missing");
	}
	
%>

<%-- do some DB processing, not doing anything for simplicity --%>
Hi <%=user %>
</body>
</html>

请注意,如果输入参数为空或null,则会抛出ServletException,并附有适当的消息,errorPage被定义为error.jsp,其代码如下。

<%@ page language="java" contentType="text/html; charset=US-ASCII"
    pageEncoding="US-ASCII" isErrorPage="true"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Error Page</title>
</head>
<body>
<% if(response.getStatus() == 500){ %>
<font color="red">Error: <%=exception.getMessage() %></font><br>

<%-- include login page --%>
<%@ include file="index.jsp"%>
<%}else {%>
Hi There, error code is <%=response.getStatus() %><br>
Please go to <a href="/index.jsp">home page</a>
<%} %>
</body>
</html>

请注意isErrorPage页面指令属性值为true。当应用程序资源抛出异常时,错误代码为500,代码编写用于处理应用程序级别异常和404 – 页面未找到等错误。还请注意,在任何异常情况下,使用包含指令向用户呈现登录页面。这是我们在web.xml中为应用程序定义错误页面的地方。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns="https://java.sun.com/xml/ns/javaee" xsi:schemaLocation="https://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>JSPExceptionHandling</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>
   
   <error-page>
   <error-code>404</error-code>
   <location>/error.jsp</location>
   </error-page>
   
   <error-page>
   <exception-type>java.lang.Throwable</exception-type>
   <location>/error.jsp</location>
   </error-page>
   
</web-app>

现在当我们运行以上应用程序时,我们会得到以下页面作为响应。登录页面 用于异常的JSP错误页面 用于404错误代码的JSP错误页面 对于JSP页面的异常处理就是这样,实现起来非常简单,我们应该使用它来确保处理所有异常和错误代码,并向客户端发送有用的响应,而不是容器默认的错误页面。

Source:
https://www.digitalocean.com/community/tutorials/jsp-exception-handling-jsp-error-page