• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

jsp页面的例外处理

发布: 2007-7-04 13:34 | 作者: admin | 来源:  网友评论 | 查看: 12次 | 进入软件测试论坛讨论

领测软件测试网
首先,什么是Exceptions?众所周知Exceptions就是一个异常事件,它可能出现在程序的任何地方,比如:你试图连接一个数据库,但是这个数据库已经关闭,这时就产生一个例外.
如何捕捉(throw)一个例外啦?我们可以用下面的表达式:
<%
try {
// Code which can throw can exception
} catch(Exception e) {
// Exception handler code here
}
%>
当然,还有另外的一种有用的方法:就是指定专用的例外处理页面,当例外发生时便由它来处理.这就是我下面要讲述的.
建立三个页面:1.Form.html(简单的年龄输入筐)代码如下:


<html>
<head>
<style>
body, input { font-family:Tahoma; font-size:8pt; }
</style>
</head>
<body>

<!-- HTML Form -->
<form action="FormHandler.jsp" method="post">
Enter your age ( in years ) :
<input type="text" name="age" />
<input type="submit" value="Submit" />
</form>

</body>
</html>2:FormHandler.jsp()(处理由Form.html传来的age)代码如下:
<%@ page errorPage="ExceptionHandler.jsp" %>
<html>
<head>
<style>
body, p { font-family:Tahoma; font-size:10pt; }
</style>
</head>
<body>

<%-- Form Handler Code --%>
<%
int age;

age = Integer.parseInt(request.getParameter("age"));
%>

<%-- Displaying User Age --%>
<p>Your age is : <%= age %> years.</p>

<p><a href="Form.html">Back</a>.</p>

</body>
</html>
请注意:(1)<%@ page errorPage="ExceptionHandler.jsp" %>是指明了一个例外处理页面,它必须在jsp的第一行.(2)

<%
int age;

age = Integer.parseInt(request.getParameter("age"));
%>是取得age(String类)并转化为int(类).<p>Your age is : <%= age %> years.</p>是输出你刚才输入的age,现在例外就可能发生了,如果 你输入的不是数字,比如:zsa;这时String能转化成int吗?3.ExceptionHandler.jsp(处理例外)代码如下:<%@ page isErrorPage="true" import="java.io.*" %>
<html>
<head>
<title>Exceptional Even Occurred!</title>
<style>
body, p { font-family:Tahoma; font-size:10pt; padding-left:30; }
pre { font-size:8pt; }
</style>
</head>
<body>

<%-- Exception Handler --%>
<font color="red">
<%= exception.toString() %><br>
</font>

<%
out.println("<!--");
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
exception.printStackTrace(pw);
out.print(sw);
sw.close();
pw.close();
out.println("-->");
%>

</body>
</html>注意:<%@ page isErrorPage="true" %>表明:当jsp宣称了一个errorPage时,应该声明isErrorPage="true;
<%
out.println("<!--");
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
exception.printStackTrace(pw);
out.print(sw);
sw.close();
pw.close();
out.println("-->");
%>运用了PrintWriter和StringWrighter类,所以你不得不声明:import java.io.* 在你jsp程序中;即:<%@ page isErrorPage="true" import="java.io.*" %>好了:开始演示:在ie中输入http://localhost:8080/myapp/Form.html 回车!当然你先要启动tomcat! 看见了吗?在输入筐中入任何一个数字:24等:结果是:Your age is : 24 years再试一下:输入:zsa.是什么结果啦??java.lang.NumberFormatException: For input string: "zsa";明白了吧!!!!

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网