web.xml 配置404和500错误的自定义页面
2015-01-16 15:15
501 查看
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<error-page>
<error-code>404</error-code>
<location>/building.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>
</web-app>
JSP页面的关键在于
isErrorPage="true"
response.setStatus(HttpServletResponse.SC_OK);
building.jsp:
<%@ page language="java" contentType="text/html; charset=GBK" isErrorPage="true" pageEncoding="GBK"%>
<%response.setStatus(HttpServletResponse.SC_OK);%>
<%
/**
* 本页面是在客户查找的页面无法找到的情况下调用的
*/
response.setStatus(HttpServletResponse.SC_OK);
%>
<body>
正在制作 <a href="javascript:history.go(-1)">返回</a>
<br/>
也可能页面连接更改了,请按 F5 键刷新整个页面看看,特别是菜单!
</body>
error.jsp
<%@ page language="java" contentType="text/html; charset=GBK" isErrorPage="true" pageEncoding="GBK"%>
<%@ page import="java.io.*,java.util.*"%>
<%response.setStatus(HttpServletResponse.SC_OK);%>
<body>
程序发生了错误,有可能该页面正在调试或者是设计上的缺陷.<br/>
你可以选择<br/> <a href=<%=request.getContextPath()+"/forum/new.jsp" %>>反馈</a>
提醒我 或者<br/><a href="javascript:history.go(-1)">返回上一页</a>
<hr width=80%>
<h2><font color=#DB1260>JSP Error Page</font></h2>
<p>An exception was thrown: <b> <%=exception.getClass()%>:<%=exception.getMessage()%></b></p>
<%
Enumeration<String> e = request.getHeaderNames();
String key;
while(e.hasMoreElements()){
key = e.nextElement();
}
e = request.getAttributeNames();
while(e.hasMoreElements()){
key = e.nextElement();
}
e = request.getParameterNames();
while(e.hasMoreElements()){
key = e.nextElement();
}
%>
<%=request.getAttribute("javax.servlet.forward.request_uri") %><br>
<%=request.getAttribute("javax.servlet.forward.servlet_path") %>
<p>With the following stack trace:</p>
<pre>
<%
exception.printStackTrace();
ByteArrayOutputStream ostr = new ByteArrayOutputStream();
exception.printStackTrace(new PrintStream(ostr));
out.print(ostr);
%>
</pre>
<hr width=80%>
</body>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<error-page>
<error-code>404</error-code>
<location>/building.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>
</web-app>
JSP页面的关键在于
isErrorPage="true"
response.setStatus(HttpServletResponse.SC_OK);
building.jsp:
<%@ page language="java" contentType="text/html; charset=GBK" isErrorPage="true" pageEncoding="GBK"%>
<%response.setStatus(HttpServletResponse.SC_OK);%>
<%
/**
* 本页面是在客户查找的页面无法找到的情况下调用的
*/
response.setStatus(HttpServletResponse.SC_OK);
%>
<body>
正在制作 <a href="javascript:history.go(-1)">返回</a>
<br/>
也可能页面连接更改了,请按 F5 键刷新整个页面看看,特别是菜单!
</body>
error.jsp
<%@ page language="java" contentType="text/html; charset=GBK" isErrorPage="true" pageEncoding="GBK"%>
<%@ page import="java.io.*,java.util.*"%>
<%response.setStatus(HttpServletResponse.SC_OK);%>
<body>
程序发生了错误,有可能该页面正在调试或者是设计上的缺陷.<br/>
你可以选择<br/> <a href=<%=request.getContextPath()+"/forum/new.jsp" %>>反馈</a>
提醒我 或者<br/><a href="javascript:history.go(-1)">返回上一页</a>
<hr width=80%>
<h2><font color=#DB1260>JSP Error Page</font></h2>
<p>An exception was thrown: <b> <%=exception.getClass()%>:<%=exception.getMessage()%></b></p>
<%
Enumeration<String> e = request.getHeaderNames();
String key;
while(e.hasMoreElements()){
key = e.nextElement();
}
e = request.getAttributeNames();
while(e.hasMoreElements()){
key = e.nextElement();
}
e = request.getParameterNames();
while(e.hasMoreElements()){
key = e.nextElement();
}
%>
<%=request.getAttribute("javax.servlet.forward.request_uri") %><br>
<%=request.getAttribute("javax.servlet.forward.servlet_path") %>
<p>With the following stack trace:</p>
<pre>
<%
exception.printStackTrace();
ByteArrayOutputStream ostr = new ByteArrayOutputStream();
exception.printStackTrace(new PrintStream(ostr));
out.print(ostr);
%>
</pre>
<hr width=80%>
</body>
相关文章推荐
- web.xml 配置404和500错误的自定义页面
- web.xml 配置404和500错误的自定义页面
- web.xml 配置404和500错误的自定义页面
- web.xml 配置404和500错误的自定义页面
- web.xml 配置404和500错误的自定义页面
- web.xml 配置404和500错误的自定义页面
- 在ssh框架下web.xml配置404和500错误代码,自定义不生效的解决办法。
- 404、500页面配置,web.xml配置404、500错误
- tomcat 可以在web.xml当中配置404 自定义错误页面
- web.xml中url-pattern配置了/ 静态页面404
- web.xml配置SpringMVC时导致访问的页面资源不存在,跳转页面时出现404
- web.xml配置如果出现404跳转的页面
- spring mvc中 一个完整的 web.xml配置,包括自定义友好提示页面
- 在web.xml配置404和500错误及异常
- web.xml配置404,500出错页面
- java web在web.xml中配置404,400,500页面
- web.xml中url-pattern配置了/ 静态页面404
- 关于web.xml中配置404错误跳转页面 在ie不能显示自定义错误页面的问题
- web.xml中url-pattern配置了/ 静态页面404
- 关于web.xml中配置404错误跳转页面 在ie不能显示自定义错误页面的问题