request.getSession()與getSession(boolean isNew)

HttpRequest對象有兩種形式的getSession方法調用:
一個是getSession(),
另一個是getSession(boolean isNew)

這樣的,前者會檢測當前時候有session存在,如果不存在則創建一個,如果存在就返回當前的。
getSession()相當於getSession(true),

參數爲true時,若存在會話則返回該會話,否則新建一個會話。
參數爲false時,若存在會話則返回該會話,否則應該返回一個NULL

這是文檔:

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  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章