getSession(boolean true) getSession getSession(boolean false) 的关系和区别

 在网上常常看到有人对request.getSession(false)提出疑问,我第一次也很迷惑,看了一下J2EE1.3 API,看一下官网是怎么解释的。 html

【官方解释】java

  getSession web

public HttpSession getSession(boolean create)spring

Returns the current HttpSession associated with this request or, if if there is no current session and create is true, returns a new session.cookie

If create is false and the request has no valid HttpSession, this method returns null.session

To make sure the session is properly maintained, you must call this method before the response is committed. If the container is using cookies to maintain session integrity and is asked to create a new session when the response is committed, an IllegalStateException is thrown.app

Parameters: true - to create a new session for this request if necessary; false to return null if there's no current sessionide

Returns: the HttpSession associated with this request or null if create is false and the request has no valid session函数

译:工具

getSession(boolean create)意思是返回当前reqeust中的HttpSession ,若是当前reqeust中的HttpSession 为null,当create为true,就建立一个新的Session,不然返回null;

简而言之:

HttpServletRequest.getSession(ture) 等同于 HttpServletRequest.getSession()

HttpServletRequest.getSession(false) 等同于 若是当前Session没有就为null;

 

【问题和bug】:

我周围不少同事是这样写的;

[java]  view plain copy
  1. HttpSession session = request.getSession();   // a new session created if no session exists, 哈哈!完蛋啦!若是session不存在的话你又建立了一个!  
  2. String user_name = session.getAttribute("user_name");  

 

须要注意的地方是request.getSession() 等同于 request.getSession(true),除非咱们确认session必定存在或者sesson不存在时明确有建立session的须要,不然尽可能使用request.getSession(false)。在使用request.getSession()函数,一般在action中检查是否有某个变量/标记存放在session中。这个场景中可能出现没有session存在的状况,正常的判断应该是这样:

 

[java]  view plain copy
  1. HttpSession session = request.getSession(false);  
  2. if (session != null) {  
  3.     String user_name = session.getAttribute("user_name");  
  4. }  

 


【投机取巧】:

若是项目中用到了Spring(其实只要是Java的稍大的项目,Spring是一个很好的选择),对session的操做就方便多了。若是须要在Session中取值,能够用WebUtils工具(org.springframework.web.util.WebUtils)的getSessionAttribute(HttpServletRequest request, String name)方法,看看高手写的源码吧:哈哈。。

 

[java]  view plain copy
  1. /** 
  2.  * Check the given request for a session attribute of the given name. 
  3.  * Returns null if there is no session or if the session has no such attribute. 
  4.  * Does not create a new session if none has existed before! 
  5.  * @param request current HTTP request 
  6.  * @param name the name of the session attribute 
  7.  * @return the value of the session attribute, or <code>null</code> if not found 
  8.  */  
  9. public static Object getSessionAttribute(HttpServletRequest request, String name) {  
  10.     Assert.notNull(request, "Request must not be null");  
  11.     HttpSession session = request.getSession(false);  
  12.     return (session != null ? session.getAttribute(name) : null);  
  13. }  

 

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

上面的代码又能够简洁一下啦,看吧:

 

[java]  view plain copy
  1. HttpSession session = request.getSession(false);  
  2. String user_name = WebUtils.getSessionAttribute(reqeust, "user_name");  
相关文章
相关标签/搜索