Reputation: 148
What's the difference between session.setAttribute
and request.setAttribute
?
Upvotes: 7
Views: 22124
Reputation: 23
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.
Upvotes: 2
Reputation: 11463
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.
Upvotes: 3
Reputation: 1480
The scope, session attribute live all the session and the request attribute only in a request
Upvotes: 13