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

相关推荐
小番茄程序猿几秒前
RAG 的 precision 只有 0.55,我以为检索烂了,MRR 一测才发现是我用错了尺子
java·后端
天空鸟_时光不老1 分钟前
07-检查点与状态持久化
java·人工智能·spring boot·spring·spring cloud·kafka·maven
GoodStudyAndDayDayUp5 分钟前
一个简单的java jar跑docker
java·docker·jar
Nebula_g18 分钟前
JavaSE加强:Commons-io框架
java·开发语言·算法·javase
程序猿_极客28 分钟前
【免费】2026分享一套优质的基于Java的电子产品抢购管理系统的设计与实现(智能推荐算法+可视化图表),源码+文档+视频详解(讲解)
java·spring boot·后端·协同过滤·电子产品抢购系统
小羊没烦恼!30 分钟前
关于大型asp.net应用系统的架构-架构的选择
java·服务器·开发语言·前端·c#
天空鸟_时光不老31 分钟前
09-RAG问答系统落地:从默认分割器的坑到Milvus召回调优
java·人工智能·spring boot·spring·spring cloud·maven·mybatis
小羊没烦恼!40 分钟前
Windows Azure Platform体验(1):Windows Azure
java·大数据·后端·python·flask·word·.net
suaizai_1 小时前
ComputerUse:让AI真正操控桌面系统
java·前端·javascript
fundoit1 小时前
资源服务器如何对 JWT 进行验签
java·运维·服务器·spring boot·php·oauth2