what is the difference between request and session scope in spring?

21,894

Solution 1

Your understanding is correct. However I would like to add something

Whereas if the scope is defined as session for the bean, if a user makes a request for a web page more than once, then on every request same bean would be returned.

I would change it as "Whereas if the scope is defined as session for the bean, if a user makes a request for a web page more than once, then on every request same bean would be returned, as long as the requests are within the same user session and made from a client which is capable of maintaining the session (You can't expect the curl to maintain the usersession unless you pass the cookie/session identifier header)."

Solution 2

Session Scope -- when the scope is session,the values of formbean(form data) would be available throughout the session. it will not destroyed until session timeout up or session destroyed.

Request Scope -- when the scope is request,the values of formbean(form data) would be available for the current request. it will refresh on every request of same user/different user.

because http is stateless protocol

Share:
21,894

Related videos on Youtube

greenHorn
Author by

greenHorn

Updated on July 09, 2022

Comments

  • greenHorn
    greenHorn almost 2 years

    In request scope, a bean is defined to an HTTP request whereas in session scope, it is scoped to an HTTP session. So for an instance,

    if the bean scope is request and, a user makes more than one request for a web page in his user session, then on every request a new bean would be created.

    Whereas if the scope is defined as session for the bean, if a user makes a request for a web page more than once, then on every request same bean would be returned.

    please let me know if this understanding is correct?

  • Chandan Gupta
    Chandan Gupta over 2 years
    - by this mean if I am creating a REST based APIs and is not using any session variable - Session Scope and Request Scope becomes same??