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

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

【前面的話】
在網上經常看到有人對request.getSession(false)提出疑問,我第一次也很迷惑,看了一下J2EE1.3 API,看一下官網是怎么解釋的。
【官方解釋】
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)意思是返回當前reqeust中的HttpSession ,如果當前reqeust中的HttpSession 為null,當create為true,就創建一個新的Session,否則返回null;
簡而言之:
HttpServletRequest.getSession(ture) 等同于 HttpServletRequest.getSession()
HttpServletRequest.getSession(false) 等同于 如果當前Session沒有就為null;
【問題和bug】:
我周圍很多同事是這樣寫的;
復制代碼 代碼如下:
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存在的情況,正常的判斷應該是這樣:
復制代碼 代碼如下:
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)方法,看看高手寫的源碼吧:哈哈。。
復制代碼 代碼如下:
/**
* 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是否為空,若為空就拋異常。
上面的代碼又可以簡潔一下啦,看吧:
復制代碼 代碼如下:
HttpSession session = request.getSession(false);
String user_name = WebUtils.getSessionAttribute(reqeust, "user_name");

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

jsp技術jsp 對request.getSession(false)的理解(附程序員常疏忽的一個漏洞),轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 中文亚洲大香伊蕉不卡一区 | 舔1V1高H糙汉| 暖暖日本大全免费观看 | 欧美在线看欧美视频免费 | 国产成人精品免费视频大全办公室 | 免费网站在线观看国产v片 免费完整版观看 | 最新无码国产在线视频9299 | 色人阁影视| 女性露出奶头流出精子 | AAA级精品无码久久久国片 | 丝袜美女自摸 | 91精品国产色综合久久 | 如懿传免费观看在线全集 | 久久国产精品高清一区二区三区 | 精品亚洲大全 | 国产精品悠悠久久人妻精品 | 秋霞电影伦网理最新在线看片 | 暖暖视频在线高清播放 | 日日噜噜噜噜夜夜爽亚洲精品 | 久久亚洲这里只有精品18 | 东北疯狂xxxxbbbb中国 | 在线高清电影理论片4399 | 久久婷婷久久一区二区三区 | 久久AV国产麻豆HD真实 | 永久免费精品精品永久-夜色 | 99re8久久热在线视频 | 国产网址在线观看 | 久久秋霞理伦片 | 女人被弄到高潮叫床免 | 夜月视频直播免费观看 | 国产高清免费视频免费观看 | 欧美成人无码视频午夜福利 | 成人毛片免费在线观看 | 儿子你得太大了慢点插 | 久久99AV无色码人妻蜜 | 色妹子综合 | 免费小视频在线观看 | 久久99国产综合精品AV蜜桃 | 久久精品国产首叶 | 亚洲一级毛片免费在线观看 | 消息称老熟妇乱视频一区二区 |