request.getSession(true)和request.getSession(false)的區別


1.轉自:http://wenda.so.com/q/1366414933061950?src=150

概括:

request.getSession(true):若存在會話則返回該會話,否則新建一個會話。

request.getSession(false):若存在會話則返回該會話,否則返回NULL

===========================================================================

2.轉自:http://blog.csdn.net/gaolinwu/article/details/7285783

一、需求原因

現實中我們經常會遇到以下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)的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);
    }
  }
}

三、運行結果

以上例子均測試驗證通過。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM