项目名称:中国电信运营支持系统-网络版(九)
web项目的异常处理:
案例演示:
工程案例目录结构
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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_2_5.xsd" version="2.5">
<display-name>netctoss</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>main</servlet-name>
<servlet-class>web.MainServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>main</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 每页显示行数 -->
<context-param>
<param-name>size</param-name>
<param-value>5</param-value>
</context-param>
<!--
错误页面:当服务器捕获到此类异常时,
它会自动转发到对应的错误页面。
服务器在做此转发行为时,会自动补充项目名,
因此转发页面的绝对路径不需要加项目名了。
-->
<!-- 1.指定某类型的异常对应某错误页面 -->
<!-- <error-page>
<exception-type>
java.lang.Exception
</exception-type>
<location>/WEB-INF/error.jsp</location>
</error-page> -->
<!-- 2.指定某编号的错误对应某错误页面 -->
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/error.jsp</location>
</error-page>
<error-page>
<error-code>405</error-code>
<location>/WEB-INF/error.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/WEB-INF/error.jsp</location>
</error-page>
</web-app>
error.jsp
<%@page pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>案例-NetCTOSS</title>
<link type="text/css" rel="stylesheet" media="all" href="styles/global.css" />
<link type="text/css" rel="stylesheet" media="all" href="styles/global_color.css" />
<script language="javascript" type="text/javascript">
var timer;
//启用跳转的定时器
function startTimes(){
timer = window.setInterval(showSecondes,1000);
}
var i =5;
function showSecondes(){
if(i>0){
i--;
document.getElementById("secondes").innerHTML = i;
}else{
window.clearInterval(timer);
location.href = "/netctoss/toLogin.do";
}
}
//取消跳转
function resetTimer(){
if(timer !=null&& timer !=undefined){
window.clearInterval(timer);
//location.href="login.html";
history.back();
}
}
</script>
</head>
<body class="error_page" onload="startTimes();">
<h1 id="error">
遇到错误, <span id="secondes">5</span> 秒后将自动跳转,立即跳转请点击
<a href="javascript:resetTimer();">返回</a>
</h1>
</body>
</html>
将netctoss工程部署到Tomcat上,运行Tomcat启动案例工程,
浏览器录入http://localhost:8080/netctoss/findCost2.do 【错误的请求路径】即可:如果没有错误,最终页面显示效果应如下图: