服务端会话跟踪技术Session

客户端会话跟踪技术Session

Session:是服务端会话技术,将数据保存到服务端。

  1. Session是存储在服务端而Cookie是存储在客户端
  2. 存储在客户端的数据容易被窃取和截获,存在很多不安全的因素
  3. 存储在服务端的数据相比于客户端来说就更安全

Session的基本使用:

JavaEE提供HttpSession接口,来实现一次会话的多次请求间数据共享功能。

​ 1.获取Session对象

HttpSession session = request.getSession(); 

Session对象提供的功能:

  • 存储数据到 session 域中

    void setAttribute(String name, Object o)
    
  • 根据 key,获取值

    Object getAttribute(String name)
    
  • 根据 key,删除该键值对

    void removeAttribute(String name)
    

例如:

(1)创建servlet类SessionDemo1:获取Session对象、存储数据

@WebServlet("/demo1")
public class SessionDemo1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //存储到Session中
        //1.获取session对象
        HttpSession session = request.getSession();
        //2.存储数据
        session.setAttribute("username","zs");


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

(2)创建servlet类SessionDemo2:获取Session对象、获取数据

@WebServlet("/demo2")
public class SessionDemo2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //从session中获取数据
        //1.获取session对象
        HttpSession session = request.getSession();
        //2.获取数据
        Object username = session.getAttribute("username");
        System.out.println(username);


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

Session的原理:

Session是基于Cookie实现的

在这里插入图片描述

(1)使用浏览器访问http://localhost:8080/cookie-demo/demo1,打开开发者模式,查看 响应头(Response Headers) 数据:

在这里插入图片描述

(2)使用浏览器再次访问http://localhost:8080/cookie-demo/demo2,查看 请求头(Request Headers) 数据:

在这里插入图片描述

Session的使用细节:

Session钝化:在服务器正常关闭后,Tomcat会自动将Session数据写入硬盘的文件中

Session活化:再次启动服务器后,从文件中加载数据到Session中

session的销毁会有两种方式:

  • 默认情况下,无操作,30分钟自动销毁

    • 对于这个失效时间,是可以通过配置进行修改的

      • 在项目的web.xml中配置

        <?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_3_1.xsd"
                 version="3.1">
        
            <session-config>
                <session-timeout>100</session-timeout>
            </session-config>
        </web-app>
        
      • 如果没有配置,默认是30分钟,默认值是在Tomcat的web.xml配置文件中写死的

  • 调用Session对象的invalidate()进行销毁
@WebServlet("/demo2")
public class SessionDemo2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //从session中获取数据
        //1.获取session对象
        HttpSession session = request.getSession();
        System.out.println(session);

        //销毁
        session.invalidate();
        //2.获取数据
        Object username = session.getAttribute("username");
        System.out.println(username);


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

销毁后访问http://localhost:8080/cookie-demo/demo2

在这里插入图片描述

  • Cookie和Session的区别:
    • 存储位置:Cookie 是将数据存储在客户端,Session 将数据存储在服务端
    • 安全性:Cookie不安全,Session安全
    • 数据大小:Cookie最大3KB,Session无大小限制
    • 存储时间:Cookie可以通过setMaxAge()长期存储,Session默认30分钟
    • 服务器性能:Cookie不占服务器资源,Session占用服务器资源
  • 应用场景:
    • 购物车:使用Cookie来存储
    • 以登录用户的名称展示:使用Session来存储
    • 记住我功能:使用Cookie来存储
    • 验证码:使用session来存储
  • 结论
    • Cookie是用来保证用户在未登录情况下的身份识别
    • Session是用来保存用户登录后的数据
      服务器资源
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值