一、三种情况

HttpSession session = request.getSession();
HttpSession session = request.getSession(true);
HttpSession session = request.getSession(false);

二、三种情况之间的差异

  getSession(boolean create)意思是返回当前reqeust中的HttpSession ,如果当前reqeust中的HttpSession 为null,当create为true,就创建一个新的Session,否则返回null; 

  简而言之: 

  HttpServletRequest.getSession(ture)等同于 :HttpServletRequest.getSession() ,若存在会话则返回该会话,否则创建一个新的会话Session

  HttpServletRequest.getSession(false):若存在会话则返回该会话,否则返回NULL

  

三、具体的使用场景

  当向Session中存入登录信息时,一般建议:HttpSession session =request.getSession();

  当从Session中获取登录信息时,一般建议:HttpSession session =request.getSession(false);

四、更简洁的方式

  如果你的项目中使用到了Spring(当然大点的项目都用到了),对session的操作就方便多了。如果需要在Session中取值,可以用WebUtils工具(org.springframework.web.util.WebUtils)的getSessionAttribute(HttpServletRequestrequest, String name)方法,看看源码:

publicstatic Object getSessionAttribute(HttpServletRequest request, String name){
  Assert.notNull(request, "Request must not be null");
  HttpSession session =request.getSession(false);
  return (session != null ?session.getAttribute(name) : null);
}

  注:Assert是Spring工具包中的一个工具,用来判断一些验证操作,本例中用来判断reqeust是否为空,若为空就抛异常

  你使用时:WebUtils.setSessionAttribute(request, “user”, User);

      User user = (User)WebUtils.getSessionAttribute(request, “user”);

  源码:

/**
* Set the session attribute with the given name to the given value.
* Removes the session attribute if value is null, if a session existed at all.
* Does not create a new session if not necessary!
* @param request current HTTP request
* @param name the name of the session attribute
*/
public static void setSessionAttribute(HttpServletRequest request, String name, Object value) {
  if (value != null) {
    request.getSession().setAttribute(name, value);
  } else {
    HttpSession session = request.getSession(false);
    if (session != null) {
      session.removeAttribute(name);
    }
  }
}

(转)request.getSession()几种获取情况之间的差异的更多相关文章

  1. request.getSession()几种获取情况之间的差异

    一.三种情况如下 HttpSession session = request.getSession(); HttpSession session = request.getSession(true); ...

  2. request.getSession().getServletContext().getRealPath("")获取工程目录 路径修改

    使用request.getSession().getServletContext().getRealPath("")获取工程目录. 设置server Locations在serve ...

  3. java获取日期之间的差异

    转载请注明出处.谢谢http://blog.csdn.net/harryweasley/article/details/42121485 当想到要计算差值.我们肯定想的是"2014.12.1 ...

  4. request.getSession(true/false)的区别

    javax.servlet.http.HttpServletRequest接口有两个方法:getSession(boolean)和getSession(). 具体什么区别,跟踪源码分析下,先摆出结论: ...

  5. Spring中获取request的几种方法,及其线程安全性分析

    前言 本文将介绍在Spring MVC开发的web系统中,获取request对象的几种方法,并讨论其线程安全性. 原创不易,如果觉得文章对你有帮助,欢迎点赞.评论.文章有疏漏之处,欢迎批评指正. 欢迎 ...

  6. 【转】SpringMVC,获取request的几种方法,及线程安全性

    作者丨编程迷思 https://www.cnblogs.com/kismetv/p/8757260.html 概述 在使用Spring MVC开发Web系统时,经常需要在处理请求时使用request对 ...

  7. [No000016E]Spring 中获取 request 的几种方法,及其线程安全性分析

    前言 本文将介绍在Spring MVC开发的web系统中,获取request对象的几种方法,并讨论其线程安全性. 原创不易,如果觉得文章对你有帮助,欢迎点赞.评论.文章有疏漏之处,欢迎批评指正. 欢迎 ...

  8. Spring中获取request的几种方法,及其线程安全性分析(山东数漫江湖)

    前言 本文将介绍在Spring MVC开发的web系统中,获取request对象的几种方法,并讨论其线程安全性. 原创不易,如果觉得文章对你有帮助,欢迎点赞.评论.文章有疏漏之处,欢迎批评指正. 欢迎 ...

  9. Struts2获取request的几种方式汇总

    Struts2获取request三种方法 struts2里面有三种方法可以获取request,最好使用ServletRequestAware接口通过IOC机制注入Request对象. 在Action中 ...

随机推荐

  1. CF每日一题系列 —— 415A

    http://codeforces.com/problemset/page/7?order=BY_SOLVED_DESC 从5000以内选的,emmm还是比较水的哈 时间还是有的,所以万事万物贵在坚持 ...

  2. handsontable 排序问题

    排序是表格的基础功能,handsontable也会支持. 有时需求会很复杂,需要自定义排序,或者调用其他排序方法:自定义排序,比较复杂,没做过:今天要用的是调用R中的排序方法. 有两个事件before ...

  3. iOS笔记之UIKit_UINavigationController

    //设置导航条的样式 self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent; //默认是白色  B ...

  4. php支持连接sqlserver数据库

    php支持连接sqlserver数据库 1.软件配置 Win7 64 +wampserver2.2d-x32+SQL Server 2008 R2数据库,wamp2.2中的php版本是5.3.10. ...

  5. [LintCode] Longest Increasing Continuous subsequence

    http://www.lintcode.com/en/problem/longest-increasing-continuous-subsequence/# Give you an integer a ...

  6. .net MVC, webAPI,webForm集成steeltoe+springcloud实现调用服务中心服务的总结

    开始之前,如果没接触过Autofac的,可以移步到Autofac官方示例学习一下怎么使用:https://github.com/autofac/Examples .net 下集成steeltoe进行微 ...

  7. Python 高级编程——单例模式

    单例模式(Singleton Pattern)是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在.当你希望在整个系统中,某个类只能出现一个实例时,单例对象就能派上用场. 在 Py ...

  8. [leetcode.com]算法题目 - Plus One

    Given a number represented as an array of digits, plus one to the number. class Solution { public: v ...

  9. Flask中的before_request和after_request

    1.@app.before_request 在请求(request)之前做出响应 @app.before_request 也是一个装饰器,他所装饰的函数,都会在请求进入视图函数之前执行 2.@app. ...

  10. CodeForces - 940C + CodeForces - 932B (两道比较好的模拟题)

    940C链接:http://codeforces.com/problemset/problem/940/C C. Phone Numbers time limit per test 2 seconds ...