在web.xml或JSP中指定错误页面
http://gemini.javaeye.com/blog/81524
★ 在web.xml中,可以使用exception-type标签或HTTP状态码error-code标签声明错误页面。
根据异常类声明错误页面:
-
<error-page>
-
<exception-type>java.io.IOException</exception-type>
-
<location>/error.jsp</location>
- </error-page>
exception-type指定的必须是一个Throwable,并且这个类在容器的类路径下能够被找到。
根据HTTP状态码声明错误页面:
-
<error-page>
-
<error-code>404</error-code>
-
<location>/error.jsp</location>
- </error-page>
使用error-page标签的要点:
① 不论是根据exception-type还是error-code来声明错误页面,location指定的路径必须以斜线开头,也就是说它是相对于Web应用根的。
② 一个web.xml中可以使用多个error-page标签,一个error-page标签中只能使用一个exception-type或error-page。
★ 在JSP中,可以在page指令中通过errorPage属性来指定错误页面:
-
<%@pageerrorPage="error.jsp"%>
★ 容器会为错误页面提供一个exception对象,不过要想在错误页面里访问这个对象,必须在错误页面里指定isErrorPage属性:
error.jsp
-
<%@pageisErrorPage="true"%>
然后,在scriptlet中就可以使用隐式对象exception,在EL中可以使用EL隐式对象${pageContext.exception}。
★ 其它要点:
如果是JSP中抛出的异常,容器首先查看JSP中有没有指定errorPage属性,如果没有,再查看web.xml中有没有error-page标签;如果是Web应用中的其它地方抛出异常,容器只会查看web.xml中的error-page标签。
可以通过HttpServletResponse.sendError()方法来发送错误的HTTP状态码:
-
response.sendError(HttpServletResponse.SC_FORBIDDEN);
web.xml配置404,500出错页面
http://hi.baidu.com/xinggede/blog/item/feb2a733b79f0ff11b4cffdc.html
<!-- 400错误 -->
<error-page>
<error-code>400</error-code>
<location>/errorPage.jsp</location>
</error-page>
<!-- 404 页面不存在错误 -->
<error-page>
<error-code>404</error-code>
<location>/errorPage.jsp</location>
</error-page>
<!-- 500 服务器内部错误 -->
<error-page>
<error-code>500</error-code>
<location>/errorPage.jsp</location>
</error-page>
<!-- java.lang.Exception异常错误,依据这个标记可定义多个类似错误提示 -->
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/errorPage.jsp</location>
</error-page>
<!-- java.lang.NullPointerException异常错误,依据这个标记可定义多个类似错误提示 -->
<error-page>
<exception-type>java.lang.NullPointerException </exception-type>
<location>/errorPage.jsp</location>
</error-page>
<error-page>
<exception-type>javax.servlet.ServletException</exception-type>
<location>/errorPage.jsp</location>
</error-page>
分享到:
相关推荐
web.xml配置详解web.xml配置详解web.xml配置详解web.xml配置详解web.xml配置详解web.xml配置详解web.xml配置详解
JSP里web.xml实现错误处理页面的制作404,和500
请在web.xml文件中使用– page >元素为整个web应用程序设置错误处理页面。已知:处理状态码为404的页面为404.jsp,处理状态码为500的页面为500.jsp。
全面详细讲述web.xml个元素的用途 还有实例哦
web.xml配置文件详解让开发人员更加清楚web.xml是怎么配置。以及各个配置的作用。
WEB.XML标准配置注解: <!--Servlet的映射,用来说明客户端IE中输入什么样的地址字符串对应到哪个Servlet的别名--> <!--在该例中在IE地址栏中firstservlet字符串对应到别名为firstservlet的servlet--> ...
在web.xml中引入其他xml配置文件的步骤,含引入错误解决。
struts.xml和applicationContext.xml、web.xml的配置
web.xml 各属性作用描述 Web.xml 常用元素 <web‐app> ‐name>‐name>定义了WEB 应用的名字 <description></description> 声明WEB 应用的描述信息
Tomcat中用web.xml控制Web应用详解
spring-mvc.xml spring-mybatis.xml web.xml log4j.properties,项目中需要用到的配置文件。直接可用。
web.xml加载顺序与web.xml常用节点解析
送给新手,java/jsp 项目下的web.xml文件解说
servlet的写登陆管理系统用到的web.xml
jboss-web.xml jndi.properties oracle-ds.xml三个文件,EJB+JPA 所需要的一些配置文件
为tomcat服务器配置https,tomcat需要设置的server.xml与web.xml配置。可以帮助小白为tomcat服务器配置https协议(域名)。
web.xml dwr.xml 任何一个dwr.xml的文件都需要包含DWR DOCTYPE的声明行,。。 有几个术语有必要理解,参数叫做converted,远程Bean叫做created.如果远程Bean A有个方法A.blah(B),那么你需要为A建立一个created,为B建立...
web.xml 文件详解 web.xml 文件详解 web.xml 文件详解 web.xml 文件详解
JSP里web.xml实现错误处理页面的制