Session技术
1.基本概念
- Session本意为"会话"的含义,是用来维护一个客户端和服务器关联的一种技术。
- 浏览器访问服务器时,服务器会为每一个浏览器都在服务器端的内存中分配一个空间,用于创建一 个Session对象,该对象有一个id属性且该值唯一,我们称为SessionId,并且服务器会将这个SessionIdCookie方式发送给浏览器存储。
- 浏览器再次访问服务器时会将SessionId发送给服务器,服务器可以依据SessionId查找相对应的Session对象
2.相关方法
使用javax.servlet.http.HttpServletRequest接口的成员方法实现Session的获取
方法声明 | 功能介绍 |
HttpSession getSession() | 返回此请求关联的当前Session,若此请求没有则创建一个 |
使用javax.servlet.http.HttpSession接口的成员方法实现判断和获取
方法声明 | 功能介绍 |
boolean isNew() | 判断是否为新创建的Session |
String getId() | 获取Session的编号 |
使用javax.servlet.http.HttpSession接口的成员方法实现属性的管理
方法声明 | 功能介绍 |
Object getAttribute(String name) | 返回在此会话中用指定名称绑定的对象,如果没有对象在该名称下绑定,则返回空值 |
void setAttribute(String name, Object value) | 使用指定的名称将对象绑定到此会话 |
void removeAttribute(String name) | 从此会话中删除与指定名称绑定的对象 |
3.生命周期
- 为了节省服务器内存空间资源,服务器会将空闲时间过长的Session对象自动清除掉,服务器默认 的超时限制一般是30分钟。
- 使用javax.servlet.http.HttpSession接口的成员方法实现失效实现的获取和设置。
方法声明 | 功能介绍 |
int getMaxInactiveInterval() | 获取失效时间 |
void setMaxInactiveInterval(int interval) | 设置失效时间 |
可以配置web.xml文件修改失效时间
<session-config>
<session-timeout>30</session-timeout>
</session-config>
4.Session特点
- 数据比较安全。
- 能够保存的数据类型丰富,而Cookie只能保存字符串。能够保存更多的数据,而Cookie大约保存4KB。
- 数据保存在服务器端会占用服务器的内存空间,如果存储信息过多、用户量过大,会严重影响服务 器的性能。
5.代码示例
package com.lagou.demo03.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet(name = "SessionServlet", urlPatterns = "/session")
public class SessionServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1.调用getSession方法获取或者创建Session对象
HttpSession session = request.getSession();
// 2.判断该Session对象是否为新建的对象
System.out.println(session.isNew()? "新创建的Session对象": "已有的Session对象");
// 3.获取编号并打印
String id = session.getId();
System.out.println("获取到的Session编号为:" + id);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
package com.lagou.demo03.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet(name = "SessionServlet2", urlPatterns = "/session2")
public class SessionServlet2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
// 1.设置属性名和属性值
session.setAttribute("name", "machao");
// 2.获取指定属性名对应的属性值
System.out.println("获取到的属性值为:" + session.getAttribute("name")); // machao
// 3.删除指定的属性名
session.removeAttribute("name");
// 4.获取指定属性名对应的属性值
System.out.println("获取到的属性值为:" + session.getAttribute("name")); // null
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
package com.lagou.demo03.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet(name = "SessionServlet3", urlPatterns = "/session3")
public class SessionServlet3 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1.获取Session对象
HttpSession session = request.getSession();
// 2.获取对象的默认失效时间并打印
int maxInactiveInterval = session.getMaxInactiveInterval();
System.out.println("获取到的失效时间为:" + maxInactiveInterval); // 1800
// 3.修改实现时间后重新获取并打印
session.setMaxInactiveInterval(1200);
maxInactiveInterval = session.getMaxInactiveInterval();
System.out.println("获取到的失效时间为:" + maxInactiveInterval); // 1200
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
节选自拉钩教育JAVA系列教程