Java

本类阅读TOP10

·使用MyEclipse开发Struts框架的Hello World!(录像1)
·hibernate配置笔记
·AOP编程入门--Java篇
·linux下Tomcat 5.0.20 与 Apache 2 安装/集成/配置
·在win2003下整合了整合Tomcat5.5+ apache_2.0.53+ mod_jk_2.0.47.dll
·构建Linux下IDE环境--Eclipse篇
·Jsp 连接 mySQL、Oracle 数据库备忘(Windows平台)
·ASP、JSP、PHP 三种技术比较
·Tomcat5.5.9的安装配置
·AWT GUI 设计笔记(二)

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
jsp页面的例外处理

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

          在这篇文章中,我将向你讲解一个jsp页面例外(Exceptions)是怎样抛出(Throw)并且怎样捕捉这些例外,以便使你在jsp设计中能得到更有利的信息.
          首先,什么是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";
明白了吧!!!!
 



相关文章

相关软件