JSP错误页面处理的两种方式

[java]  view plain  copy
  1. JSP错误页面处理的两种方式:  
[java]  view plain  copy
  1.   
[java]  view plain  copy
  1. 方法1(真能针对单一页面生效,不推荐):  
[java]  view plain  copy
  1. 出错页面实例  
[java]  view plain  copy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <%--发生错误,想让用户看到友好页面 error.jsp--%>  
  4. <%@ page errorPage="/demo4/error.jsp" %>  
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  6. <html>  
  7. <head>  
  8. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  9. <title>Insert title here</title>  
  10. </head>  
  11. <body>  
  12. <!-- 制作错误 -->  
  13. <%  
  14.     int d = 1/0;  
  15. %>  
  16. </body>  
  17. </html>  


处理错误页面

[java]  view plain  copy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <%--  设置当前页面是错误页面, 获得错误信息 exception  --%>  
  4. <%@page isErrorPage="true" %>  
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  6. <html>  
  7. <head>  
  8. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  9. <title>Insert title here</title>  
  10. </head>  
  11. <body>  
  12. <!-- 错误友好信息页面 -->  
  13. <h4>对不起,服务器正在升级,请稍后访问!</h4>  
  14. <h5>错误原因:<%=exception.getMessage() %></h5>  
  15. </body>  
  16. </html>  

方法2:

设置web.xml全局文件

如定义404不存在页面和500服务器内部错误页面

[java]  view plain  copy
  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. <meta content="3;url=/day08/index.jsp" http-equiv="refresh">  
  8. <title>Insert title here</title>  
  9. </head>  
  10. <body>  
  11. <h1>对不起,你访问的资源不存在,网站将在3秒之后 自动跳转到主页面!</h1>  
  12. </body>  
  13. </html>  

[java]  view plain  copy
  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. <h1>对不起,服务器发生内部错误,请稍后访问!</h1>  
  11. </body>  
  12. </html>  

web.xml设置

[html]  view plain  copy
  1. <error-page>  
  2.     <error-code>500</error-code>  
  3.     <location>/demo5/500.jsp</location>  
  4.   </error-page>     
  5.   <error-page>  
  6.     <error-code>404</error-code>  
  7.     <location>/demo5/404.jsp</location>  
  8.   </error-page>  
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值