【JAVA】会话跟踪技术

目录

【会话跟踪技术】

【会话】

【会话跟踪】

【实现】

【Cookie】

【使用】

【Cookie原理】

【Cookie使用注意事项】

【cookie存活时间】

【Cookie存储中文】

【Session】

【使用】

【Session原理】

【Session使用注意事项】

【Session 钝化、活化】

【Seesion 销毁】

【Cookie 和 Session 区别】


【会话跟踪技术】

【会话】

【概述】

用户打开浏览器,访问web服务器的资源,会话建立,直到有一方断开连接,会话结束。在一次会话中可以包含多次请求和响应

【会话跟踪】

【概述】

一种维护浏览器状态的方法,服务器需要识别多次请求是否来自于同一浏览器,以便在同一次会话的多次请求间共享数据

HTTP协议是无状态的,每次浏览器向服务器请求时,服务器都会将该请求视为新的请求,因此我们需要会话跟踪技术来实现会话内数据共享

【实现】

  • 客户端会话跟踪技术:Cookie
  • 服务端会话跟踪技术:Session

【Cookie】

【概述】

客户端会话技术,将数据保存到客户端,以后每次请求都携带Cookie数据进行访问

【使用】

【发送Cookie】

1、创建Cookie对象,设置数据

Cookie cookie = new Cookie("key","value");

2、发送Cookie到客户端:使用response对象

response.addCookie(cookie);

【获取Cookie】

3、获取客户端携带的所有Cookie,使用request对象

Cookie[] cookies = request.getCookies();

4、获取客户端携带的所有Cookie,使用request对象

for循环

5、使用Cookie对象方法获取数据

cookie.getName();
cookie.getValue();

【Cookie原理】

【概述】

Cookie的实现是基于HTTP协议的

  • 响应头:set-cookie
  • 请求头:cookie

 

【Cookie使用注意事项】

【cookie存活时间】

  • 默认情况下,Cookie 存储在浏览器内存中,当浏览器关闭,内存释放,则Cookie被销毁
  • setMaxAge(int seconds):设置Cookie存活时间
  1. 正数:将 Cookie写入浏览器所在电脑的硬盘,持久化存储。到时间自动删除
  2. 负数:默认值,Cookie在当前浏览器内存中,当浏览器关闭,则 Cookie被销毁
  3. 零:删除对应 Cookie

例:

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //发送Cookie
        //创建Cookie对象
        Cookie cookie=new Cookie("username","zs");
        //设置存活时间:一分钟
        cookie.setMaxAge(60);
        //发送Cookie.response
        response.addCookie(cookie);
    }

【Cookie存储中文】

Cookie 不能直接存储中文,如需要存储,则需要进行转码:URL编码

例:

    //发送Cookie
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //发送Cookie
        //创建Cookie对象
        String value="张三";
        //URL编码
        value= URLEncoder.encode(value,"UTF-8");
        Cookie cookie=new Cookie("username",value);
        //设置存活时间:一分钟
        cookie.setMaxAge(60);
        //发送Cookie.response
        response.addCookie(cookie);
    }
    //获取Cookie
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取Cookie
        //获取Cookie数组
       Cookie[] cookies = request.getCookies();
        //遍历数组
        for (Cookie cookie : cookies) {
            //获取数据
            String name = cookie.getName();
            if("username".equals(name)){
                String value = cookie.getValue();
                //URL解码
                value = URLDecoder.decode(value , "UTF-8");
                System.out.println(name+":"+value);
            }
        }
    }

【Session】

【概述】

服务端会话跟踪技术:将数据保存到服务端

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

【使用】

1、获取Session对象

HttpSession session = request.getSession();

2、Session对象功能

  • void setAttribute(String name, Object o):存储数据到 session 域中
  • Object getAttribute(String name):根据 key,获取值
  • void removeAttribute(String name):根据 key,删除该键值对

例:

    //请求1
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //存储到Session中
        //获取Session对象
        HttpSession session=request.getSession();
        //存储数据
        session.setAttribute("username","zs");
    }
//请求2
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取数据,从Session中
        //获取Session对象
        HttpSession session=request.getSession();
        //获取数据
        Object username = session.getAttribute("username");
        System.out.println(username);
    }

【Session原理】

【概述】

Session是基于Cookie实现的

【Session使用注意事项】

【Session 钝化、活化】

  • 钝化:在服务器正常关闭后, Tomcat会自动将 Session数据写入硬盘的文件中
  • 活化:再次启动服务器后,从文件中加载数据到Session中

【Seesion 销毁】

1、默认情况下,无操作,30分钟自动销毁(在web.xml中)

<web-app>
    <session-config>
      <session-timeout>30</session-timeout>
    </session-config>
</web-app>

2、调用 Session对象的 invalidate()方法(销毁自己)

        //销毁
        session.invalidate();

【Cookie 和 Session 区别】

  1. 存储位置:Cookie 是将数据存储在客户端,Session 将数据存储在服务端
  2. 安全性:Cookie 不安全,Session 安全
  3. 数据大小:Cookie 最大3KB,Session 无大小限制
  4. 存储时间:Cookie 可以长期存储,Session 默认30分钟
  5. 服务器性能:Cookie 不占服务器资源,Session 占用服务器资源

猜你喜欢

转载自blog.csdn.net/huihu__/article/details/126298968