Narrator: Hey, welcome back.
In this video, I'm gonna show you how to use
managed bean scopes.
So we'll cover the following topics.
We'll first define the scopes for managed beans,
and then we'll discuss application scope.
Next, we'll discuss session scope.
And then finally, we'll wrap it up with the request scope.
Okay, so a lot of good topics here.
Let's get started.
Okay, so what I'm gonna do is cover
the most commonly used scopes.
So we'll start off with the request scope.
Request scope is used on a per-web request.
It's short-lived.
We'll also talk about session scope
that's unique for a given user,
and then we'll also look at application scope.
And this scope is shared by all users.
Now, there are also some other scopes
that are available in JSF,
and I'll list them here on this next slide.
So here's a list of all of the scopes in JSF.
Now, I don't want to overwhelm you
with all this information.
We'll simply cover the first three,
because the first three are the most commonly used:
request, session, and application.
However, I wanted to show you
that there are other scopes available
just for completeness.
However, we won't go into any details
on those additional scopes.
If you would like, you can pause the video here,
and go research those other scopes.
However, we'll use the most commonly used scopes:
request, session, and application.
Now, let's take a look at the first scope here.
Application scope.
Now application scope is a bean that's created once
for all application users,
and it's commonly used to share
an instance of a utility class.
For example, when we get into database development,
it's the primary use for keeping track
of our database utility class.
And we'll actually use it later on in the course
when we get into database development.
However, I will show you a short example
of the application scope in this video,
just so you can see how it works in general.
So in this example, what we'll do
is make use of application scope.
And I want to create a very simple example.
It's gonna have a form that's gonna interact with the bean.
And all it's gonna do
is simply increment the value of a bean,
so the bean will simply have a counter
that will increment the value
each time we click on the form button.
So over in the bottom light here,
we have this counter_two.xhtml.
That's the actual HTML or JSF HTML
that we'll use to render this form.
So we'll have a command button,
we have a value for increment,
and the action is counterTwo.increment.
So it's gonna call it increment method
on our counter two bean.
So now let's take a look at this counter two bean
on the next slide.
So here's our counter two bean.
This bean will have application scope.
So note here, when we settle the coding for it,
we add a new annotation here,
and this is something that's new,
so after managed bean,
we also say @ApplicationScoped.
So what this means is that this bean
will be created once in the application
and shared by all users,
and it will kind of exist
while all users are in there using it.
So it's kind of like global data.
So whenever we call increment,
it's gonna increment the value of this bean.
Again, it's only created once,
and it's shared by all users.
Now let's go ahead and look at a demo here
of the application scope.
So I'll move back into Eclipse.
I'll make use of an existing project
from the previous video, bean-demo,
and again, this code is also available
in the downloads from earlier in the course.
I'll expand the window here for web content.
And I'll take a look at this file here,
counter_two.xhtml.
Now this is just a very simple page
that we're gonna use for testing the application scope.
On line 17, I'm simply gonna print out
the actual value of the counter bean.
And then lines 21 through 24,
I have my command button or my form.
So on line 23, I have action, counterTwo.increment.
So I'm gonna call it the increment method
on my counter two bean.
Okay, so let's go ahead and look
at the actual bean for counter two.
So I move to my Java Resources,
down to source.
I'll choose CounterTwo.java.
I'm gonna go ahead and expand this window here
so I can see more of the code.
So, on lines 6 through 8,
we're gonna have this CounterTwo.
It's a managed bean,
and on line 7, we make it application scope.
So that means that it's gonna be used or shared
by all users of the application.
Application scoped.
On line 10, I have a value that's initialized to zero.
And then here's that actual method
that's called by the form,
so it will simply say CounterTwo.increment.
So on line 13, we have value++,
basically increments the value by one.
It's the same thing as saying value equals value plus one.
Finally, we return student_two,
the name of the actual page
or the form that we want to show.
Well, now let's go ahead and run the application.
So I'll choose this file here, counter_two.xhtml.
And what I'll do is I'll right-click,
and I'll say Run As,
and I'll choose Run on Server.
So once the application runs,
I have the basic page here,
Counter Two Application Scope Test.
What I'd like to do is actually look at this
in a regular web browser.
So I'm simply gonna copy this URL,
just a right-click and choose copy,
and then what I want to do
is open up a FireFox browser and a Chrome browser.
So in my FireFox browser,
I dropped in the actual value here for the URL,
and it's our test,
and I simply hit the increment button a couple times,
and the value's incrementing as desired.
Now let me copy this URL one more time,
and bring up my Chrome browser.
So in the Chrome browser, I'll drop in the URL,
so counter_two.xhtml.
And one thing to note here
is that even in the Chrome browser,
the counter value starts at seven.
Why? Because this bean is being shared application-wide.
So even if I make any changes to that bean,
it's being shared by any user of the application
or all users of the application.
Now let me resize this a bit,
and put it side-by-side,
just we can kind of see it in action.
So just give me one second to line these windows up.
All right, so now I have the windows lined up.
On the left-hand side, I have the FireFox browser.
We can do an increment.
And then over on the right-hand side,
I have the Chrome browser,
so a different browser.
But again, note here,
this bean is being shared by all users,
so note, you make any changes in one browser,
the same change is reflected in the other browser.
So again, the single bean,
multiple users are making use of that bean.
So we'll actually use this for sharing a common object,
such as a database utility class.
And we'll cover that
when we get into the database section of the course.
So there is a valid use case for this.
And we'll see it when we make it
to the database section of the course.
Hey. So this video's going a little bit long,
so what I'm gonna do
is actually break it up into two separate videos.
So we just finished information on application scope.
In the next video, I'm gonna pick up,
and we'll cover session scope and request scope.
So I'll see you in the next video
for session and request scope.