【什么是Cookie、Session】

Cookie

Cookie是什么?

Cookie是浏览器访问服务器时获取到的一份具有有效期的信息。当浏览器获取到Cookie之后会存到本地磁盘中,只要Cookie还在有效期内,那么再次访问服务器时,会自动携带Cookie给服务器。

Cookie有什么用?

Cookie可以存放访问服务器时需要用到多次的信息,比说用户的信息、权限、会话时间等。

Cookie的特点:

1)不可跨域
2)存储中文会乱码
3)有效期
4)只能存储字符串键值对

代码示例
package com.peko.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLDecoder;

@RestController
public class MyController {

    @RequestMapping("/cookieSet")
    public void cookieSet(HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        Cookie cookie = new Cookie("username","jack");
        cookie.setMaxAge(5);   //单位:秒
//        cookie.setMaxAge(-1);  //设置为负数,则Cookie是临时性的,关闭浏览器后失效
//        cookie.setMaxAge(0);   //设置为0,则为删除该Cookie
        response.addCookie(cookie);
    }

    @RequestMapping("/cookieGet")
    public String cookieGet(HttpServletRequest request,HttpServletResponse response) throws IOException {
        Cookie[] cookies = request.getCookies();
        String value = "";
        for(int i=0;cookies != null && i<cookies.length;i++){
            String name = cookies[i].getName();
            value = URLDecoder.decode(cookies[i].getValue(),"UTF-8");

        }
        //如果想修改Cookie,则要以覆盖的形式
//        Cookie cookie = new Cookie("username","jack1111");
//        cookie.setMaxAge(5);
//        response.addCookie(cookie);

        return value;
    }
}

首先访问 "/cookieSet"
在这里插入图片描述
然后访问 "/cookieGet"
在这里插入图片描述


Session

Session是什么?

Session是一种服务器存储浏览器访问信息的一种机制,只要Session对象没有被销毁,Servlet(即各应用)之间就可以通过Session对象实现通讯。

Session有什么用?

在需要保存用户数据时,服务器程序可以把用户数据写到用户浏览器独占的session中,当用户使用浏览器访问其它程序时,其它程序可以从用户的session中取出该用户的数据,为用户服务。

Session的特点:

1)可以储存对象
2)Session的超时时间默认是30分钟,超时之后的Session会被删除,在未超时的情况下访问Session时,Session会更新超时时间
3)利用Cookie区分每个浏览器用户:服务器会给浏览器发送Cookie,里面存有一个JESSIONID的值,服务器会根据这个来区别各个浏览器,Cookie的maxAge值默认是-1,这也就为什么关闭了浏览器之后,再次访问服务器会找不到先前的Session。

代码示例
package com.peko.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLDecoder;

@RestController
public class MyController {

    @RequestMapping("/sessionSet")
    public void sessionSet(HttpServletRequest request,HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        session.setAttribute("number","123455");

//          设置Session最长超时时间为60秒,这里的单位是秒
//          session.setMaxInactiveInterval(60);
    }

    @RequestMapping("/sessionGet")
    public void sessionGet(HttpServletRequest request,HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        String number = (String)session.getAttribute("number");
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write("获取session中的number:"+number);
    }
}

首先访问 "/sessionSet"
在这里插入图片描述

然后访问 "/sessionGet"
在这里插入图片描述

推荐博文:
https://mp.weixin.qq.com/s?__biz=MzI4Njg5MDA5NA==&mid=2247484755&idx=6&sn=3a370551b0ee800f3bcad8ff37a72b9d&chksm=ebd74452dca0cd44f454ca8aa006d352c6994bb7ea955b5ca5f2ec2b227792010939bfa25532###rd
https://mp.weixin.qq.com/s?__biz=MzI4Njg5MDA5NA==&mid=2247484755&idx=7&sn=fb35232f3c15e2b4336498ac9f8804f1&chksm=ebd74452dca0cd44942721a159088a2f286d4e5c5f2bcdc7e264f0dccc8f9928d66858e475d4###rd
https://www.cnblogs.com/xdp-gacl/p/3855702.html

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值