JSP学习-------------request属性范围

JSP学习记录:

十六、request属性范围
执行了服务端跳转后依然可以取得属性内容,可以采用request
-----1.jsp
<%@ page pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<% //设置属性
    request.setAttribute("name","haha");
    request.setAttribute("birthday",new Date());
%>
<jsp:forward page="2.jsp"/>  //服务器端跳转

-----2.jsp
<%@ page pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<%  //取得属性
    String sname=(String)request.getAttribute("name");
    Date sbirthday=(Date)request.getAttribute("birthday");
%>
<h1>姓名:<%=sname%></h1>
<h1>生日:<%=sbirthday%></h1>
只要是服务器端跳转,request属性就可以一直保留

重要的结论:服务器端跳转与客户端跳转最大使用区别:服务器端跳转可以传递request属性,而客户端跳转不能够传递request属性

猜你喜欢

转载自blog.csdn.net/amuist_ting/article/details/80989429