request对象可以视为一个域,可以应用setAttribute()方法在域范围内存放数据。将数据保存在request范围内的变量中后,可以通过request对象的getAttribute()方法获取该变量值。
index.jsp:
<%@ page language="java" 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>Insert title here</title>
</head>
<body>
<%
try{
int money=100;
int number=0;
request.setAttribute("result",money/number);
}
catch(Exception e){
request.setAttribute("result","很抱歉,页面产生错误!");
}
%>
<jsp:forward page="deal.jsp"/>
</body>
</html>
deal.jsp:
<%@ page language="java" 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=ISO-8859-1">
<title>结果页</title>
</head>
<body>
<% String message=request.getAttribute("result").toString();%>
<%=message %>
</body>
</html>