jsp错误处理页面_JSP异常处理– JSP错误页面

jsp错误处理页面

Exception handling in JSP is done by JSP exception pages.

JSP异常页面中完成了JSP中的异常处理。

JSP中的异常处理 (Exception Handling in JSP)

Sometime back I wrote a post about Servlet Exception Handling and why do we need it. Same explanation is also applicable for JSP pages also and that’s why Java EE provides a clear approach for exception handling in JSP using JSP error pages.

有时候我写了一篇关于Servlet异常处理以及为什么我们需要它的文章。 同样的解释也适用于JSP页面,这就是Java EE为使用JSP错误页面的JSP中的异常处理提供清晰方法的原因。

To handle exceptions thrown by the JSP page, all we need is an error page and define the error page in JSP using jsp page directive.

要处理JSP页面引发的异常,我们只需要一个错误页面,并使用jsp page指令在JSP中定义错误页面。

JSP错误页面 (JSP Error Page)

To create a JSP error page, we need to set page directive attribute isErrorPage value to true, then we can access exception jsp implicit object in the JSP and use it to send customized error message to the client.

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

JSP错误页面配置 (JSP Error Page Configuration)

We need to set page directive errorPage attribute to define the JSP that will handle any exception thrown by the JSP service method. When JSP Error page is translated to servlet code, it extends org.apache.jasper.runtime.HttpJspBase in Tomcat.

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

错误页面部署描述符配置 (Error Page Deployment Descriptor Configuration)

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.

大多数情况下,我们有一个通用错误页面,我们希望将其用于所有JSP,因此,我们可以在web.xml中使用error-page元素定义错误页面,而不是在所有JSP中单独配置它。 我们可以配置JSP错误页面来处理其他错误代码,例如404。

Let’s see how all these fit together in a web application.

让我们看看所有这些如何在Web应用程序中组合在一起。

We will create a simple web application JSPExceptionHandling whose project structure will look like below image.

我们将创建一个简单的Web应用程序JSPExceptionHandling,其项目结构如下图所示。

The entry point of the application is index.jsp whose code is given below.

该应用程序的入口点是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>

When we submit the form, request will be sent to login.jsp, code is like below.

当我们提交表单时,请求将发送到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>

Notice that if input parameters are null or empty, its throwing ServletException with a proper message and errorPage is defined as error.jsp whose code is like below.

请注意,如果输入参数为null或为空,则抛出带有适当消息和errorPage ServletException定义为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>

Notice the isErrorPage page directive attribute value is true. When application resources throw exceptions, the error code is 500, the code is written to handle both application level exceptions and errors such as 404 – page not found.

注意isErrorPage页面指​​令的属性值为true 。 当应用程序资源引发异常时,错误代码为500,编写该代码以处理应用程序级异常和错误,例如404 –未找到页面。

Also notice the use of include directive to present user with login page incase of any exception.

还要注意,在发生任何异常的情况下,使用include指令向用户显示登录页面。

Here is the web.xml where we are defining the error page for the application.

这是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>

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错误页面的异常

JSP Error Page for 404 Error Code

JSP Error Code 404 Page, exception handling in JSP

404错误代码的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.

多数民众赞成在JSP页面中进行异常处理,它非常易于实现,我们应该使用它来确保我们处理所有异常和错误代码,并将有用的响应发送给客户端,而不是发送容器默认错误页面。

翻译自: https://www.journaldev.com/2049/jsp-exception-handling-jsp-error-page

jsp错误处理页面

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值