(转)request.getSession()几种获取情况之间的差异
一、三种情况
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()几种获取情况之间的差异的更多相关文章
- request.getSession()几种获取情况之间的差异
一.三种情况如下 HttpSession session = request.getSession(); HttpSession session = request.getSession(true); ...
- request.getSession().getServletContext().getRealPath("")获取工程目录 路径修改
使用request.getSession().getServletContext().getRealPath("")获取工程目录. 设置server Locations在serve ...
- java获取日期之间的差异
转载请注明出处.谢谢http://blog.csdn.net/harryweasley/article/details/42121485 当想到要计算差值.我们肯定想的是"2014.12.1 ...
- request.getSession(true/false)的区别
javax.servlet.http.HttpServletRequest接口有两个方法:getSession(boolean)和getSession(). 具体什么区别,跟踪源码分析下,先摆出结论: ...
- Spring中获取request的几种方法,及其线程安全性分析
前言 本文将介绍在Spring MVC开发的web系统中,获取request对象的几种方法,并讨论其线程安全性. 原创不易,如果觉得文章对你有帮助,欢迎点赞.评论.文章有疏漏之处,欢迎批评指正. 欢迎 ...
- 【转】SpringMVC,获取request的几种方法,及线程安全性
作者丨编程迷思 https://www.cnblogs.com/kismetv/p/8757260.html 概述 在使用Spring MVC开发Web系统时,经常需要在处理请求时使用request对 ...
- [No000016E]Spring 中获取 request 的几种方法,及其线程安全性分析
前言 本文将介绍在Spring MVC开发的web系统中,获取request对象的几种方法,并讨论其线程安全性. 原创不易,如果觉得文章对你有帮助,欢迎点赞.评论.文章有疏漏之处,欢迎批评指正. 欢迎 ...
- Spring中获取request的几种方法,及其线程安全性分析(山东数漫江湖)
前言 本文将介绍在Spring MVC开发的web系统中,获取request对象的几种方法,并讨论其线程安全性. 原创不易,如果觉得文章对你有帮助,欢迎点赞.评论.文章有疏漏之处,欢迎批评指正. 欢迎 ...
- Struts2获取request的几种方式汇总
Struts2获取request三种方法 struts2里面有三种方法可以获取request,最好使用ServletRequestAware接口通过IOC机制注入Request对象. 在Action中 ...
随机推荐
- Codeforces Beta Round #27 E. Number With The Given Amount Of Divisors 含n个约数最小数
http://codeforces.com/problemset/problem/27/E RT,求含n个约数的最小的数 我们设答案p = 2^t1 * 3^t2 * -- * p^tk(其中p是第k ...
- 2015年 10月最新苹果IOS上架App Store商店步骤
1.1.前期工作 首先你需要有一个苹果的开发者帐号,一个Mac系统. 如果没有帐号可以在打开http://developer.apple.com/申请加入苹果的开发者计划.支付99美元每年,怎么申请网 ...
- Python--随机生成指定长度的密码
在浏览别人博客时学习了random模块,手痒自我练习下,写个随机生成指定长度的密码字符串的函数,拿出来供各位参考: 废话不多说,上代码: # coding: utf-8 import random i ...
- php file_get_contents fopen 连接远程文件
使用file_get_contents和fopen必须空间开启allow_url_fopen. 方法: 编辑php.ini,设置allow_url_fopen =true On,allow_url_f ...
- cesiumjs
官网 https://cesium.com/ https://cesiumjs.org/Cesium/ 论坛 http://cesium.coinidea.com/ 中文网 http://cesium ...
- Weblogic 错误 <BEA-000403> <BEA-000438>解决办法
控制台提示如下错误: <Error> <Socket> <BEA-000438> <Unable to load performance pack. Us ...
- .NET Core 从1.1升级到2.0记录(Cookie中间件踩坑)
.NET Core 2.0 新时代 万众瞩目的.NET Core 2.0终于发布了,原定于9.19的dotnetconf大会的发布时间大大提前了1个月,.NET Core 2.0/.NET Stand ...
- [ActionScript 3.0] 有必要记录一下:flash builder运用Animate CC 发布的swc的一个奇葩问题,亲测
之前一直用flash cs6 发布swc 配合flash builder4.6开发,最近用Animate CC发布swc,却出现无法flash builder4.6 无法连接到调试器的问题, 经过反复 ...
- css 的 conic-gradient 学习
偶然间在微信公众号奇舞周刊上看到了这篇文章<CSS Painting API>,算是对 conic-gradient的初次见面. 后来有空的时候,百度搜了一下,看了这篇文章<CSS神 ...
- POJ 2696
#include<iostream> #define MAXN 1005 #include<stdio.h> using namespace std; int _m[MAXN] ...