简体   繁体   中英

What's the difference between session.setAttribute and request.setAttribute?

session.setAttributerequest.setAttribute之间有什么区别?

范围,会话属性仅在请求中存在所有会话和请求属性

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.

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.

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.

request.setAttribute() may help you in getting rid of the hidden fields.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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