欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間: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)實(shí)中我們經(jīng)常會(huì)遇到以下3中用法:

HttpSession session = request.getSession();

HttpSession session = request.getSession(true);

HttpSession session = request.getSession(false);

二、區(qū)別

1.      Servlet官方文檔說(shuō):

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.      翻譯過(guò)來(lái)的意思是:

getSession(boolean create)意思是返回當(dāng)前reqeust中的HttpSession ,如果當(dāng)前reqeust中的HttpSession 為null,當(dāng)create為true,就創(chuàng)建一個(gè)新的Session,否則返回null;

簡(jiǎn)而言之:

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

3.      使用

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

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

4.      更簡(jiǎn)潔的方式

如果你的項(xiàng)目中使用到了Spring,對(duì)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工具包中的一個(gè)工具,用來(lái)判斷一些驗(yàn)證操作,本例中用來(lái)判斷reqeust是否為空,若為空就拋異常

你使用時(shí):

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

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

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

相關(guān)文章

  • SpringBoot使用@Validated處理校驗(yàn)的方法步驟

    SpringBoot使用@Validated處理校驗(yàn)的方法步驟

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

    Java中批處理框架spring batch詳細(xì)介紹

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

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

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

    SpringBoot整合Mysql和Redis的詳細(xì)過(guò)程

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

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

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

    springboot集成mqtt的實(shí)踐開(kāi)發(fā)

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

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

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

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

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

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

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

    淺談java中的訪(fǎng)問(wèn)修飾符

    這篇文章介紹了java中的訪(fǎng)問(wèn)修飾符,有需要的朋友可以參考一下
    2013-10-10

最新評(píng)論