jsp 对request.getSession(false)的理解(附程序员常疏忽的一个漏洞)

2016-02-19 09:42 49 1 收藏

今天图老师小编给大家展示的是jsp 对request.getSession(false)的理解(附程序员常疏忽的一个漏洞),精心挑选的内容希望大家多多支持、多多分享,喜欢就赶紧get哦!

【 tulaoshi.com - Web开发 】


在网上经常看到有人对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;

我周围很多同事是这样写的;
代码如下:

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 codenull/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");

来源:https://www.tulaoshi.com/n/20160219/1591789.html

延伸阅读
标签: Web开发
目的:因为blog程序里的某些模块需要用到ajax,直接使用prototype.js体积比较大(40多k),而且仅仅用到其中的ajax功能,因此为了减轻下载的负担,又不能改动已经在prototype.js框架下写好的代码,只能是按照prototype的风格,自己写一个ajax类,达到零成本移植框架。 新的ajax类如下: var Ajax = {xmlhttp:function(){ try{...
到了第二年,大家也都能知道编程语言有好多种的,且各有千秋。而其中比较有前途(钱途?)的是TC。不管怎么着,TC这东西可上可下,可对操作系统编程,可完成汇编的大部分功能。这可让爱捣蛋的男生一提起来就满面红光。动不动就会说,你小子给我小心点。得罪了哥们我,小心我编个病毒KILL了你。呵呵,谁K谁呀。吹起来,谁都不怕谁呢。是的,...
当你看到这个标题的时候肯定回以为是不是作者写错了标题,告诉你没有,学习java语言就是要有创新精神,你只有不断突破前人的你才会有进步。下面我把这一剂良药送给你。 Java作为一门编程语言,最好的学习方法就是写代码。当你学习一个类以后,你就可以自己写个简单的例子程序来运行一下,看看有什么结果,然后再多调用几个类的方法,看看运行...
标签: Web开发
当 jQuery 在2006年1月现身时,给我的第一印象,是这玩意儿构造得很精明。基于CSS选择器(CSS selectors)来打点一切,其思路相当灵巧(参考getElementsBySelector)。但链盒工事(chaining stuff)看起来更像个噱头,并且整体看来,jQuery库提供的功能并不能覆盖所有基础性的东西。因此我断定,jQuery只会昙花一现。 几个月以来,我...
我觉得“做其他的工作,35岁是事业蒸蒸日上,做软件35岁就失业 ”是今天Bird说的一句非常错误的话。真的。 大家千万不要受这种话影响,也不要散布这样的话。否则也许断送不了自己,但是有可能断送中国软件业的将来。 我觉着可以试试讨论,35岁以后,软件人员应该怎样转型而不是消极的谈失业,也太颓废了吧!

经验教程

785

收藏

49
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部