亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

java 中 request.getSession(true、false、null)的區(qū)別

 更新時間:2017年02月17日 16:07:41   投稿:lqh  
這篇文章主要介紹了java 中 request.getSession(true/false/null)的區(qū)別的相關(guān)資料,需要的朋友可以參考下

java 中 request.getSession(true/false/null)的區(qū)別

一、需求原因

現(xiàn)實中我們經(jīng)常會遇到以下3中用法:

HttpSession session = request.getSession();

HttpSession session = request.getSession(true);

HttpSession session = request.getSession(false);

二、區(qū)別

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)意思是返回當(dāng)前reqeust中的HttpSession ,如果當(dāng)前reqeust中的HttpSession 為null,當(dāng)create為true,就創(chuàng)建一個新的Session,否則返回null;

簡而言之:

HttpServletRequest.getSession(ture)等同于 HttpServletRequest.getSession() 
HttpServletRequest.getSession(false)等同于 如果當(dāng)前Session沒有就為null; 

3.      使用

當(dāng)向Session中存取登錄信息時,一般建議:HttpSession session =request.getSession();

當(dāng)從Session中獲取登錄信息時,一般建議:HttpSession session =request.getSession(false);

4.      更簡潔的方式

如果你的項目中使用到了Spring,對session的操作就方便多了。如果需要在Session中取值,可以用WebUtils工具(org.springframework.web.util.WebUtils)的WebUtils.getSessionAttribute(HttpServletRequestrequest, String name);方法,看看源碼:

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是否為空,若為空就拋異常

你使用時:

WebUtils.setSessionAttribute(request, "user", User);

User user = (User)WebUtils.getSessionAttribute(request, "user");

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

  • SpringBoot使用@Validated處理校驗的方法步驟

    SpringBoot使用@Validated處理校驗的方法步驟

    @Validated?注解的主要目的是啟用和利用?Spring?的驗證框架,它可以用于類上也可以用于方法參數(shù)上,本文給大家介紹了SpringBoot使用@Validated優(yōu)雅的處理校驗的方法步驟,通過代碼示例介紹的非常詳細,需要的朋友可以參考下
    2024-08-08
  • Java中批處理框架spring batch詳細介紹

    Java中批處理框架spring batch詳細介紹

    這篇文章主要介紹了Java中批處理框架spring batch詳細介紹,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Java使用Runnable和Callable實現(xiàn)多線程的區(qū)別詳解

    Java使用Runnable和Callable實現(xiàn)多線程的區(qū)別詳解

    這篇文章主要為大家詳細介紹了Java使用Runnable和Callable實現(xiàn)多線程的區(qū)別之處,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起了解一下
    2022-07-07
  • SpringBoot整合Mysql和Redis的詳細過程

    SpringBoot整合Mysql和Redis的詳細過程

    這篇文章主要介紹了SpringBoot整合Mysql和Redis的示例代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-02-02
  • Java 將Excel轉(zhuǎn)為OFD格式(方法步驟)

    Java 將Excel轉(zhuǎn)為OFD格式(方法步驟)

    OFD是一種開放版式文檔是我國國家版式文檔格式標準,本文通過Java后端程序代碼展示如何將Excel轉(zhuǎn)為OFD格式,分步驟給大家介紹的非常詳細,感興趣的朋友一起看看吧
    2021-12-12
  • springboot集成mqtt的實踐開發(fā)

    springboot集成mqtt的實踐開發(fā)

    本篇文章主要介紹了springboot集成mqtt的實踐開發(fā),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • 使用通過ARP類似P2P終結(jié)者實現(xiàn)數(shù)據(jù)封包

    使用通過ARP類似P2P終結(jié)者實現(xiàn)數(shù)據(jù)封包

    目前網(wǎng)絡(luò)上類似P2P終結(jié)者這類軟件,主要都是基于ARP欺騙實現(xiàn)的,網(wǎng)絡(luò)上到處都有關(guān)于ARP的介紹,不過為了本文讀者不需要再去查找,我就在這里大概講解一下
    2012-12-12
  • springboot實現(xiàn)熱部署操作方法

    springboot實現(xiàn)熱部署操作方法

    這篇文章主要介紹了springboot實現(xiàn)熱部署操作方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • java讀取配置文件(properties)的時候,unicode碼轉(zhuǎn)utf-8方式

    java讀取配置文件(properties)的時候,unicode碼轉(zhuǎn)utf-8方式

    這篇文章主要介紹了java讀取配置文件(properties)的時候,unicode碼轉(zhuǎn)utf-8方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • 淺談java中的訪問修飾符

    淺談java中的訪問修飾符

    這篇文章介紹了java中的訪問修飾符,有需要的朋友可以參考一下
    2013-10-10

最新評論