您的位置:首页 > Web前端 > JavaScript

JSP九大内置对象详解全析(三):session对象

2018-01-17 16:29 633 查看

1、session对象概述

session对象是由服务器自动创建与用户请求相关的对象。服务器会为每一个用户创建一个session对象用来保存用户信息,跟踪用户操作。该对象内部使用Map类来保存数据,因此它的数据类型是key-value形式。对应javax.servlet.http.HttpSession.class对象。

服务器为不同的浏览器在内存中创建用于保存数据的对象叫seesion。并将这个对象的唯一身份认证SessionId(存放在cookie中)返回给浏览器,浏览器将这个SessionId保存,当浏览器向服务器再次发送请求的时候,连同这个cookie一起发送过去,由此找到对应的session。



2、获取session

通过request对象可以获得:

HttpSession s = request.getSession(boolean flag);
HttpSession s = request.getSession( );


当flag为true时,会在在浏览器发送过来的请求中查看是否有SID,有,则返回与SID对应的session对象,没有,创建一个新的。当flag为false,没有就返回null。第二种方法相当于true。

3、session添加、获取、移除数据

Session作为服务器端为各客户端保存交互数据的一种方式,采用key-value对的形式来区分每一组数据。

向session添加数据
void session.setAttribute(String name,Object obj);

获取绑定数据
void session.getAttribute(String name);

移除绑定数据
void session.removeAttribute(String name);

删除session对象
void  invalidate()
Session对象可以保存更复杂的对象类型数据了,不像Cookie只能保存字符串。


4、session超时、设置缺省时间

Session会以对象的形式占用服务器端的内存,过多的以及长期的消耗内存会降低服务器端的运行效率,所以Session对象存在于内存中时会有默认的时间限制,一旦Session对象存在的时间超过了这个缺省的时间限制则认为是Session超时,Session会失效,不能再继续访问。(默认30min)

//返回客户端最后一次与会话有关的请求时间
public abstract long getLastAccessedTime();

//以秒为单位返回一个会话内两个请求时间间隔
public abstract int getMaxInactiveInterval();

//以秒为单位设置session有效时间
public abstract void setMaxInactiveInterval(int i);


5、禁用cookie与URL重写

Session对象的查找依赖SID,当cookie禁用的时候,为了使session仍然可用,就要使用URL重写。

URL重写:浏览器在访问服务器的某个地址时,会使用一个改写过的地址,即在原有地址后追加SessionID,这种重新定义URL内容的方式叫做URL重写。如:原有地址的写法为:

http://localhost:8080/test/some

重写后的地址写法为:

http://localhost:8080/test/some;jsessionid=4E113CB3

session保存在服务器,由浏览器发送一个sessionID(保存在cookie中,cookie封装在request对象中)来获得这个session对象,当cookie禁用,我们得不到这个sessionID,所以需要通过URL传参的方式来传递这个ID。

cookie 和session 的区别:

一、cookie数据存放在客户的浏览器上,session数据放在服务器上。

二、cookie不是很安全,别人可以分析存放在本地的COOKIE并进行COOKIE欺骗,考虑到安全应当使用session。

三、session会在一定时间内保存在服务器上。当访问增多,会比较占用你服务器的性能。考虑到减轻服务器性能方面,应当使用COOKIE。

四、单个cookie保存的数据不能超过4K,很多浏览器都限制一个站点最多保存20个cookie。

6、session产生机制

sessionid是一个会话的key,浏览器第一次访问服务器会在服务器端生成一个session,有一个sessionid和它对应。tomcat生成的sessionid叫做jsessionid。

session在访问tomcat服务器HttpServletRequest的getSession(true)的时候创建,tomcat的ManagerBase类提供创建sessionid的方法:随机数+时间+jvmid;

存储在服务器的内存中,tomcat的StandardManager类将session存储在内存中,也可以持久化到file,数据库,memcache,redis等。客户端只保存sessionid到cookie中,而不会保存session,session销毁只能通过invalidate或超时,关掉浏览器并不会关闭session。

在服务器端程序运行的过程中创建的,不同语言实现的应用程序有不同创建Session的方法,而在Java中是通过调用HttpServletRequest的getSession方法(使用true作为参数)创建的。在创建了Session的同时,服务器会为该Session生成唯一的Session id,而这个Session id在随后的请求中会被用来重新获得已经创建的Session;在Session被创建之后,就可以调用Session相关的方法往Session中增加内容了,而这些内容只会保存在服务器中,发到客户端的只有Session id;当客户端再次发送请求的时候,会将这个Session id带上,服务器接受到请求之后就会依据Session id找到相应的Session,从而再次使用之。



在哪里产生:服务器

session由谁产生:服务器的某个服务调用request.getSssion时候产生

sessionid由谁产生:Tomcat中BaseManager类的一个算法产生

什么算法:随机数+时间+jvmid

sessionid干嘛用:判断客户端是否第一次登陆

禁用cookie怎么办:URL重写或者form表单隐藏提交

7、session应用实例

第一步:session1.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta htt
10e31
p-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

<link rel="stylesheet" type="text/css" href="css/style.css">

</head>

<body>
<form id="form1" name="form1" method="post" action="session2.jsp">
<div align="center">
<table width="40%" border="0">
<tr>
<td width="36%"><div align="center">您的名字是:</div></td>
<td width="64%">
<label>
<div align="center">
<input type="text" name="name" />
</div>
</label>
</td>
</tr>
<tr>
<td colspan="2">
<label>
<div align="center">
<input type="submit" name="Submit" value="提交" />
</div>
</label>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>


第二步:session2.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'session.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="css/style.css">

</head>

<body>
<%
String name = request.getParameter("name");     //获取用户填写的用户名

session.setAttribute("name",name);              //将用户名保存在session对象中
%>
<div align="center">
<form id="form1" name="form1" method="post" action="sessionShow.jsp">
<table width="28%" border="0">
<tr>
<td>您的名字是:</td>
<td><%=name%></td>
</tr>
<tr>
<td>您最喜欢去的地方是:</td>
<td><label>
<input type="text" name="address" />
</label></td>
</tr>
<tr>
<td colspan="2"><label>
<div align="center">
<input type="submit" name="Submit" value="提交" />
</div>
</label></td>
</tr>
</table>
</form>
<p> </p>
</div>
</body>
</html>


第三步:sessionShow.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'result.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="css/style.css">

</head>

<body>
<div align="center">
<%
Cookie[] ck=request.getCookies();

for(Cookie coo:ck){
String ID=coo.getValue();
System.out.println("ID:"+ID);
}

String sessionID=request.getSession(true).getId();

String name = (String)session.getAttribute("name");     //获取保存在session范围内的对象

String solution = request.getParameter("address");      //获取用户输入的最想去的地方
%>
<form id="form1" name="form1" method="post" action="">
<table width="28%" border="0">
<tr>
<td colspan="2"><div align="center"><strong>显示答案</strong></div>          </td>
</tr>
<tr>
<td width="49%"><div align="left">您的名字是:</div></td>
<td width="51%"><label>
<div align="left"><%=name%></div>       <!-- 将用户输入的用户名在页面中显示 -->
</label></td>
</tr>
<tr>
<td><label>
<div align="left">您最喜欢去的地方是:</div>
</label></td>
<td><div align="left"><%=solution%></div></td> <!-- 将用户输入的最想去的地方在页面中显示 -->
</tr>

<td><label>
<div align="left">sessionID:</div>
</label></td>
<td><div align="left"><%=sessionID%></div></td>
</tr>

</table>
</form>
<p> </p>
</div>
</body>
</html>


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  session jsp