javaee springMVC session的使用

controller

java 复制代码
package com.test.controller;

import com.test.pojo.Address;
import com.test.pojo.Users;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/users")
//指定放入model中的某个变量 存入session中
@SessionAttributes(value="sessionUser2")
public class UsersController {

   

    //存入session方式1:原生session
    @RequestMapping("/getUser6")
    public String getUser6(HttpServletRequest request)
    {
        Users user=new Users(6,"daimenglaoshi6","888",new Address(1,"shanghai"),new Date(),888888);

        HttpSession session= request.getSession();

        session.setAttribute("sessionUser",user);

        return "showSessionUser";
    }

    //存入session方式2:用注解的方式  @SessionAttributes(value="sessionUser2")
    @RequestMapping("/getUser7")
    public ModelAndView getUser7() {

        ModelAndView modelAndView=new ModelAndView();

        Users user=new Users(7,"daimenglaoshi7","888",new Address(1,"shanghai"),new Date(),888888);

        modelAndView.addObject("sessionUser2",user);

        modelAndView.setViewName("showSessionUser2");

        return modelAndView;
    }

    @RequestMapping("/destroySession")
    public String destroySession(HttpServletRequest request){

          HttpSession session= request.getSession();

          //销毁session
          session.invalidate();

          return "showDestroySession";
    }


  

   






}

jsp

html 复制代码
<%--
  Created by IntelliJ IDEA.
  User: HIAPAD
  Date: 2019/12/5
  Time: 19:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${sessionScope.sessionUser.uname}<br/>

${sessionScope.sessionUser2.uname}<br/>
</body>
</html>
html 复制代码
<%--
  Created by IntelliJ IDEA.
  User: HIAPAD
  Date: 2019/12/5
  Time: 19:08
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${sessionUser.uname}
</body>
</html>
html 复制代码
<%--
  Created by IntelliJ IDEA.
  User: HIAPAD
  Date: 2019/12/5
  Time: 19:08
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${sessionScope.sessionUser2.uname}
</body>
</html>

两种方式存储session

方式一

原生方式

方式二

注解方式

推荐

推荐使用原生方式,因为经过测试,销毁方法只能销毁原生方式创建的session

相关推荐
大圣编程7 分钟前
面向对象深度理解
java·开发语言·算法
影寂ldy14 分钟前
C# const 常量 / readonly 只读 / static readonly
java·开发语言·c#
摇滚侠18 分钟前
Maven 入门+高深 体系外 jar 包导入 172
java·maven·jar
做个文艺程序员27 分钟前
第02篇:K8s 存储与配置管理:ConfigMap、Secret、PV/PVC 实战——Java SaaS 多租户配置最佳实践
java·容器·kubernetes
爱吃牛肉的大老虎30 分钟前
Spring中用到的设计模式
java·spring·设计模式
Refrain_zc33 分钟前
Android TV 语音消息实战:遥控器 PCM 录音失真修复与扬声器强制播放方案
java
Stick_ZYZ34 分钟前
从“能调用工具”到“能稳定执行任务”:Agent 工程化的下一步
java·人工智能·后端·spring·ai
代码中介商37 分钟前
C++四大设计模式:单例、工厂、观察者、策略
java·c++·设计模式
宋志宗41 分钟前
从三层架构到清晰边界:一套更适合复杂 Java 服务的分层方法
java
lulu12165440781 小时前
Codex Computer Use 深度分析:AI桌面自动化的技术突破与行业影响
java·运维·人工智能·自动化·ai编程