Narrator: Hey, welcome back.
This is part two on our discussion on Bean Scopes,
so here we'll pick up with Session Scope and Request Scope.
The next scope we have is Session Scope.
Now, this scope is used when you need
to keep track of a user's actions.
So the bean is created once for
the user's browser session, and it's unique for this user.
Again, you want to keep track of
a user's individual actions, so
as an example, a shopping cart,
online banking, or online exam,
here you need to keep track of information
for a given user; a specific user,
not for everyone in the application.
And that's a very practical use of the Session Scope.
Alright, so let's take a look at
a Session Scope code example, so again,
we're gonna return to this form
to interact with the bean, where we have
a counter value and an increment button.
So in the code here for this file:
counter_one.xhtml, we display the counter value,
and then move down to the form,
and we simply have a command button
with the value of Increment, and
then the action here will say counterOne.increment.
So we're gonna call a method on this CounterOne bean.
Okay, so let's go ahead and look at
the source code here for our bean.
So, for our bean, to make use of Session Scope
we add a special annotation on it,
say @SessionScoped, and so here's
our bean called CounterOne, so public class CounterOne.
And we'll have our normal method for increment,
which will simply increment the value
of the counter by one, and will return
the actual name of the page that we wanna show, CounterOne.
So the key here is that annotation
at the beginning: @SessionScoped,
so this bean will be created once
for the user's browser session,
and it's unique for this given user.
Alright, so let's go ahead and take a look at
a code example using the Session Scope.
Now, let's go ahead and take a look
at the Session Scope demo, so again,
moving back into Eclipse, making
use of the same project as before,
bean-demo, I move into the web content directory,
and now I'll take a look at the file counter_one.xhtml.
So this is my example where I'll simply
make use of the Session Scope.
So on line 17, I simply display
the value of the bean at that time, counterOne.value,
on lines 21-24, I have the form
for my increment button, and the action
here is counterOne.increment, so we'll
call the increment method on the CounterOne bean.
Okay, so let's go ahead and look
at the code here for the CounterOne bean.
So, moving up here, CounterOne.java,
let me expand the window here so we can see everything,
and on lines six-eight, that's our Managedbean.
Session Scope CounterOne.
The important thing to notice here
is in line seven, SessionScoped, so
that means that this bean will be created,
and it will be unique for a given user of our application.
So each user will have their own bean
that'll have their own counter value.
On line 10 I set up the value=0,
and lines 12-16 I have the increment method
that we've seen before, so on line 13
we say value++, we simply increment the value by one.
Now let's go ahead and run the application.
I'll move down to my form here,
counter_one.xhtml, I'll right click,
I'll say Run As, Run on Server.
And this will run the application
and it'll show us our Session Scope test page.
Again, I'd like to go ahead and copy this URL,
and I wanna open it up in one of our real browsers.
So here I have Firefox on the left and Chrome on the right.
I'll go ahead and paste the URL into the Firefox browser,
and now I just do an increment,
and so notice here it's incrementing,
and it works out fine as desired.
Now let me do a similar thing,
dropping this URL into the Chrome browser,
and I'll go through, and note here
Counter value is zero,
because this Chrome browser represents a new user,
so each user, or in this case, each browser
has its own Counter value, so we're
using a different browser to simulate a different user,
so it's almost like each user's in,
is accessing our application, with
their own shopping cart, and so they
have their own unique value.
And this is great, so this is primarily
for using it with like a shopping cart,
or an online test, or like online banking.
You wanna make sure the user's actions
are unique for that given user.
Now let's take a look at our next
example using a Request Scope.
So with Request Scope, a new bean
is created for every web request,
so these beans are simply short-lived.
It's commonly used for submitting form data,
and it's really a one-time use,
once you submit the data, it's
used for that one request and then thrown away.
They don't keep track of it over any period of time.
An important thing to note here is that this is
the default Scope for a bean if you don't specify it.
Alright, so let's look at a code example here.
We're gonna start off with our Counter value again,
with our Increment button, and so
on our code here for counter_three.xhtml,
we have the command action: counterThree.increment.
So it'll call the increment method on our counterThree bean.
So here's the code example for this Request Scope.
Here, a new bean's gonna be created for every request.
So we have our bean called CounterThree,
and then, note the new annotation here: @RequestScoped,
so this basically says that this bean
will only be used for this one web request,
and then we'll throw it away.
And again remember, this is the default scope
if you don't explicitly specify one.
Well, let's go ahead and look at a code demo
here of using this Request Scope.
Let's go ahead and move back into Eclipse,
and let's take a look at this Request demo.
So again, we're gonna use the same
project as before: bean-demo.
And I'm gonna look at this file
here called counter_three.xhtml.
So this is the file that we'll use
for our Request Scope Test.
So again, follow the similar format,
on line 17 we simply display the value of the counter,
lines 21-24 is our form, and our
action is counterThree.increment,
so we'll call the increment method on the CounterThree bean.
Now let's go look at that CounterThree bean.
So in my source directory, CounterThree.java,
I'll expand the window here,
lines six-eight is our class counter bean
which is a ManagedBean.
Note: on line seven we have it listed
as RequestScoped, so this bean will only be used
for the current web request, and then thrown away,
so it'll create a new one every time.
So then we move down, say private value=0,
we have our increment method, just like we've seen before,
returns counter_three, and that's pretty much it.
Now the important thing on line seven
is that, if you don't specify a Scope,
by default, it's Request Scope,
so all of the examples that we've used
in the previous videos, we didn't specify a Scope,
so they all defaulted to Request Scope.
So Request Scope is good for using one-time only form data.
Alright, so let's go ahead and run the application.
I'll choose the file counter_three.xhtml,
I'll do a right click, I'll choose Run As,
and I'll choose Run on Server.
And this will fire it up, again we'll see
our information here in the browser,
I'll go ahead and copy this URL,
just like I did before, now I'll
move back to my regular browsers, Chrome and Firefox,
and what I'll do here is I'll just drop
in that URL, so I'll paste the URL into
the Firefox browser, and then I'll just test it out,
I'll hit Increment, and note here,
what's really interesting here is
that the Counter value stays at one,
so it's doesn't keep track of the Counter Value
over multiple web requests, so remember,
the Request Scope, it's a one-time use only,
they use it for one web request and throw it away,
so effectively here, the bean is being thrown away
and starting from scratch every time,
so there's no history that's maintained, nothing per user,
per application, it's simply per web request.
So this is great for sending one-time form data,
when you don't need to send any history.
So most of your standard forms will be Request Scope.
Well great, we can go ahead and wrap up this video.
So in summary, we defined the Scopes for ManagedBeans,
we also made use of the three most common
Scopes: Application Scope, Session Scope, and Request Scope,
and we followed each of them up with a real example.
So this is some really good stuff, great work.