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 Error Page for Exception

JSP错误页面的异常

JSP Error Page for 404 Error Code

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错误处理页面

jsp错误处理页面_JSP异常处理– JSP错误页面相关推荐

  1. ThinkPHP6项目基操(13.实战部分 项目中的自定义异常处理总结 错误页面API错误)

    项目中的自定义异常处理总结 错误页面&API错误 前言 一.异常分类 1. 控制器找不到 2. 方法找不到 3. 请求资源不存在 4. 系统內部异常.HTTP异常等 二.异常处理 1. 前置处 ...

  2. SpringBoot 错误页面和异常处理

    一,错误页面 如图所有,这两种错误提示在springboot开发的应有程序中都很常见,第一种是html请求,第二种是json格式的请求. 在SpringBoot中,默认错误请求控制器是BasicErr ...

  3. jsp跳转到本身页面_jsp页面中的js实现跳转

    展开全部 Jsp 页面跳转的几种方法 1. RequestDispatcher.forward() 在服务器32313133353236313431303231363533e4b893e5b19e31 ...

  4. jsp隐式对象_JSP隐式对象

    jsp隐式对象 JSP Implicit Objects are very useful for developers. This is the second post in the series o ...

  5. 动态页面技术(JSP/EL/JSTL)

    1 JSP技术 1.1 jsp脚本和注释 jsp脚本:   1) <%java代码%> ----- 内部的java代码翻译到service方法的内部   2) <%=java变量或表 ...

  6. jsp java el表达式_jsp相关笔记,el表达式、jsp标签库(jstl)

    一.jsp基础部分 1.介绍 概念 JSP(Java Server Page),java服务器端页面,可以定义html标签,又可以定义java代码. 原理 JSP本质上就是一个Servlet 2.JS ...

  7. 网站开发进阶(十一)如何将一个jsp页面嵌套在另一个页面中

    如何将一个jsp页面嵌套在另一个页面中 这个在做网页中常要用到,有些通用的内容可集中放在一个页面文件中,其它要用到这些内容的页面只需要包含(引用)这个通用文件即可.这样便于维护,如果有很多网页,当通用 ...

  8. JavaWeb——动态页面技术(JSP/EL/JSTL)

    静态页面与动态页面: 1.动态网页,是指跟静态网页相对的一种网页编程技术.静态网页,随着html代码的生成,页面的内容和显示效果就基本上不会发生变化了--除非你修改页面代码.而动态网页则不然,页面代码 ...

  9. 在JSP中定义一个全局变量,供所有页面引用

    在java中设定全局变量是非常容易的,但是在jsp中如果想在一个页面定义一个变量供所有其他的jsp来引用,就不一样了,搞了几个小时吧,终于实现了,用到了以前很少用的include标签. 首先定义两个j ...

最新文章

  1. java 打印当月日历_Java打印日历表
  2. python适合零基础学习吗-零基础能学好Python吗?哪些人更适合学习?
  3. Ventoy 制作可启动 U 盘的开源工具
  4. LiveVideoStack公众号内容改版通知
  5. python多线程队列处理_Python线程和队列使用的一点思考
  6. 利用 Docker 搭建单机的 Cloudera CDH 以及使用实践
  7. 程序员日记我们需要有条理的生活
  8. 【报告分享】字节跳动2019年企业社会责任报告.pdf(附下载链接)
  9. AI电子宠物Vector,会跑会动会聊天,一天内火遍美国科技圈
  10. 由echarts想到的js中的时间类型
  11. Hadoop 文本分类 终于跑通了
  12. hdu 1540 Tunnel Warfare (线段树维护左右最长连续区间)
  13. javaSocket编程UDP
  14. HIVE语法基础及实战----干货
  15. mysql 检索结果排序方式_MySQL--排序检索数据(ORDER BY)
  16. Android短信开发 发送彩信 ‘ 高通源码 ‘ (彩信发送过程1)
  17. OO系统分析员之路--用例分析系列(4)--业务建模一般步骤和方法[整理重发]
  18. 《程序员的第一年》---------- 周未回想
  19. 【老板须知】实施ERP系统能为公司带来什么样的好处
  20. Java的基本学习(五)——高级UI设计与异常处理

热门文章

  1. 如何在linux centos下安装git(转)
  2. sql 存储过程分页
  3. [转载] python支持complex吗_Python 内置函数complex详解
  4. [转载] Python将列表转换成字符串及字符串左右中对齐输出问题
  5. [转载] java中对象作为参数传递给一个方法,到底是值传递,还是引用传递
  6. ffmpeg下载rtmp flv
  7. Python 模块安装失败
  8. DrawerLayout和NavigationView的简单实用
  9. Git正确的协作方式(很简单)
  10. Flume-NG源码阅读之Interceptor(原创)