日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

理解request.getSession(boolean create)

發布時間:2024/4/14 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 理解request.getSession(boolean create) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

為什么80%的碼農都做不了架構師?>>> ??

【前面的話】

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

【官方解釋】

? getSession?

public HttpSession getSession(boolean?create)

Returns the currentHttpSessionassociated with this request or, if if there is no current session andcreateis true, returns a new session.

Ifcreateisfalseand the request has no validHttpSession, this method returnsnull.

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;falseto returnnullif there's no current session

Returns: theHttpSessionassociated with this request ornullifcreateisfalseand 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
  • HttpSession?session?=?request.getSession();???//?a?new?session?created?if?no?session?exists,?哈哈!完蛋啦!如果session不存在的話你又創建了一個!??
  • 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
  • HttpSession?session?=?request.getSession(false);??
  • if?(session?!=?null)?{??
  • ????String?user_name?=?session.getAttribute("user_name");??
  • }??

  • 【投機取巧】:

    如果項目中用到了Spring(其實只要是Java的稍大的項目,Spring是一個很好的選擇),對session的操作就方便多了。如果需要在 Session中取值,可以用WebUtils工具(org.springframework.web.util.WebUtils)的 getSessionAttribute(HttpServletRequest request, String name)方法,看看高手寫的源碼吧:哈哈。。

    [java] view plain copy
  • /**?
  • ?*?Check?the?given?request?for?a?session?attribute?of?the?given?name.?
  • ?*?Returns?null?if?there?is?no?session?or?if?the?session?has?no?such?attribute.?
  • ?*?Does?not?create?a?new?session?if?none?has?existed?before!?
  • ?*?@param?request?current?HTTP?request?
  • ?*?@param?name?the?name?of?the?session?attribute?
  • ?*?@return?the?value?of?the?session?attribute,?or?<code>null</code>?if?not?found?
  • ?*/??
  • 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是否為空,若為空就拋異常。

    上面的代碼又可以簡潔一下啦,看吧:

    [java] view plain copy
  • HttpSession?session?=?request.getSession(false);??
  • String?user_name?=?WebUtils.getSessionAttribute(reqeust,?"user_name");?
  • 轉載自:http://blog.csdn.net/xxd851116/article/details/4296866

    轉載于:https://my.oschina.net/wrean/blog/334915

    總結

    以上是生活随笔為你收集整理的理解request.getSession(boolean create)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。