Session 服务器会话技术
Cookie的局限:
1.Cookie只能存字符串类型。不能保存对象。
2.一个cookie的容量不超过4KB。
Session概念
1.概念
服务器端会话技术,在一次会话多次请求间共享数据,将数据保存在服务器端的对象中。HttpSession
2.常用方法
1.创建或者得到Session对象
HttpSession getSession()
HttpSession getSession(boolean create)
2.设置Session对象
void setMaxInactiveInterval(int interval):设置Session的有效时间
void invalidate():销毁Session对象
java.lang.String getId():得到Session编号
3.保存会话数据到Session对象
void setAttribute(java.lang.String name, java.lang.Object value):保存数据
java.lang.Object getAttribute(java.lang.String name):获取数据
void removeAttribute(java.lang.String name):清除数据
3.原理
Session的实现是依赖于Cookie的。
现象:服务器能够识别不同的浏览者!!!
浏览器1:(给s1分配一个唯一的标记:s001,把s001发送给浏览器)
1)创建session对象,保存会话数据
HttpSession session = request.getSession(); --保存会话数据 s1
浏览器1:新窗口(带着s001的标记到服务器查询,s001->s1,返回s1)
1)得到session对象的会话数据
HttpSession session = request.getSession(false); --可以取出 s1
新的浏览器1:(没有带s001,不能返回s1)
1)得到session对象的会话数据
HttpSession session = request.getSession(false); --不可以取出 s2
浏览器2:(没有带s001,不能返回s1)
1)得到session对象的会话数据
HttpSession session = request.getSession(false); --不可以取出 s3
newSession代码解读
HttpSession session = request.getSession();
1)第一次访问创建session对象,给session对象分配一个唯一的ID,叫JSESSIONID
new HttpSession();
2)把JSESSIONID作为Cookie的值发送给浏览器保存
Cookie cookie = new Cookie("JSESSIONID", sessionID);
response.addCookie(cookie);
3)第二次访问的时候,浏览器带着JSESSIONID的cookie访问服务器
4)服务器得到JSESSIONID,在服务器的内存中搜索是否存放对应编号的session对象。
5)如果找到对应编号的session对象,直接返回该对象
6)如果找不到对应编号的session对象,创建新的session对象,继续走1的流程
前提:在哪个session域对象保存数据,就必须从哪个域对象取出!!!
结论:通过JSESSION的cookie值在服务器找session对象!!!
4. Sesson细节
1)java.lang.String getId():得到session编号
2)两个getSession方法:
getSession(true) / getSession():创建或得到session对象,没有匹配的session编号,自动创建新的session对象。
getSession(false): 得到session对象,没有匹配的session编号,返回null
3)void setMaxInactiveInterval(int interval):设置session的有效时间
session对象销毁时间:
1.默认情况30分服务器自动回收
2.修改session回收时间
3.全局修改session有效时间
<!-- 修改session全局有效时间:分钟 -->
<session-config>
<session-timeout>1</session-timeout>
</session-config>
4.手动销毁session对象
void invalidate():销毁session对象
4)如何避免浏览器的JSESSIONID的cookie随着浏览器关闭而丢失的问题
手动发送一个硬盘保存的cookie给浏览器
Cookie c = new Cookie("JSESSIONID",session.getId());
c.setMaxAge(60*60);
response.addCookie(c);
5. session的钝化和活化
客户端不关闭,服务器关闭后,两次获取的session不是同一个,但是要确保数据不丢失。
tomcat已经自动完成以下工作
钝化:在服务器正常关闭之前,将session对象系列化到硬盘上
活化:在服务器启动后,将session文件转化为内存中的session对象即可
6. session的特点
1. session用于存储一次会话的多次请求的数据,存在服务器端
2. session可以存储任意类型,任意大小的数据
7. session与Cookie的区别:
1. session存储数据在服务器端,Cookie在客户端
2. session没有数据大小限制,Cookie有
3. session数据安全,Cookie相对于不安全
##代码实现##
登录页面:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="/sessiondemo1"> 用户名:<input type="text" name="username"><br><br> 密码:<input type="password" name="password"><br><br> <input type="submit" name="登录"> </form> </body> </html>
实体类:
public class User { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User{" + "username='" + username + '\'' + ", password='" + password + '\'' + '}'; } }
新建Session,保存数据
import com.qf.pojo.User; 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("/sessiondemo1") public class SessionDemo1 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取前端提交的数据 String username = req.getParameter("username"); String password = req.getParameter("password"); //将数据保存到对象中 User user = new User(); user.setUsername(username); user.setPassword(password); //模拟数据库 String db_username = "jack"; String db_password = "123"; //创建Session对象 HttpSession session = req.getSession(); //保存数据 session.setAttribute("user", user); //比价数据 if (db_password.equals(password)){ //跳转主页 resp.sendRedirect(req.getContextPath() + "/sessiondemo2"); }else { resp.sendRedirect(req.getContextPath() + "/login.html"); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } }
Session获取数据
import com.qf.pojo.User; import javax.servlet.ServletContext; 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("/sessiondemo2") public class SessionDemo2 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //编码格式 resp.setContentType("text/html;charset=utf-8"); //创建Session HttpSession session = req.getSession(false); if(session != null) { //根据 键 获取数据 User user = (User) session.getAttribute("user"); //判断 if (user != null) { //输出 resp.getWriter().write("<html><body>欢迎:" + user.getUsername() + "<a href = '/sessiondemo3'>安全退出</a></body></html>"); } else { //返回登录页面 resp.sendRedirect(req.getContextPath() + "login.html"); } }else { //返回登录页面 resp.sendRedirect(req.getContextPath() + "login.html"); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } }
Session移除对象
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("/sessiondemo3") public class SessionDemo3 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //创建Session HttpSession session = req.getSession(false); //销毁httpSession session.invalidate(); //清除数据 // session.removeAttribute("user"); //返回登录页面 resp.sendRedirect(req.getContextPath() + "/login.html"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } }
获取SessionID
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("/sessiondemo4") public class SessionDemo4 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //创建Session HttpSession session = req.getSession(false); //打印SessionID resp.getWriter().write(session.getId()); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } }
Session有效期
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("/sessiondemo5") public class SessionDemo5 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //创建Session HttpSession session = req.getSession(false); //打印SessionID resp.getWriter().write(session.getId()); //设置Session有效期,单位秒 session.setMaxInactiveInterval(10); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } }
Session默认有效期
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!-- Session默认时长30分钟 --> <session-config> <!-- 单位分钟 --> <session-timeout>1</session-timeout> </session-config> </web-app>

浙公网安备 33010602011771号