【jsp exception】如何处理jsp页面的错误
根据jsp对错误的处理方式不同可以将其分为局部异常处理和全局异常处理。局部异常处理适用于个别jsp页面,当这些页面发生错误后,采取特殊的处理方式;全局异常处理适用于所有jsp页面,当所有页面发生某些指定错误后,采取同一方式处理。
(1)局部异常处理
局部异常处理主要涉及jsp页面page指令的 errorPage 和 isErrorPage 属性
errorPage 属性用于设置错误处理的jsp页面,如果当前jsp页面内产生了未被捕获的异常,则跳转到errorPage指定的jsp页面进行处理。
isErrorPage 属性用于错误处理页面,只有将jsp页面中的 isErrorPage 属性设置为true,此jsp页面才能被用作错误处理页面,在该jsp的代码中才可以使用exception饮食对象(!不设isErrorPage 会报错!)
错误处理页面dealError.jsp
<%@ page contentType="text/html;charset=gb2312" import="java.util.*" %>
<%@ page import="java.io.PrintWriter"%>
<%@ page isErrorPage="true" %>
<html>
<body>
<%
out.println("out of deal error!\r\n");
exception.printStackTrace(new PrintWriter(out));
%>
</body>
</html>
业务处理页面divide.jsp
<%@ page contentType="text/html;charset=gb2312" import="java.util.*" %>
<%@ page errorPage="dealError.jsp" %>
<html>
<body>
<%
out.println("before exception!");
int x= 1/0;
out.println("after execption!");
%>
</body>
</html>
运行结果:
out of deal error!
java.lang.ArithmeticException: / by zero at org.apache.jsp.divide_jsp._jspService(divide_jsp.java:71) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:727) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) at javax.servlet.http.HttpServlet.service(HttpServlet.java:727) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) at
...
...
(2) 全局异常处理
通过在web.xml 文件中添加<error-page>配置,可以为整个web应用程序设置异常处理页面。每个<error-page>元素用于设置一种异常或者一个HTTP错误状态码的处理页面。
web.xml 中的示例代码如下:
<!-- 依据HTTP错误状态码设置错误处理页面 -->
<error-page>
<error-code>404</error-code>
<location>/errorhandler.jsp</location>
</error-page>
<!-- 依据异常类型设置错误处理页面 -->
<error-page>
<exception-type>javax.servlet.ServletException</exception-type>
<location>/errorhandler.jsp</location>
</error-page>
在上述代码中,<error-page>元素中的子元素<exception-type> 和<error-code>,两者是二选一的关系
错误处理页面errorhandler.jsp
<%@ page contentType="text/html;charset=gb2312" import="java.util.*" %>
<%@ page isErrorPage="true" %>
<html>
<body>
错误码: <%=request.getAttribute("javax.servlet.error.status_code") %><br/>
讯息: <%=request.getAttribute("javax.servlet.error,message") %> <br/>
例外: <%=request.getAttribute("javax.servlet.error.exception_type") %><br/>
</body>
</html>
运行结果(如servlet 中某一句前加一个~ 使其编译失败):
错误码: 500
讯息: null
例外: class java.lang.Error
【jsp exception】如何处理jsp页面的错误的更多相关文章
- jsp登录页面,展示错误信息,刷新页面后错误依然存在解决方案
在做登录页面的时候,通常使用form表单同步提交的方法进行提交的,也就是在form表单里去写action,如果登录失败,jsp通过jstl表达式获取错误信息展示在页面上,但是有一个问题就是,即使你刷新 ...
- JSP的几个页面指令
页面指令:向服务器说明页面自身的特征,以便服务器. 1,<%@page contentType="text/xml;charset=utf-8" %> 客户端---&g ...
- JSP隐式对象是JSP容器为每个页面提供的Java对象
JSP 隐式对象 JSP隐式对象是JSP容器为每个页面提供的Java对象,开发者可以直接使用它们而不用显式声明.JSP隐式对象也被称为预定义变量. JSP所支持的九大隐式对象: 对象 描述 reque ...
- 报错:严重: Servlet.service() for servlet [jsp] in context with path [/20161116-Struts2-6] threw exception [/index.jsp (line: 13, column: 20) No tag "textfiled" defined in tag library imported with prefix
严重: Servlet.service() for servlet [jsp] in context with path [/20161116-Struts2-6] threw exception [ ...
- JSP动作跳转页面的时候与根目录的问题
在JSP动作:<jsp:forward page="....">中,这个page属性所指定的页面要包含根目录的话,必须要用"/",不能够用" ...
- Jsp与servlet之间页面跳转及参数传递实例(转)
原网址:http://blog.csdn.net/ssy_shandong/article/details/9328985 11. jsp与servlet之间页面跳转及参数传递实例 分类: Java ...
- Web jsp开发学习——实现页面跳转和传参
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletExcepti ...
- JSP生成静态Html页面
[转载]JSP生成静态Html页面 在网站项目中,为了访问速度加快,为了方便百度爬虫抓取网页的内容,需要把jsp的动态页面转为html静态页面.通常有2种常用的方式: 1.伪静态,使用URL Rewr ...
- 027 SSM综合练习03--数据后台管理系统--product-list.jsp和main.jsp页面制作
1.product-list.jsp页面制作 (1)创建一个product-list1.jsp文件,清空,只保留 <%@ page contentType="text/html;cha ...
随机推荐
- HDU 1969 Pie(二分查找)
Problem Description My birthday is coming up and traditionally I'm serving pie. Not just one pie, no ...
- Babel 相关资料
Babel online editor Babel Plugin Handbook babeljs usage options
- IdentityDbContext类源码
Microsoft.AspNet.Identity.EntityFramework/IdentityDbContext.cs 源码: 其中涉及到用户信息表.用户角色表的相关操作. using Syst ...
- table边框1px
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Chapter 14_4 使用_ENV
因为_ENV是一个普通的变量,我们可以像其他变量一样去对它进行赋值和访问. _ENV = nil 上面的赋值操作,将会使得在它之后的代码块不能直接访问全局变量.不过,对控制你的代码所使用的变量有用处. ...
- poj1256(全排列stl)
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;bool cmp ...
- SpringMVc上传excel或csv文件
1.JSP页面代码 <form enctype=""multipart/form-data" method="post"> <inp ...
- 让人头疼的CSS兼容
先说点Hack的知识(真正的高手是不用Hack的,但要成为高手必须通过Hack这一关) /* CSS属性级Hack */ color:red; /* 所有浏览器可识别*/ _color:red; /* ...
- css font简写规则
是不是在很很多网站的公共样式中会看到这样的代码?font: 12px/150% Arial, Verdana, "\5b8b\4f53";意思为:字体大小/行高 字体族 " ...
- JMeter基础
转载自虫师-http://www.cnblogs.com/fnng/archive/2012/12/21/2828440.html JMeter 介绍: 一个非常优秀的开源的性能测试工具. 优点:你用 ...