• 技术文章 >Java >java教程

    简述java中的session用法和细节记录

    Y2JY2J2017-05-08 13:33:27原创4183
    下面小编就为大家带来一篇浅谈SpringMVC中的session用法及细节记录。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    前言

    初学SpringMVC,最近在给公司做的系统做登录方面,需要用到session。

    在网上找了不少资料,大致提了2点session保存方式:

    1、javaWeb工程通用的HttpSession

    2、SpringMVC特有的@SessionAttributes

    我个人比较关注@SessionAttributes的用法,毕竟现在是在用SpringMVC嘛。但是我看网上那些文章,基本都是只说明了基础用法,详细的使用和细节却基本没有,我想这是不够的,所以我自己做了一些测试,然后整理了下代码做了个demo,记录并分享下,有什么不足的欢迎提出来讨论。

    好了,废话就说到这,下面正戏开始!

    结论

    嗯,为了给一些不喜欢看代码的客官省去翻结论的麻烦,我这里就先把我测试后的结论先列一下吧。

    1、可以通过SpringMVC特有的ModelMap、Model在Controller中自动保存数据到session,也可以通过传统的HttpSession等参数保存session数据

    2、保存session数据必须使用@SessionAttributes注解,该注解有2种参数声明方式(value和types),且该注解声明必须写在类上,不能在方法上

    3、保存的session数据必须与@SessionAttributes注解中的参数列表对应,未被声明的参数无法保存到session中

    4、使用SessionStatus可以清除session中保存的数据,注意是全部清除,无法单独删除指定的session数据。同时,清除时有效权限遵循上述第2、3条规则(借用此规则可人为达到删除指定session数据的效果)

    5、通过ModelMap等读取session中数据时,也有上述的参数权限限制

    6、使用ModelMap或Model等保存session数据时,ModelMap必须作为方法参数传入,在方法中新定义的无效。同时,只要把ModelMap作为参数传入,即使是被别的方法调用也能起效

    7、使用@ResponseBody注解时(一般配合ajax使用),无法保存session数据

    8、@SessionAttributes注解可以使用value和types 2种参数列表

    9、使用HttpSession的传统方式操作没有上述注解及权限等限制,下面有简单测试,但是不做具体说明

    以下还有几个应该算是常识性的知识点

    10、操作session数据可以跨类,与包或者url的路径等也没有关系

    11、同一个session值操作,后面的值会覆盖前面的值

    测试代码及简单说明

    开发工具: Spring Tool Suite 。

    spring专为SpringMVC搞出来的一款基于Eclipse的IDE开发工具,集成了Maven和Tomcat,最近用下来感觉还不错的,推荐下。

    首先来一个项目结构截图吧

    1.jpg

    因为后面的测试中有用到ajax的@ResponseBody注解,所以要在pom.xml文件中配置jar包。

    <!-- 使用@ResponseBody注解所需的2个包 -->
    <dependency>
     <groupId>org.codehaus.jackson</groupId>
     <artifactId>jackson-core-asl</artifactId>
     <version>1.9.13</version>
    </dependency>
    <dependency>
     <groupId>org.codehaus.jackson</groupId>
     <artifactId>jackson-mapper-asl</artifactId>
     <version>1.9.13</version>
    </dependency>

    下面是主要的测试代码

    package test.dmh.session;
    
    import java.util.Enumeration;
    
    import javax.servlet.http.HttpSession;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.SessionAttributes;
    import org.springframework.web.bind.support.SessionStatus;
    
    /**
     * @SessionAttributes 只声明了参数test1
     */
    @Controller
    @SessionAttributes(value={"test1"})
    public class HomeController {
     
     private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
     
     @RequestMapping(value = "/show1")
     public String show(ModelMap modelMap, HttpSession session) {
      logger.info("show session");
      for (Object key : modelMap.keySet()) {
       Object value = modelMap.get(key);
       System.out.println(key + " = " + value);
      }
      System.out.println("***********************************");
      Enumeration<String> e = session.getAttributeNames();
      while (e.hasMoreElements()) {
       String s = e.nextElement();
       System.out.println(s + " == " + session.getAttribute(s));
      }
      System.out.println("***********************************");
      return "home";
     }
     
     @RequestMapping("/set1")
     public String setSession(ModelMap modelMap) {
      logger.info("set session 1");
      modelMap.addAttribute("test1", "value 1"); //设置一个在@SessionAttributes中声明过的参数
      modelMap.addAttribute("test2", "value 2"); //设置一个未在@SessionAttributes中声明过的参数
      return "home";
     }
     
     @RequestMapping("/setM")
     public String setSessionM(Model model) {
      logger.info("set session 1");
      model.addAttribute("test1", "value 1"); //设置一个在@SessionAttributes中声明过的参数
      model.addAttribute("test2", "value 2"); //设置一个未在@SessionAttributes中声明过的参数
      return "home";
     }
     
     @RequestMapping("/clear1")
     public String clear(SessionStatus status) {
      logger.info("clear session 1");
      status.setComplete();
      return "home";
     }
     
    }
    package test.dmh.session.controller;
    
    import javax.servlet.http.HttpSession;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    /**
     * 没有使用@SessionAttributes注解
     */
    @Controller
    public class IndexController {
     
     private static final Logger logger = LoggerFactory.getLogger(IndexController.class);
     
     @RequestMapping("/set2")
     public String setSession(ModelMap modelMap, HttpSession session) {
      logger.info("set session 2 : without @SessionAttributes");
      modelMap.addAttribute("test3", "value 3");
      session.setAttribute("test4", "value 4");
      return "home";
     }
     
    }
    package test.dmh.session.controller;
    
    import java.util.Enumeration;
    import java.util.HashMap;
    import java.util.Map;
    
    import javax.servlet.http.HttpSession;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.bind.annotation.SessionAttributes;
    import org.springframework.web.bind.support.SessionStatus;
    
    @Controller
    @SessionAttributes(value={"test5", "index"})
    public class IndexController2 {
     
     private static final Logger logger = LoggerFactory.getLogger(IndexController2.class);
     
     @RequestMapping("/set3")
     public String setSession(ModelMap modelMap, HttpSession session) {
      logger.info("set session 3");
      modelMap.addAttribute("test5", "value 5");
      session.setAttribute("test6", "value 6");
      
      ModelMap map = new ModelMap();
      map.addAttribute("test7", "value 7");
      
      this.setValueToSession(modelMap, session, "Hello World");
      
      return "home";
     }
     
     @ResponseBody
     @RequestMapping(value="/login")
     public Map<String, Object> login(ModelMap modelMap, HttpSession session) {
      logger.info("login");
      
      Map<String, Object> map = new HashMap<String, Object>();
      
      map.put("success", true);
      map.put("info", "登录成功!");
      
      modelMap.addAttribute("testAjax", "test ajax value");
      session.setAttribute("httpTestAjax", "http test ajax Value");
      
      setValueToSession(modelMap, session, "This is Ajax");
      
      return map;
     }
     
     private void setValueToSession(ModelMap modelMap, HttpSession session, String value) {
      logger.info("set session private");
      modelMap.addAttribute("index", value);
      session.setAttribute("httpIndex", value);
     }
     
     @RequestMapping("/clear2")
     public String clear(SessionStatus status) {
      logger.info("clear session 2");
      status.setComplete();
      return "home";
     }
     
     @RequestMapping(value = "/show2")
     public String show(ModelMap modelMap, HttpSession session) {
      logger.info("show session");
      for (Object key : modelMap.keySet()) {
       Object value = modelMap.get(key);
       System.out.println(key + " = " + value);
      }
      System.out.println("***********************************");
      Enumeration<String> e = session.getAttributeNames();
      while (e.hasMoreElements()) {
       String s = e.nextElement();
       System.out.println(s + " == " + session.getAttribute(s));
      }
      System.out.println("***********************************");
      return "home";
     }
     
    }

    这里如果也是跟我一样用STS建的项目,默认jsp文件会有配置<%@ page session="false" %>,一定要删除或者注释掉。否则无法在页面上展示session中的数据,当然通过我这边写的/show测试直接看后台代码也是可以的。

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    4 <html>
    <head>
     <title>Home</title>
    </head>
    <body>
    <h1>
     Hello world! 
    </h1>
    
    <p> The test1 is ${sessionScope.test1}. </p>
    <p> The test2 is ${sessionScope.test2}. </p>
    <p> The test3 is ${sessionScope.test3}. </p>
    <p> The test4 is ${sessionScope.test4}. </p>
    <p> The test5 is ${sessionScope.test5}. </p>
    <p> The test6 is ${sessionScope.test6}. </p>
    <p> The test7 is ${sessionScope.test7}. </p>
    <p> The index is ${sessionScope.index}. </p>
    <p> The httpIndex is ${sessionScope.httpIndex}. </p>
    
    <br>
    <input type="button" value="test" onclick="test();">
    
    <script src="resources/js/jquery.min.js"></script>
    <script type="text/javascript">
    function test() {
     $.ajax({
      type : "POST",
      url : "login",
      dataType : "json",
      success : function(data) {
       console.log(data);
       window.open("/session/test", "_self");
      },
      error : function() {
       alert("出错了!");
      }
     });
    }
    </script>
    </body>
    </html>

    另外还有一个特别针对@SessionAttributes参数配置的测试代码

    package test.dmh.session.controller;
    
    import java.util.Enumeration;
    
    import javax.servlet.http.HttpSession;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.SessionAttributes;
    import org.springframework.web.bind.support.SessionStatus;
    
    @Controller
    @SessionAttributes(value={"index1", "index2"}, types={String.class, Integer.class})
    public class IndexController3 {
     
     private static final Logger logger = LoggerFactory.getLogger(IndexController3.class);
     
     @RequestMapping("/setIndex")
     public String setSession(ModelMap modelMap) {
      logger.info("set session index");
      modelMap.addAttribute("index1", "aaa");
      modelMap.addAttribute("index2", "bbb");
      modelMap.addAttribute("index2", "ccc");
      modelMap.addAttribute("DDD");
      modelMap.addAttribute("FFF");
      modelMap.addAttribute(22);
      
      return "home";
     }
     
     @RequestMapping(value = "/showIndex")
     public String show(ModelMap modelMap, HttpSession session) {
      logger.info("show session");
      for (Object key : modelMap.keySet()) {
       Object value = modelMap.get(key);
       System.out.println(key + " = " + value);
      }
      System.out.println("***********************************");
      Enumeration<String> e = session.getAttributeNames();
      while (e.hasMoreElements()) {
       String s = e.nextElement();
       System.out.println(s + " == " + session.getAttribute(s));
      }
      System.out.println("***********************************");
      return "home";
     }
     
     @RequestMapping("/clearIndex")
     public String clear(SessionStatus status) {
      logger.info("clear session index");
      status.setComplete();
      return "home";
     }
     
    }

    测试过程简单说明:

    因为参数比较多,所以我也是懒得想名字,序列化的test1、2、3过去了。

    测试的时候就是在浏览器上输入网址:localhost:8080/session/show1

    然后把后缀show1改成别的,比如set1, set2以及clear1, clear2这些,具体的请看我代码中的@RequestMapping配置。

    每次输入set1,set2这些以后,需要输入show1,show2来通过控制台查看session中的内容,当然直接在浏览器上看显示信息也是可以的。

    这边我再说一下主要的几个结论:

    1、使用ModelMap自动保存数据到session必须配置@SessionAttributes注解

    2、使用@SessionAttributes注解只能声明在类上,声明以后,该类中的方法操作session数据只能对@SessionAttributes中配置的参数起作用,包括保存、清除和读取。

    最后还有针对@SessionAttributes中的参数配置得出的几点结论:

    1、配置参数提供value和types,存放的都是数组类型。(只有1个参数时不需要写成数组形式,比如@SessionAttributes(value="test1", types=Integer.class))

    2、使用value配置参数类似于Map的键值对中的key

    3、实用types配置参数后,后台保存的key就是它的类型,个人感觉只有在保存自定义类对象的时候有些用处,比如types=User.class,一般的常用类对象如String等我觉得还是用value的键值对比较好。当然,具体情况还是要具体分析的。

    【相关推荐】

    1.Java免费视频教程

    2.Java实现图片等比例缩略图视频教程

    3.FastJson教程手册

    以上就是简述java中的session用法和细节记录的详细内容,更多请关注php中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:spring mvc session用法
    上一篇:关于java时间计算的算法与优化方法详解 下一篇:简述多线程实现及同步互斥通讯

    相关文章推荐

    • 动态代理的两种方式是什么• JAVA-5NIO之Selector• J2ME程序开发全方位基础讲解汇总(一)• Java基础之类和对象、单例模式、继承• 打造高性能Java应用需掌握的5大知识

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网