当前位置: 代码迷 >> JavaScript >> jsp中errorPage与isErrorPage的设立(转)
  详细解决方案

jsp中errorPage与isErrorPage的设立(转)

热度:520   发布时间:2012-12-28 10:29:05.0
jsp中errorPage与isErrorPage的设置(转)
http://huihai.iteye.com/blog/897028

1、新建一个webjava的项目,然后在webroot下新建html页面Compute.html


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<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>


2、新建页面 divide.jsp


<%@ page language="java" errorPage="error.jsp" contentType="text/html;  charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<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("除数不是整数! ");
					} 
					result = dividend / divisor;
					out.println(dividend + " / " + divisor + " = " + result);
				%>
			</h1>
			<br>
			<br>
			<br>
			<a href="javascript: history.back();">返回</a>
		</center>
	</body>
</html>



3、新建页面error.jsp

<%@ page language="java" isErrorPage="true" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!-- isErrorPage的作用设置当前页面为错误页面,当在别的页面设置errorPage="error.jsp" 后,在设置页面出现问题后就自动会跳转到当前的错误页面 -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>Compute error</title>
	</head>
	<body bgcolor="#FFFFFF">
		<div align="center">
			<br>
			<br>
			<h1>
				错误信息
			</h1>
			<hr>
			<p>
			<h3><%=exception.toString()%></h3>
			<br>
			<br>
			<br>
			<a href="javascript: history.back();">返回</a>
		</div>
	</body>
</html>



5、然后运行computer.html页面。这时如果把除数或被除数输成小数,就会出错,而出错的信息就会自动的调用errror.jsp页面的信息



6、errorPage 的意思是设置当前页面要引入错误页面。也就是浮面当前页面如果出现错误就会跳转到errorPage所指定的页面。
   isErrorpage的意思是当前页面为错误页面。isErrorpage默认值为false,若要当前页面为错误页面就设置isErrorPage=true。