JSP4——错误处理机制

页面中出现错误时(throw)会转到错误页,执行错误页(error.jsp)

注:实验验证时,应将几个文件放在一起添加在tomcat的webapps中。并通过localhost:8080/文件夹/文件名打开.html文件。
Compute.html:
<html>
<head>
<title>Compute</title>
</head>
<body bgcolor="#FFFFFF">
<div align="center">
  <form method="post" action="Divide.jsp">
    <p>--- 整数除法 --- 
    <p>被除数
      <input type="text" name="value1">
      除数
      <input type="text" name="value2">
    </p>
    <p>
      <input type="submit" name="Submit" value="计算">
    </p>
  </form>
</div>
</body>
</html>
-------------------------------------------------
Divide.jsp:
<%@ page 
  errorPage="error.jsp" 
  contentType="text/html;charset=gbk"
%>
<html>
<head>
<title>Divide</title>
</head>
<body bgcolor="#FFFFFF">
<center>
<br>
<h1>
<% 
  	    int dividend = 0;
   		int divisor = 0;
		int result = 0;
   		
   		try{
   			dividend = Integer.parseInt( request.getParameter( "value1" ) );
   		}
   		catch( NumberFormatException nfex ){
   			throw new NumberFormatException( "被除数不是整数! " );
   		}
   		try{
   			divisor = Integer.parseInt( request.getParameter( "value2" ) );
   		}
   		catch( NumberFormatException nfex ){
   			throw new NumberFormatException( "除数不是整数! " );
   		}	
   		try {
   		result=dividend/divisor;
		}
		catch(ArithmeticException zz) {
			throw new ArithmeticException( "被零除!!!" );
		}

   		out.println( dividend + " / " + divisor + " = " + result );
%>   
</h1><br>
  <a href="javascript: history.back();">返回</a>
</center>	
</body>
</html>
-----------------------------------------
error.jsp:
<%@ page 
  isErrorPage="true"  
  contentType="text/html;charset=gbk"
%>
<html>
<head>
<title>Compute error</title>
</head>
<body bgcolor="#FFFFFF">
<div align="center">
  <p>
  <center>
  <h3><%= exception.toString() %></h3><!--讲错误对象转成字符串输出到页面 -->
 
  <a href="javascript: history.back();">返回</a><!--返回的超链接 Javascript语句-->
  </center>
  </p>
  <h1>
      错误信息错误信息错误信息错误信息错误信息错误信息错误信息
  </h1>
  <hr>  
</div>
</body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值