java 中 request.getSession(true/false/null)的区别
一、需求原因
现实中我们经常会遇到以下3中用法:
HttpSession session = request.getSession();
HttpSession session = request.getSession(true);
HttpSession session = request.getSession(false);
二、区别
1. Servlet官方文档说:
public HttpSessiongetSession(boolean create)
Returns the currentHttpSession associated with this request or, if if there is no current sessionand create is true, returns a new session.
If create is falseand the request has no valid HttpSession, this method returns null.
To make sure thesession is properly maintained, you must call this method before the responseis committed. If the Container is using cookies to maintain session integrityand is asked to create a new session when the response is committed, anIllegalStateException is thrown.
Parameters: true -to create a new session for this request if necessary; false to return null ifthere's no current session
Returns: theHttpSession associated with this request or null if create is false and therequest has no valid session
2. 翻译过来的意思是:
getSession(boolean create)意思是返回当前reqeust中的HttpSession ,如果当前reqeust中的HttpSession 为null,当create为true,就创建一个新的Session,否则返回null;
简而言之:
HttpServletRequest.getSession(ture)等同于 HttpServletRequest.getSession() HttpServletRequest.getSession(false)等同于 如果当前Session没有就为null;
3. 使用
当向Session中存取登录信息时,一般建议:HttpSession session =request.getSession();
当从Session中获取登录信息时,一般建议:HttpSession session =request.getSession(false);
4. 更简洁的方式
如果你的项目中使用到了Spring,对session的操作就方便多了。如果需要在Session中取值,可以用WebUtils工具(org.springframework.web.util.WebUtils)的WebUtils.getSessionAttribute(HttpServletRequestrequest, String name);方法,看看源码:
public static 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");
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
问题内容: 这些通话实际上对会话意味着什么? 输出值 问题答案: 在服务方法内部,我们请求会话,并且所有事情都会自动获取,就像创建HttpSession对象一样。无需生成唯一的会话ID。无需创建新的Cookie对象。一切都会在后台自动发生。 调用请求对象的方法后,容器将立即创建会话的新对象,并生成一个唯一的会话ID来维护会话。该会话ID被传输回响应对象,以便每当客户端发出任何请求时,它也应将会话I
问题内容: 在Java2D中,当您使用setOpaque时,我对true和false的含义有些困惑。 例如,我知道在Swing中,不透明意味着在绘制Swing时不会绘制组件后面的内容。还是倒退?哪一个? 谢谢 问题答案: 对于您的问题的简短回答是,“不透明”在英语中被定义为完全不透明。因此,不透明的组件是一个绘制其整个矩形的组件,并且每个像素都不完全透明。 但是,Swing组件不透明性API是设计
问题内容: 当我们在Django中添加数据库字段时,通常会这样写: 同样是与做,等有什么根本区别在其 null=True only blank=True only null=True, blank=True 在相对于不同的字段。使用1/2/3有什么优点/缺点? 问题答案: 在数据库的列中设置NULL(与相对)。字段类型(例如或)的空白值将存储在数据库中。 blank确定是否需要表单中的字段。这包括
问题内容: 当我们在Django中添加数据库字段时,通常会这样写: 同样是与做等有什么根本区别在其 null=True only blank=True only null=True, blank=True 在相对于不同的()字段。使用1/2/3有什么优点/缺点? 问题答案: 在数据库的列中设置(与相对)。字段类型(例如或)的空白值将存储NULL在数据库中。 确定是否需要表单中的字段。这包括管理员和
问题内容: 如果我这样做: 那又回来了。仅仅是因为在列表中。 但是,如果我这样做: 那又回来了。而等于: 为什么? 问题答案: 运算符优先级 2.x,3.x。的优先级低于的优先级。因此,它等效于: 这就是你想要的: 正如@Ben指出的那样:建议从不写作,更喜欢。前者使它看起来像一个函数调用,而它却是一个运算符,而不是一个函数。
问题内容: 我试过运行这段代码: 并输出 False 。我认为Python会将任何有价值的东西都视为 True 。为什么会这样呢? 问题答案: 从 6.11开始。 布尔运算: 在布尔运算的上下文中,以及当控制流语句使用表达式时,以下值将解释为false:False,None,所有类型的数字零以及空字符串和容器(包括字符串,元组,列表,字典) ,集合和Frozensets)。所有其他值均解释为tru
为什么当使用括号时,这些语句会像预期的那样工作: 但是当没有括号时,它返回?