使用errorPage属性指定错误页面

avatar 2017年07月10日12:38:08 1 3036 views
博主分享免费Java教学视频,B站账号:Java刘哥
errorPage 指定一个错误页面,如果该 JSP 程序抛出一个未捕捉的异常,则转到 errorPage 指定的页面。errorPage指定的 isErrorPage 属性为 true,且内置的 exception 对象为未捕捉的异常。 Demo如下 (1)新建 page.jsp 新建 web 项目为 JspDemo , 在 webContent 下新建 page.jsp 代码如下
  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  3. <html>
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  6. <title>Insert title here</title>
  7. </head>
  8. <body>
  9.     <%
  10.         int a = 1/0;
  11.     %>
  12. </body>
  13. </html>
(2)启动 Tomcat ,在浏览器中输入 http://localhost:8080/JspDemo/page.jsp 上面的异常看起来真是不爽,由于异常是没有捕获的,所以 errorPage 需要派上用场 (3)新建 error.jsp 同样,在 page.jsp 同级目录新建 error.jsp ,代码如下
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2.     pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10.     <h2>抱歉,服务器出现故障,正在解决,请稍后。。。。。。</h2>
  11. </body>
  12. </html>
(4)修改 page.jsp 即在 page.jsp 头部加上 errorPage="error.jsp" ,代码如下
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2.     pageEncoding="UTF-8" errorPage="error.jsp"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10.     <%
  11.         int a = 1/0;
  12.     %>
  13. </body>
  14. </html>
(5)刷新浏览器 或者在地址栏输入:http://localhost:8080/JspDemo/page.jsp 这样的错误提示明显更加友好   本文链接:https://liuyanzhao.com/5119.html
  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0