request.getSession(false) java

本文屬於本人原創,轉載請註明出處:http://blog.csdn.net/xxd851116/archive/2009/06/25/4296866.aspx

【前面的話】

在網上經常看到有人對request.getSession(false)提出疑問,我第一次也很迷惑,看了一下J2EE1.3 API,看一下官網是怎麼解釋的。 

【官方解釋】

  getSession 

public HttpSession getSession(boolean create)

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

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

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.

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

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 plaincopy
  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 plaincopy
  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 plaincopy
  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 plaincopy
  1. HttpSession session = request.getSession(false);  
  2. String user_name = WebUtils.getSessionAttribute(reqeust, "user_name");  

發佈了71 篇原創文章 · 獲贊 7 · 訪問量 57萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章