捕获jsp页面500,404错误,页面不显示错误码

本文介绍了如何在Tomcat中配置自定义错误页面,包括通过错误代码和异常类型两种方式,并解决了IE浏览器下无法正确显示的问题。
一:Tomcat Error Page配置 

如果JSP页面出现异常,就会转到tomcat自动的那个异常页面,页面不怎么友好。jsp标准中提供了error page的配置,可以自己定义当出现错误时跳转到哪个页面。这个配置在web.xml里面进行配置,下面一步步来实现自定义错误页面。 
1.首先,在apache-tomcat-5.5.20\conf\web.xml加入下面的标签: 
     <error-page> 
         <error-code>400</error-code> 
         <location>/400.html</location> 
     </error-page> 

     <error-page> 
         <error-code>404</error-code> 
         <location>/404.html</location> 
     </error-page> 

     <error-page> 
         <error-code>500</error-code> 
         <location>/error.jsp</location> 
     </error-page> 

     解释一下,error-code是错误代码,location是转向页面。如果这个配置成功,当服务器出现这个错误代码的时候,就会跳转到location这个页面。location可以是html文件,也可以是jsp页面。 
2.下面编写一下error.jsp页面的代码,例如: 
<%@page contentType="text/html;charset=Big5" isErrorPage="true"%> 
<html> 
<head><title>出现错误</title></head> 
<body> 
     <H1>错误:</H1><%=exception%> 
     <H2>错误内容:</H2> 
     <% 
         exception.printStackTrace(response.getWriter()); 
     %> 
</body> 
</html> 
因为这个页面调用了exception内置对象,所以isErrorPage必须为true。 
3.不仅可以根据html的错误代码来条转页面,也可以按异常类型来进行跳转,例如: 
<error-page> 
    <exception-type>javax.servlet.ServletException</exception-type> 
    <location>/errorhandler.jsp</location> 
</error-page> 
不仅可以使用jsp内置exception对象来取得异常,也可以取得request中的attribute。例如: 
<%@page contentType="text/html;charset=Big5" isErrorPage="true"%> 
<html> 
<head><title>错误信息</title></head> 
<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> 

二: Error Page在IE下不能转发的问题 

这是IE自身的设定导致的,经过百度,找到几个解决办法:    
1, IE设定   工具-->Internet选项-->高级--->显示http友好错误信息(取消选择) , 这样就可以了 
2, 设置指定错误页页状态为正常,来告诉IE这不是一个服务器错误, 从而不显示IE的自定义错误页 
<% 
    response.setStatus(200); // 200 = HttpServletResponse.SC_OK 
%> 
3, 把错误页做大一点,弄个几百K 就可以显示错误页面 (加一个div块,display设为none就可以了),这个问题比较奇怪.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值