永发信息网

HttpSession session = request.getSession() 与HttpSession session = request.getSession(false)的区别

答案:2  悬赏:80  手机版
解决时间 2021-03-03 23:43
HttpSession session = request.getSession() 与HttpSession session = request.getSession(false)的区别
最佳答案
【官方解释】

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】:

我周围很多同事是这样写的;
view plaincopy to clipboardprint?
HttpSession session = request.getSession(); // a new session created if no session exists, 哈哈!完蛋啦!如果session不存在的话你又创建了一个!
String user_name = session.getAttribute("user_name");
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存在的情况,正常的判断应该是这样:

view plaincopy to clipboardprint?
HttpSession session = request.getSession(false);
if (session != null) {
String user_name = session.getAttribute("user_name");
}
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)方法,看看高手写的源码吧:哈哈。。

view plaincopy to clipboardprint?

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);
}

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是否为空,若为空就抛异常。

上面的代码又可以简洁一下啦,看吧:

view plaincopy to clipboardprint?
HttpSession session = request.getSession(false);
String user_name = WebUtils.getSessionAttribute(reqeust, "user_name");
全部回答
httpsession session=request.getsession();得到的是session内置对象,这个永远不为空,除非服务器禁止session 你应该判断session你你要的内容是否为空 比如session.getattribute("你定义的名字") == null
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
索易快乐成长中心我想知道这个在什么地方
扣篮是怎么练出来的?
【航嘉电源功率计算】怎么计算电源的额定功率
lol多少刀顶一人头的经济
2016年出生的孩子叫什么名字好
月经后期有褐色分泌物正常么?还是黄体功能不
南乡村这个地址在什么地方,我要处理点事
【说明下选择的原因. 】
家园国际酒店怎么去啊,有知道地址的么
橙色的包搭配什么颜色的衣服好看
下兔套套不住老绊套是啥原因
在云南某些干旱灾区,一吨自来水可以卖120元
庞家屋场地址在什么地方,想过去办事
BANKINDONESIA50000元换人民币多少
结合自己的生活,说说灰尘有哪些危害。
推荐资讯
哪有卖对联横批是心若移动如何联通
问道游戏电精、雷怪和雪女哪个宠物好?我是一
怎样将windows phone 10数据转移安卓手机
吕梁学院(离石)和山西师范大学(临汾学院)
地下城与勇士黄色装备能用封腊么?
英文的一个节目,会先播一段视频,接下来让你
为什么我的泰坦之旅没有哈迪斯?
石家庄到清西陵开车怎么走
如图所示,同一水平桌面上放有长方体木块和铁
权志龙飞机头怎么
华为荣耀5x,手机下滑4g图标,突然就没了,在
苹果一个多少钱
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?