java 中 request.getSession(true、false、null)的區(qū)別
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沒有就為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)文章
使用Apache Ignite實(shí)現(xiàn)Java數(shù)據(jù)網(wǎng)格
今天我們來(lái)探討如何使用Apache Ignite來(lái)實(shí)現(xiàn)Java數(shù)據(jù)網(wǎng)格,Apache Ignite是一個(gè)高性能的內(nèi)存計(jì)算平臺(tái),它提供了分布式緩存、數(shù)據(jù)網(wǎng)格和計(jì)算功能,可以顯著提高大規(guī)模應(yīng)用的數(shù)據(jù)處理性能,感興趣的小伙伴跟著小編一起來(lái)看看吧2024-08-08
java實(shí)現(xiàn)二維碼生成功能詳細(xì)示例
這篇文章主要給大家介紹了關(guān)于java實(shí)現(xiàn)二維碼生成功能的相關(guān)資料,隨著信息化時(shí)代的到來(lái),二維碼作為一種信息傳遞的工具,越來(lái)越受到人們的歡迎,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07
Monaco?Editor實(shí)現(xiàn)sql和java代碼提示實(shí)現(xiàn)示例
這篇文章主要為大家介紹了Monaco?Editor代碼提示sql和java實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
Http Cookie機(jī)制及Cookie的實(shí)現(xiàn)原理
Cookie是進(jìn)行網(wǎng)站用戶身份,實(shí)現(xiàn)服務(wù)端Session會(huì)話持久化的一種非常好方式。Cookie最早由Netscape公司開發(fā),現(xiàn)在由 IETF 的RFC 6265標(biāo)準(zhǔn)備對(duì)其規(guī)范,已被所有主流瀏覽器所支持2021-06-06
SpringMVC如何獲取表單數(shù)據(jù)(radio和checkbox)
這篇文章主要介紹了SpringMVC如何獲取表單數(shù)據(jù)(radio和checkbox)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
MyEclipse 2016 CI 4新增BootStrap模板
MyEclipse2016是一款全球使用最為廣泛的企業(yè)級(jí)開發(fā)環(huán)境程序,這篇文章主要介紹了MyEclipse 2016 CI 4新增BootStrap模板的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06

