在spring boot的controller层里使用session

概念

解释session:当访问服务器否个网页的时候,会在服务器端的内存里开辟一块内存,这块内存就叫做session,而这个内存是跟浏览器关联在一起的。这个浏览器指的是浏览器窗口,或者是浏览器的子窗口,意思就是,只允许当前这个session对应的浏览器访问,就算是在同一个机器上新启的浏览器也是无法访问的。而另外一个浏览器也需要记录session的话,就会再启一个属于自己的session

上代码

package com.springboot06.test.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author SongXianYang
 * @date 2020-07-26 13:01:01
 **/
@Controller
@RequestMapping("session")
public class SessionTest {
    @RequestMapping("test")
    public String test() {
        return "index/sessionTest";
    }

    @PostMapping("post")
    @ResponseBody
    public String mySession(HttpServletRequest request, @RequestParam("username") String username) {
        //首先获取session
        HttpSession session = request.getSession();
        //往session中存入你想要的东西
        session.setAttribute("username", username);
//        session.setAttribute("password",passWord);
//        session.setAttribute("userId",userId);

        //然后就可以通过rsession.getAttribute 来获取你存的东西
        String username1 = (String) session.getAttribute("username");
        System.out.println(username1);
        return username1;
    }

}

结果输出:
user

控制台:
在这里插入图片描述

页面打印:
在这里插入图片描述
前端代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    <form action="/session/post" method="post">
        用户名:<input name="username" type="text"><br>
        <button type="submit">提交</button>
    </form>
</div>
</body>
</html>

就一个表单提交

暂时没有总结
知识点不少慢慢学 加油

https://www.jianshu.com/p/73974b6d4f90

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Spring Boot中,不同的Controller层之间是可以共享Session存储的数据的。这是因为Session是与用户会话相关的,而不是与特定的Controller层关联。 当用户在一个Controller层中存储数据到Session中后,其他Controller层可以通过HttpServletRequest对象获取同一个会话的Session,并访问存储在Session中的数据。 以下是一个示例代码,展示了如何在不同的Controller层中共享Session存储的数据: ```java @Controller public class FirstController { @Autowired private HttpServletRequest request; @PostMapping("/login") public String login(String username) { HttpSession session = request.getSession(); session.setAttribute("username", username); // ... } } @Controller public class SecondController { @Autowired private HttpServletRequest request; @GetMapping("/profile") public String profile(Model model) { HttpSession session = request.getSession(); String username = (String) session.getAttribute("username"); model.addAttribute("username", username); // ... } } ``` 在上述示例中,`FirstController`中的`login()`方法将用户名存储到Session中。然后,`SecondController`中的`profile()`方法通过HttpServletRequest对象获取同一个会话的Session,并读取存储在Session中的用户名。在`profile()`方法中,可以将用户名传递给前端页面或进行其他操作。 要注意的是,在使用Session存储数据时,需要确保会话的有效性。Spring Boot默认使用基于Cookie的会话管理,通过在响应中设置Session Cookie来识别和跟踪用户会话。确保在配置文件中启用Session支持,并设置合适的Session超时时间等配置,以确保会话的正确管理。 总结来说,不同的Controller层可以共享Session存储的数据,只需通过HttpServletRequest对象获取同一个会话的Session即可。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SteveCode.

永远年轻,永远热泪盈眶

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值