Servlet:HttpServletRequest中的setAttribute与HttpSession中的setAttribute [英] Servlets: setAttribute in HttpServletRequest vs setAttribute in HttpSession

查看:564
本文介绍了Servlet:HttpServletRequest中的setAttribute与HttpSession中的setAttribute的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HttpServletRequest中的setAttribute()和HttpSession中的setAttribute()之间的区别是什么?他们在什么情况下使用?感谢您的帮助!

whats the difference between setAttribute() in HttpServletRequest and setAttribute() in HttpSession? Under what circumstances are they used? Thanks for any help!

推荐答案

一个在请求范围内设置一个属性,另一个在会话范围内设置一个属性。主要区别在于范围的生命周期。请求范围在关联的响应完成时结束。当会话已被客户端或服务器超时时,会话范围结束。当作用域结束时,它的所有属性都将被删除,并且它们在不同的请求或会话中不可用。

The one sets an attribute in the request scope and the other sets an attribute in the session scope. The major difference is in the lifetime of the scope. The request scope ends when the associated response is finished. The session scope ends when the session has been timed out by the client or server. When a scope ends, then all of its attributes will be trashed and they aren't available in a different request or session.

您使用请求范围来存储数据应该特定于HTTP请求(例如,基于特定请求的数据库结果,成功/错误消息等)。您可以使用会话范围来存储特定于HTTP会话的数据(例如,登录用户,用户设置等)。同一客户端的所有请求共享同一会话(因此,同一客户端会话中的所有不同浏览器选项卡/窗口将共享相同的服务器会话)。

You use the request scope to store data which should be specific to the HTTP request (for example, the database results based on a specific request, the success/error messages, etc). You use the session scope to store data which should be specific to the HTTP session (for example, the logged-in user, user settings, etc). All requests by the same client share the same session (thus, all different browser tabs/windows within the same client session will share the same server session).

  • Servlet instantiation and session variables

这篇关于Servlet:HttpServletRequest中的setAttribute与HttpSession中的setAttribute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆