JAVA-JSP内置对象之session范围

 

相关资料:
《21天学通Java Web开发》

session范围
1.就是指客户浏览器与服务器一次会话范围内,如果和服务器断开连接,那么这个属性也就失效了。
2.通过使用session的setAttribute()方法来设置属性,并通过session的getAttribute()方法来取得属性。
3.如果同时打开多个浏览器,那结果可能不同。

 

SessionScopeDemo.jsp

 1 <%@ page language="java" contentType="text/html;charset=gb2312" %>
 2 <html>
 3 <head>
 4   <title>session范围</title>
 5 </head>
 6 <body>
 7   <%-- 在session范围设置属性 --%>
 8   <%
 9     session.setAttribute("name","James");//设置属性name,其值为James  
10   %>
11   <%-- 添加页面链接,用来跳转到SessionScopeDemo2.jsp --%>
12   <a href="SessionScopeDemo2.jsp">SessionScopeDemo2.jsp</a>
13 </body>
14 </html>
View Code

 

SessionScopeDemo2.jsp

 1 <%@ page language="java" contentType="text/html;charset=gb2312" %>
 2 <html>
 3 <head>
 4   <title>session范围</title>
 5 </head>
 6 <body>
 7   <%-- 取得session范围属性 --%>
 8   <%
 9     String strName=(String)session.getAttribute("name");//取得属性name的值
10     out.println("session范围:name属性值为"+strName);//输出属性name的值 
11   %>
12 </body>
13 </html>
View Code
原文地址:https://www.cnblogs.com/FKdelphi/p/7665057.html