request.getSession(true)和request.getSession()

request.getSession()和request.getSession(true)意思相同:获取session,如果session不存在,就新建一个

reqeust.getSession(false)获取session,如果session不存在,则返回null

如果 项目中无法确定回话一定存在,最好用request.session(false);

getSession(boolean create)意思是返回当前reqeust中的HttpSession ,如果当前reqeust中的HttpSession 为null,当create为true,就创建一个新的Session,否则返回null;
简而言之:
HttpServletRequest.getSession(ture)等同于 HttpServletRequest.getSession()
HttpServletRequest.getSession(false)等同于 如果当前Session没有就为null;


getSession是返回当前用户的会话对象
request.getSession() 与 request.getSession(true) 在效果上没有区别。
request.getSession(true) ,“当前用户的会话对象”为空(第一次访问时)则创建一个新的会话对象返回;
request.getSession(false) ,“当前用户的会话对象”为空,则返回null(即不自动创建会话对象)
request.getSession(boolean create)
如果有与当前的request相关联的HttpSession,那么返回与当前request关联的HttpSession,
如果还没有,那么:
如果 create == true 那么返回一个新建的HttpSession;
如果 create == false,那么返回 null。

HttpSession session = request.setSession(); //建立新的session
session .setAttribute(“A”,”a”);//在session中存值

HttpSession session = request.getSession(true/false);//获取session
session getAttribute(“A”);//获取session中对象

在前台获取session中对象,${A!};

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值