Web开发基础_Servlet学习_0026_项目练习(九)

项目名称:中国电信运营支持系统-网络版(九)


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">
			遇到错误,&nbsp;<span id="secondes">5</span>&nbsp;秒后将自动跳转,立即跳转请点击&nbsp;
			<a href="javascript:resetTimer();">返回</a>
		
		</h1>
	</body>
</html>

将netctoss工程部署到Tomcat上,运行Tomcat启动案例工程,

浏览器录入http://localhost:8080/netctoss/findCost2.do 【错误的请求路径】即可:如果没有错误,最终页面显示效果应如下图:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Coder_Boy_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值