What is the difference between session.setAttribute and request.setAttribute?

What is the difference between session.setAttribute and request.setAttribute ?

+4
source share
3 answers

Scope, live session attribute, entire session, and request attribute in the request only

+8
source

The difference lies in the area. The Request-scoped attribute is displayed only when processing the current request. The Session attribute is persistent between multiple requests of the same user. Session support mechanisms may vary (the most common of them are based on cookies), but all of them guarantee that the session will be persistent until the user's session remains the same.

+2
source

The request attribute is only available for the lifetime of the request object. filters, servlet, jsp, include, forward uses the same request object. Upon completion of the request, the request object is destroyed.

While session attributes are available until the end of the session or until the browser closes. Therefore, the difference lies in the area.

For example, a stream, for example page1-> page2-> page3-> page4. session.setAttribute will make the key available on all pages. But if we use request.setAttribute on page 2, then only page3 can get the key value set on page2.

request.setAttribute() can help you get rid of hidden fields.

+1
source

Source: https://habr.com/ru/post/1484910/


All Articles