标签:
web.xml中配置error-page标签
1、WEB工程中打开 web.xml 文件
修改web.xml文件
404错误时显示/error/404error.jsp页面,500错误时显示/error/500error.jsp页面
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 3 <display-name>web01</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list> 12 13 <error-page> 14 <error-code>404</error-code> 15 <location>/error/404error.jsp</location> 16 </error-page> 17 <error-page> 18 <error-code>500</error-code> 19 <location>/error/500error.jsp</location> 20 </error-page> 21 22 <servlet> 23 <servlet-name>empController</servlet-name> 24 <servlet-class>com.test.controller.EmpController</servlet-class> 25 </servlet> 26 27 <servlet-mapping> 28 <servlet-name>empController</servlet-name> 29 <url-pattern>/empController</url-pattern> 30 </servlet-mapping> 31 32 </web-app>
2、建立/error/404error.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 3 <html> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 6 <title>404错误</title> 7 </head> 8 <body> 9 <h1>访问路径错误或已删除此页面</h1> 10 <% 11 12 response.setStatus(HttpServletResponse.SC_OK); 13 14 %> 15 </body> 16 </html>
3、建立/error/500error.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 3 <html> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 6 <title>500错误</title> 7 </head> 8 <body> 9 <h1>此页面在维护中</h1> 10 <% 11 12 response.setStatus(HttpServletResponse.SC_OK); 13 14 %> 15 </body> 16 </html>
4、测试404错误,访问没有资源的路径,如:http://localhost:8081/web01/test/aa
5、测试500错误,访问代码有问题的页面,如:在jsp页面中没有导入Date对象的情况下使用了Date d = new Date();语句
1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 2 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 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>web01</title> 8 9 <style type="text/css"> 10 body 11 { 12 margin-left: 100px; 13 } 14 table, th, td 15 { 16 border: 1px solid black; 17 } 18 </style> 19 <% 20 Date d = new Date(); 21 %> 22 </head> 23 <body> 24 <form action="/web01/empController" method="get"> 25 员工姓名:<input type="text" name="searchTxt"> 26 <input type="submit" value="Search"> 27 <table> 28 <tr> 29 <th>员工号</th> 30 <th>员工姓名</th> 31 <th>职位</th> 32 <th>领导号</th> 33 <th>雇佣日期</th> 34 <th>工资</th> 35 <th>奖金</th> 36 <th>部门编号</th> 37 </tr> 38 <c:forEach items="${requestScope.empBean}" var="emp"> 39 <tr> 40 <td>${emp.empNo }</td> 41 <td>${emp.ename }</td> 42 <td>${emp.job }</td> 43 <td>${emp.mgr }</td> 44 <td>${emp.hiredate }</td> 45 <td>${emp.sal }</td> 46 <td>${emp.comm }</td> 47 <td>${emp.deptno }</td> 48 </tr> 49 </c:forEach> 50 </table> 51 </form> 52 </body> 53 </html>
标签:
原文地址:http://www.cnblogs.com/seabird1979/p/4809201.html