看如下两个JSP的内容就能看到两者的联系。用request.setAttribute()方法赋值后,用request.getAttribute()方法可以取得,在页面上也能取得。
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*" %>
<html>
<head><title></title></head>
<body>
<%
try{
int a=10;
int b=0;
request.setAttribute("t1",a/b);
}catch(Exception e){
request.setAttribute("t1","信息错误");
}
%>
<jsp:forward page="page.jsp"/>
</body>
</html>
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*" %>
<html>
<head><title></title></head>
<body>
<%
String message= request.getAttribute("t1").toString();
%>
<%=message %>
</body>
</html>
getAttribute() 返回值为object型,需要调用其toString()方法,将object类型转为字符串类型。