JSP 异常处理 - JSP 错误页面

JSP 中的例外处理由 JSP 例外页进行。

JSP 中的例外处理

有一段时间前,我写了一篇关于 [Servlet Exception Handling](/community/tutorials/servlet-exception-and-error-handling-example-tutorial Servlet Exception and Error Handling Example Tutorial)的文章,以及为什么我们需要它。 同样的解释也适用于JSP页面,这就是为什么Java EE提供了使用JSP错误页面对JSP的例外处理的清晰方法。 为了处理JSP页面所抛出的例外,我们所需要的只是一个错误页面,并使用JSP中的错误页面定义(/community/tutorials/jsp-directives-page-include-tag-lib-example `JSP指南 - 页面,包括标签和标签例)。

JSP 错误页面

要创建 JSP 错误页面,我们需要将页面指令属性 isErrorPage 值设置为 true,然后我们可以在 JSP 中访问例外 [jsp 暗示对象](/community/tutorials/jsp-implicit-objects JSP 暗示对象)并使用它发送自定义错误消息给客户端。

JSP错误页面配置

我们需要设置页面指令 **errorPage ** 属性来定义 JSP 将处理 JSP 服务方法所投放的任何例外。

错误页面部署描述器配置

Most of the times, we have a common error page that we want to use for all the JSPs, so rather than configuring it in all the JSPs individually, we can define error page in web.xml with error-page element. We can configure JSP error page to handle other error codes like 404 also. Let's see how all these fit together in a web application. We will create a simple web application JSPExceptionHandling whose project structure will look like below image. JSP Exception Handling, JSP error page, exception handling in jsp The entry point of the application is index.jsp whose code is given below.

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

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

 1<%@ page language="java" contentType="text/html; charset=US-ASCII"
 2    pageEncoding="US-ASCII" errorPage="error.jsp"%>
 3<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
 4<html>
 5<head>
 6<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
 7<title>User Home Page</title>
 8</head>
 9<body>
10<%
11    String user = request.getParameter("id");
12    String pwd = request.getParameter("password");
13    
14    if(user == null || "".equals(user) || pwd == null || "".equals(pwd)){
15    	throw new ServletException("Mandatory Parameter missing");
16    }
17    
18%>
19
20<%-- do some DB processing, not doing anything for simplicity --%>
21Hi <%=user %>
22</body>
23</html>

请注意,如果输入参数为 null 或空,则将ServletException 与正确的消息和errorPage 丢弃,定义为error.jsp,其代码如下。

 1<%@ page language="java" contentType="text/html; charset=US-ASCII"
 2    pageEncoding="US-ASCII" isErrorPage="true"%>
 3<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
 4<html>
 5<head>
 6<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
 7<title>Error Page</title>
 8</head>
 9<body>
10<% if(response.getStatus() == 500){ %>
11<font color="red">Error: <%=exception.getMessage() %></font><br>
12
13<%-- include login page --%>
14<%@ include file="index.jsp"%>
15<%}else {%>
16Hi There, error code is <%=response.getStatus() %><br>
17Please go to <a href="/index.jsp">home page</a>
18<%} %>
19</body>
20</html>

请注意,isErrorPage页面指令属性值为true。当应用资源抛出例外时,错误代码为500,代码被写成处理应用级别的例外和错误,如404 - 页面未找到。

 1<?xml version="1.0" encoding="UTF-8"?>
 2<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">
 3  <display-name>JSPExceptionHandling</display-name>
 4  <welcome-file-list>
 5    <welcome-file>index.jsp</welcome-file>
 6   </welcome-file-list>
 7
 8   <error-page>
 9   <error-code>404</error-code>
10   <location>/error.jsp</location>
11   </error-page>
12
13   <error-page>
14   <exception-type>java.lang.Throwable</exception-type>
15   <location>/error.jsp</location>
16   </error-page>
17
18</web-app>

Now when we run above application, we get following pages as response. Login Page JSP exception handling example Login Page JSP Error Page for Exception JSP Error Page JSP Error Page for 404 Error Code JSP Error Code 404 Page, exception handling in JSP Thats all for exception handling in JSP pages, its very easy to implement and we should use it to make sure we handle all the exceptions and error codes and send a useful response to client rather than container default error page.

Published At
Categories with 技术
Tagged with
comments powered by Disqus