(转)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中 ...
随机推荐
- spark图解
导语 spark 已经成为广告.报表以及推荐系统等大数据计算场景中首选系统,因效率高,易用以及通用性越来越得到大家的青睐,我自己最近半年在接触spark以及spark streaming之后,对spa ...
- 3.java面向对象编程三大特性之多态
面向对象编程的三大特性:封装.继承.多态 封装隐藏了类的内部实现机制,可以在不影响使用的情况下改变类的内部结构,同时也保护了数据.对类的外部而言它的内部实现细节是隐藏的,暴露给外界的只是它的实现方法. ...
- mysql 幻象读
[sql] view plain copy CREATE TABLE `t100` ( `id` bigint(20) NOT NULL default '0', `value` varchar(32 ...
- Excel表格公式大全[转]
Excel技巧网_官方微博 作者: Excel技巧网_官方微博 2016-09-23 14:05:20 举报 阅读数:21219 1.查找重复内容公式:=IF(COUNTIF(A:A,A2)> ...
- node-webkit学习(3)Native UI API概览
node-webkit学习(3)Native UI API概览 文/玄魂 目录 node-webkit学习(3)Native UI API概览 前言 3.1 Native UI api概览 Exte ...
- sql解决避免除以零的错误
在实际项目中,我们可能会遇到求百分比,比值等带除法的sql语句.这时,我们也许会遇到分母为零的情况.下面给出我总结的一些方法: 1. 用NULLIF函数: 首先说一下NULLIF函数的语法: NULL ...
- SignalR 2 入门
在本教程中使用的软件版本 Visual Studio 2015 .NET 4.5 SignalR 版本 2 概述 本教程介绍了通过演示如何生成简单的基于浏览器的聊天应用程序的 SignalR 开发. ...
- win10 开机背景图
地址 C:\Users\*\AppData\Local\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalSta ...
- [leetcode.com]算法题目 - Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- [JSOI2018]列队(主席树)
跟上次那道列队不一样,但都是九条可怜...(吉老师太强了) 在主席树上统计答案,因为值域只有 \(10^6\) 甚至不用离散化... \(Code\ Below:\) #include <bit ...