【Spring】获取cookie,session,header(3)

本系列共涉及4个框架:Sping,SpringBoot,Spring MVC,Mybatis。

博客涉及框架的重要知识点,根据序号学习即可。

目录

[本系列共涉及4个框架:Sping,SpringBoot,Spring MVC,Mybatis。](#本系列共涉及4个框架:Sping,SpringBoot,Spring MVC,Mybatis。)

博客涉及框架的重要知识点,根据序号学习即可。

1、获取cookie

1.1传统版本【使用HttpServletRequest】

1.2简洁版本【使用注解@CookieValue】

2、获取session

2.1传统版本【使用HttpServletRequest】

2.2简洁版本【使用注解@SessionAttribute】

2.3简洁版本【使用HttpSession】

3、获取header

3.1传统版本【使用HttpServletRequest】

3.2简洁版本【使用注解@RequestHeader】

最后:无论使用传统的HttpServletRequest获取,还是使用注解获取,大差不差,关键要懂其原理,当然更推荐使用注解的方式,因为更方便(哈哈哈哈哈哈代码少!!!)

1、获取cookie

1.1传统版本【使用HttpServletRequest】

复制代码
    @RequestMapping("/m11")
    public String m11(HttpServletRequest request, HttpServletResponse response){
        //获取所有的cookie信息
        Cookie[] cookies=request.getCookies();
        //打印cookie信息
        StringBuilder builder=new StringBuilder();
        if(cookies!=null){
            for(Cookie ck:cookies){
                builder.append(ck.getName()+":"+ck.getValue());
            }
        }

        return "Cookie信息:"+builder;
    }

运行结果:

没有获取到cookie是因为没有设置,下图展示如何自定义cookie【 在Application中手动添加cookie,其实这是不安全的,后端需要进行cookie的校验】

1.2简洁版本【使用注解@CookieValue】

复制代码
 @RequestMapping("/m12")
    public String m12(@CookieValue("zhangsan") String name){
        //简洁版获取cookie,使用注解
        return "获取到的name:"+name;
    }

注意区分这里的zhangsan 和name ,搞懂为啥不是第一个cookie,而是以zhangsan为name的cookie。因为@CookieValue

2、获取session

2.1传统版本【使用HttpServletRequest】

复制代码
    @RequestMapping("/m13")
    public String m13(HttpServletRequest request){
        //session的存储
        //获取session对象
        HttpSession session =request.getSession();//与getSession(true)一样,默认值为true
        if(session!=null){
            session.setAttribute("u","123");
        }
        return "Session存储成功";
    }


    @RequestMapping("/m14")
    public String m14(HttpServletRequest request){
        //session的读取
        //如果session不存在,不会自动创建session
        HttpSession session=request.getSession(false);
        String username=null;
        if(session!=null&&session.getAttribute("u")!=null){
            username=(String) session.getAttribute("u");
        }
        return "username:"+username;
    }

session是存放在服务器中的,不方便演示,所以我们先存储session,在读取

2.2简洁版本【使用注解@SessionAttribute】

复制代码
    @RequestMapping("/m15")
    public String m15(@SessionAttribute(value = "u",required = false)String username){
        //获取session简洁版【1】
        return "username:"+username;
    }

2.3简洁版本【使用HttpSession】

复制代码
    @RequestMapping("/m16")
    public String m16(HttpSession session){
        //获取session简洁版【2】
        //通过Spring MVC内置队象HttpSession来获取
        String username=(String)session.getAttribute("u");
        return "username:"+username;
    }

3、获取header

3.1传统版本【使用HttpServletRequest】

复制代码
    @RequestMapping("/m17")
    public String m17(HttpServletRequest request){
        //传统获取header
        String userAgent=request.getHeader("User-Agent");

        return "userAgent:"+userAgent;
    }

3.2简洁版本【使用注解@RequestHeader】

复制代码
    @RequestMapping("/m18")
    public String m18(@RequestHeader("User-Agent")String userAgent){
        //简洁版获取header
        return "userAgent:"+userAgent;
    }

最后:无论使用传统的HttpServletRequest获取,还是使用注解获取,大差不差,关键要懂其原理,当然更推荐使用注解的方式,因为更方便(哈哈哈哈哈哈代码少!!!)

相关推荐
岳麓丹枫0017 小时前
PostgreSQL 中 pg_wal 目录里的 .ready .done .history 文件的生命周期
数据库·postgresql
Coder_Boy_7 小时前
技术发展的核心规律是「加法打底,减法优化,重构平衡」
人工智能·spring boot·spring·重构
陌上丨13 小时前
Redis的Key和Value的设计原则有哪些?
数据库·redis·缓存
AI_567813 小时前
AWS EC2新手入门:6步带你从零启动实例
大数据·数据库·人工智能·机器学习·aws
ccecw14 小时前
Mysql ONLY_FULL_GROUP_BY模式详解、group by非查询字段报错
数据库·mysql
JH307314 小时前
达梦数据库与MySQL的核心差异解析:从特性到实践
数据库·mysql
数据知道14 小时前
PostgreSQL 核心原理:如何利用多核 CPU 加速大数据量扫描(并行查询)
数据库·postgresql
麦聪聊数据15 小时前
Web 原生架构如何重塑企业级数据库协作流?
数据库·sql·低代码·架构
未来之窗软件服务15 小时前
数据库优化提速(四)新加坡房产系统开发数据库表结构—仙盟创梦IDE
数据库·数据库优化·计算机软考
程序员侠客行16 小时前
Mybatis连接池实现及池化模式
java·后端·架构·mybatis