天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

jsp 對(duì)request.getSession(false)的理解(附程序員常疏忽的一個(gè)漏洞)

【前面的話】
在網(wǎng)上經(jīng)常看到有人對(duì)request.getSession(false)提出疑問,我第一次也很迷惑,看了一下J2EE1.3 API,看一下官網(wǎng)是怎么解釋的。
【官方解釋】
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
譯:
getSession(boolean create)意思是返回當(dāng)前reqeust中的HttpSession ,如果當(dāng)前reqeust中的HttpSession 為null,當(dāng)create為true,就創(chuàng)建一個(gè)新的Session,否則返回null;
簡而言之:
HttpServletRequest.getSession(ture) 等同于 HttpServletRequest.getSession()
HttpServletRequest.getSession(false) 等同于 如果當(dāng)前Session沒有就為null;
【問題和bug】:
我周圍很多同事是這樣寫的;
復(fù)制代碼 代碼如下:
HttpSession session = request.getSession(); // a new session created if no session exists, 哈哈!完蛋啦!如果session不存在的話你又創(chuàng)建了一個(gè)!
String user_name = session.getAttribute("user_name");

需要注意的地方是request.getSession() 等同于 request.getSession(true),除非我們確認(rèn)session一定存在或者sesson不存在時(shí)明確有創(chuàng)建session的需要,否則盡量使用request.getSession(false)。在使用request.getSession()函數(shù),通常在action中檢查是否有某個(gè)變量/標(biāo)記存放在session中。這個(gè)場景中可能出現(xiàn)沒有session存在的情況,正常的判斷應(yīng)該是這樣:
復(fù)制代碼 代碼如下:
HttpSession session = request.getSession(false);
if (session != null) {
String user_name = session.getAttribute("user_name");
}

【投機(jī)取巧】:

如果項(xiàng)目中用到了Spring(其實(shí)只要是Java的稍大的項(xiàng)目,Spring是一個(gè)很好的選擇),對(duì)session的操作就方便多了。如果需要在Session中取值,可以用WebUtils工具(org.springframework.web.util.WebUtils)的getSessionAttribute(HttpServletRequest request, String name)方法,看看高手寫的源碼吧:哈哈。。
復(fù)制代碼 代碼如下:
/**
* 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工具包中的一個(gè)工具,用來判斷一些驗(yàn)證操作,本例中用來判斷reqeust是否為空,若為空就拋異常。
上面的代碼又可以簡潔一下啦,看吧:
復(fù)制代碼 代碼如下:
HttpSession session = request.getSession(false);
String user_name = WebUtils.getSessionAttribute(reqeust, "user_name");

來源:http://blog.csdn.NET/xxd851116

jsp技術(shù)jsp 對(duì)request.getSession(false)的理解(附程序員常疏忽的一個(gè)漏洞),轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: www.精品久久| 色AV色婷婷96人妻久久久 | 亚洲AV无码国产精品色在线看 | 99热这里有精品 | 国产精品一区二区三区免费 | 色欲AV无码乱码精品国产 | 国产日韩欧美有码在线视频 | 1788vv视频| 日韩一级精品久久久久 | 亚洲熟女丰满多毛XXXXX | 免费在线a | 色偷偷综合网 | 校花在公车上被内射好舒服 | 日本久久免费大片 | 午夜国产免费视频亚洲 | 最好看中文字幕国语 | 亚洲 自拍 偷拍 另类综合图区 | 99热婷婷国产精品综合 | 国产精品熟女人妻 | 久久无码人妻中文国产 | 欧美性情video sexo视频 | 国产亚洲视频在线 | 97无码人妻精品1国产精东影业 | 亚洲AV久久久噜噜噜久久 | 亚洲精品国产AV成人毛片 | 777久久人妻少妇嫩草AV蜜桃 | 果冻传媒2021在线观看 | 久久视频这里只精品99re8久 | 国产成人精品123区免费视频 | 打扑克床上视频不用下载免费观看 | 亚洲青青青网伊人精品 | 亲胸揉胸膜下刺激视频网站APP | 亚洲一区国产 | 精品国产免费第一区二区 | 在线免费视频a | 97在线视频免费人妻 | 肉动漫h黄动漫日本免费观看 | 国产精品自在在线午夜蜜芽tv在线 | 亚洲中文 字幕 国产 综合 | 国产精品成人观看视频免费 | 无码专区aaaaaa免费视频 |