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

来源:互联网 发布:2016电视盒子破解软件 编辑:程序博客网 时间:2024/05/17 23:00
[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>  
原创粉丝点击