使用request.getSession()传输属性值

1. request.getSession():

request.getSession()可以用来存储当前用户对象到session中,或者从session中获取当前用户对象。常用request.getSession()获取对象,不传参数时没获取到session对象会自动创建一个对象并返回。

2. request.getSession(true):

request.getSession(true)这种方式和无参时基本一样,只是增加一个true参数明确告诉它在获取不到的时候自动创建session。

3. request.getSession(false):

request.getSession(false)增加一个参数false,告诉它在获取不到的时候 不进行 自动创建session。

// 发送请求前获取session对象增加AddMessage属性
request.getSession().setAttribute("AddMessage", "保存信息成功");


// 接收请求的位置可以获取到传输过来的属性值使用
model.addAttribute("message", request.getSession().getAttribute("AddMessage"));


// 获取到之后,需要进行删除操作,不然会一直保存
request.getSession().removeAttribute("AddMessage");


  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
request.getSession().setAttribute()是用来在session对象中设置属性的方法。通过这个方法,可以将参数存储在session中,并且可以在多个页面上使用。\[3\]这个方法可以通过sessionID得到自己的session,并且即使重定向客户端也不会丢失。\[3\]在引用\[2\]中的例子中,通过request.getSession().setAttribute("name", str)将一个名为"name"的属性为"huxiwen"。然后通过request.getSession().getAttribute("name")可以获取到这个属性。\[2\]所以,通过request.getSession().setAttribute()方法可以给session对象设置属性。 #### 引用[.reference_title] - *1* *2* [对request.getSession().setAttribute和request.getSession().getAttribute()的理解](https://blog.csdn.net/weixin_42200027/article/details/80482716)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [request.setAttribute()和request.getSession().setAttribute()解释](https://blog.csdn.net/sinat_15274667/article/details/51585538)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值