java session.setAttribute 和 request.setAttribute 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16971427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between session.setAttribute and request.setAttribute?
提问by ThePhantom05
What's the difference between session.setAttribute
and request.setAttribute
?
什么之间的区别session.setAttribute
和request.setAttribute
?
回答by DGomez
The scope, session attribute live all the session and the request attribute only in a request
scope、session 属性存在于所有的 session 中,而 request 属性只存在于一个请求中
回答by Jk1
Difference lies in the scope. Request-scoped attribute is visible only while current request is processed. Session attribute is persistent between several requests from the same user. Session support mechanisms may differ (the most widespread are cookie based), but all of them guarantee session attrigbute persistence until user's session stays the same.
区别在于范围。请求范围属性仅在处理当前请求时可见。会话属性在来自同一用户的多个请求之间是持久的。会话支持机制可能不同(最普遍的是基于 cookie 的),但它们都保证会话属性持久化,直到用户的会话保持不变。
回答by HRB
Request attribute is only available in the request
object lifetime.
filters, servlet, jsp, include, forward
uses same request object.
Once the request is completed, request object is destroyed.
请求属性仅在request
对象生命周期内可用。
filters, servlet, jsp, include, forward
使用相同的请求对象。一旦请求完成,请求对象就会被销毁。
Whereas session attributes are available till the session ends or till the browser is closed. Hence the difference lies in the scope.
而会话属性在会话结束或浏览器关闭之前一直可用。因此,区别在于范围。
For example, the flow like page1->page2->page3->page4. session.setAttribute
will make the key available in all pages. But if we use request.setAttribute
in page2, then only page3 can get the key value set in page2.
例如,像page1->page2->page3->page4这样的流程。session.setAttribute
将使密钥在所有页面中可用。但是如果我们request.setAttribute
在page2中使用,那么只有page3才能拿到page2中设置的key值。
request.setAttribute()
may help you in getting rid of the hidden fields.
request.setAttribute()
可以帮助您摆脱隐藏的领域。