简体   繁体   English

Servlet:HttpServletRequest中的setAttribute与HttpSession中的setAttribute

[英]Servlets: setAttribute in HttpServletRequest vs setAttribute in HttpSession

What is the difference between method setAttribute() of HttpServletRequest class and setAttribute() of HttpSession class? HttpServletRequest类的方法setAttribute()HttpSession类的setAttribute()有什么区别?

Under what circumstances are they used? 他们在什么情况下使用?

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. 当作用域结束时,它的所有属性都将被删除,并且它们在不同的请求或会话中不可用。

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

See also: 也可以看看:

if you use httpServletRequest.setAttribute(); 如果你使用httpServletRequest.setAttribute(); then attribute will be binded to that request object , 那么属性将绑定到该请求对象,

while in httpServletSession.setAttribute(); 而在httpServletSession.setAttribute(); will bind attr. 将绑定attr。 in session. 在会议中。

so if you want the scope of that data to session use session or if you need the scope of that data for just request use request 因此,如果您希望该数据的范围用于会话使用session或者您需要该数据的范围仅用于请求使用request

For Example: 例如:

UserName of logged in user should be shared across session so keep it in session 登录用户的UserName应该在会话中共享,因此请将其保留在session

while, error message you are giving to user while consider authentication failure case, its needed for this request only after that we don't need so keep it in request 虽然,您在考虑身份验证失败的情况下给予用户的错误消息,仅在我们不需要之后才需要此请求,因此请将其保留在request

When you set an attribute on the Request object, the variable is available only on the scope of the request. 在Request对象上设置属性时,该变量仅在请求范围内可用。 That variable can be accessed by other jsp/resources which you forward as part of this request. 该变量可以由您作为此请求的一部分转发的其他jsp /资源访问。

While setting an attribute on session scope will be available to all the requests in the user session (unless you remove it from session). 虽然在会话范围上设置属性将可用于用户会话中的所有请求(除非您从会话中删除它)。

So the major difference it boils down is the scope/life of the attribute. 因此,它归结的主要区别是属性的范围/寿命。

Always try to use request scope variables unless you need to use it across the user session ex: like user roles. 总是尝试使用请求范围变量,除非您需要在用户会话中使用它:例如用户角色。 Keeping more data on the session with more concurrent users may lead to out of memory issues. 使用更多并发用户在会话上保留更多数据可能会导致内存不足问题。 Also if you are using the session sharing backed by a database (like you can do in websphere), it will lead to performance issues. 此外,如果您使用由数据库支持的会话共享(就像您在websphere中所做的那样),则会导致性能问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM