【学习笔记】Session

Session

什么是session

  • Session:在计算机中,尤其是在网络应用中,称为“会话控制”。Session对象用于存储用户会话所需的属性和配置信息,这样,当用户在应用程序的Web页之间跳转时,存储在Session对象中的变量将不会丢失,而是在整个用户会话中一直存在下去。当用户请求来自应用程序的 Web页时,如果该用户还没有会话,则Web服务器将自动创建一个 Session对象。当会话过期或被放弃后,服务器将终止该会话。Session 对象最常见的一个用法就是存储用户的首选项。例如,如果用户指明不喜欢查看图形,就可以将该信息存储在Session对象中。

  • 服务器会给每一个用户(浏览器)创建一个Session对象,一个Session独占一个浏览器,只要浏览器没有关闭,这个Session就一直存在

  • 常见场景:用户登录后,就可以访问这个网站的所有功能。

 

Session源码:

接口:HttpSession

方法:

String getId();   //获得Session的id
long getCreationTime();   //获得Session的创建时间
ServletContext getServletContext();   //获得ServletContext对象
void setAttribute(String var1, Object var2);  //向Session中放数据
Object getAttribute(String var1);   //从Session中拿数据
void removeAttribute(String var1);  //清除session中的数据
void invalidate();    //清除session
boolean isNew();    //判断是否是新创建的session

 

Session使用:

获得session及session的id

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
​
import java.io.IOException;
​
public class SessionDemo01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");
​
        //拿到Session
        HttpSession session = req.getSession();
​
        //往session中放数据
        session.setAttribute("name","张三"); //第一个参数是key,第二个是value
​
        if (session.isNew()){
            resp.getWriter().write("这是新的Session,id:"+session.getId());
        }else {
            resp.getWriter().write("这不是新的Session,id:"+session.getId());
        }
​
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

image-20221019155130656

结果是“这不是新的Session,id:554B427911AA189DC3CDB8AAF0E379CA“

原因就是,当我们一打开浏览器,服务器就为我们创建了一个Session,并且把SessionId保存在cookie中

image-20221019155514241

 

在新的Servlet中拿到session中的数据

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
​
import java.io.IOException;
​
public class SessionDemo02 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");
        //拿到session
        HttpSession session = req.getSession();
        //获取session中的数据
        String name = (String) session.getAttribute("name");//参数为key
​
        resp.getWriter().write(name);
    }
​
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

 

清除session中的数据

//拿到session
HttpSession session = req.getSession();
//清除session中的数据
session.removeAttribute("name");

 

注销Session

//拿到session
HttpSession session = req.getSession();
//注销session
session.invalidate();

当我们访问注销Session的servlet后,session就被注销了,sessionId也就没了,当我们再去执行获得session的servlet时,就会出现”这是新的Session,id:0D48CB84BC9C542A9966DD6AEBA7D3FB“

image-20221019161110935image-20221019161128734

 

即使在同一台电脑上,不同的浏览器,获得的session是不同的

 

设置Session的默认失效时间

<session-config>
    <!--以分钟为单位,1分钟后Session自动失效-->
    <session-timeout>1</session-timeout>
</session-config>

 

Session和Cookie的区别

  • Cookie是把用户的数据写给用户的浏览器,是浏览器保存

  • Session是把用户的数据写道用户独占的session中,服务器保存

  • Session对象由服务器创建

 

Cookie:第一次访问,服务器给用户一个Cookie,每一次用户访问都要携带这个cookie,服务器就知道是该用户来访问了。

image-20221019162040513

 

Session:用户第一次来登记一个session,每个用户都有一个唯一的SessionID,用户拿到的是sessionID,通过sessionID证明自己,服务器中的Session可以存放数据

image-20221019162339982

posted @ 2022-10-19 16:25  GrowthRoad  阅读(40)  评论(0编辑  收藏  举报