29

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?

so-random-dude
  • 15,277
  • 10
  • 68
  • 113
greenHorn
  • 497
  • 1
  • 5
  • 16

2 Answers2

19

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)."

so-random-dude
  • 15,277
  • 10
  • 68
  • 113
  • - 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?? – Chandan Gupta Oct 28 '21 at 04:13
10

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

Onic Team
  • 1,620
  • 5
  • 26
  • 37
  • Does session here mean the browser session? – Naman Jul 25 '23 at 08:59
  • 1
    @Naman: It's about the server session, server session is stored in the browser, we set the session, update the session and destroyed the session using backend code. – Onic Team Jul 26 '23 at 04:56