﻿WEBVTT

1
00:00:01.326 --> 00:00:03.066
<v Narrator>Hey, welcome back.</v>

2
00:00:03.066 --> 00:00:05.416
In this video, I'm gonna show you how to use

3
00:00:05.416 --> 00:00:07.083
managed bean scopes.

4
00:00:09.390 --> 00:00:11.666
So we'll cover the following topics.

5
00:00:11.666 --> 00:00:15.034
We'll first define the scopes for managed beans,

6
00:00:15.034 --> 00:00:18.235
and then we'll discuss application scope.

7
00:00:18.235 --> 00:00:20.844
Next, we'll discuss session scope.

8
00:00:20.844 --> 00:00:24.055
And then finally, we'll wrap it up with the request scope.

9
00:00:24.055 --> 00:00:25.892
Okay, so a lot of good topics here.

10
00:00:25.892 --> 00:00:27.392
Let's get started.

11
00:00:28.616 --> 00:00:30.260
Okay, so what I'm gonna do is cover

12
00:00:30.260 --> 00:00:32.473
the most commonly used scopes.

13
00:00:32.473 --> 00:00:34.496
So we'll start off with the request scope.

14
00:00:34.496 --> 00:00:36.990
Request scope is used on a per-web request.

15
00:00:36.990 --> 00:00:38.508
It's short-lived.

16
00:00:38.508 --> 00:00:40.216
We'll also talk about session scope

17
00:00:40.216 --> 00:00:42.370
that's unique for a given user,

18
00:00:42.370 --> 00:00:45.360
and then we'll also look at application scope.

19
00:00:45.360 --> 00:00:47.687
And this scope is shared by all users.

20
00:00:47.687 --> 00:00:49.468
Now, there are also some other scopes

21
00:00:49.468 --> 00:00:51.538
that are available in JSF,

22
00:00:51.538 --> 00:00:54.371
and I'll list them here on this next slide.

23
00:00:54.371 --> 00:00:56.994
So here's a list of all of the scopes in JSF.

24
00:00:56.994 --> 00:00:58.701
Now, I don't want to overwhelm you

25
00:00:58.701 --> 00:01:00.095
with all this information.

26
00:01:00.095 --> 00:01:02.061
We'll simply cover the first three,

27
00:01:02.061 --> 00:01:04.295
because the first three are the most commonly used:

28
00:01:04.295 --> 00:01:06.549
request, session, and application.

29
00:01:06.549 --> 00:01:08.403
However, I wanted to show you

30
00:01:08.403 --> 00:01:10.136
that there are other scopes available

31
00:01:10.136 --> 00:01:11.816
just for completeness.

32
00:01:11.816 --> 00:01:13.536
However, we won't go into any details

33
00:01:13.536 --> 00:01:15.218
on those additional scopes.

34
00:01:15.218 --> 00:01:17.251
If you would like, you can pause the video here,

35
00:01:17.251 --> 00:01:19.034
and go research those other scopes.

36
00:01:19.034 --> 00:01:21.488
However, we'll use the most commonly used scopes:

37
00:01:21.488 --> 00:01:24.321
request, session, and application.

38
00:01:25.974 --> 00:01:28.294
Now, let's take a look at the first scope here.

39
00:01:28.294 --> 00:01:30.196
Application scope.

40
00:01:30.196 --> 00:01:32.821
Now application scope is a bean that's created once

41
00:01:32.821 --> 00:01:35.111
for all application users,

42
00:01:35.111 --> 00:01:36.916
and it's commonly used to share

43
00:01:36.916 --> 00:01:39.244
an instance of a utility class.

44
00:01:39.244 --> 00:01:41.996
For example, when we get into database development,

45
00:01:41.996 --> 00:01:44.159
it's the primary use for keeping track

46
00:01:44.159 --> 00:01:46.356
of our database utility class.

47
00:01:46.356 --> 00:01:48.639
And we'll actually use it later on in the course

48
00:01:48.639 --> 00:01:50.427
when we get into database development.

49
00:01:50.427 --> 00:01:52.548
However, I will show you a short example

50
00:01:52.548 --> 00:01:55.468
of the application scope in this video,

51
00:01:55.468 --> 00:01:59.479
just so you can see how it works in general.

52
00:01:59.479 --> 00:02:00.951
So in this example, what we'll do

53
00:02:00.951 --> 00:02:03.351
is make use of application scope.

54
00:02:03.351 --> 00:02:05.620
And I want to create a very simple example.

55
00:02:05.620 --> 00:02:08.586
It's gonna have a form that's gonna interact with the bean.

56
00:02:08.586 --> 00:02:09.447
And all it's gonna do

57
00:02:09.447 --> 00:02:12.280
is simply increment the value of a bean,

58
00:02:12.280 --> 00:02:14.031
so the bean will simply have a counter

59
00:02:14.031 --> 00:02:15.512
that will increment the value

60
00:02:15.512 --> 00:02:17.797
each time we click on the form button.

61
00:02:17.797 --> 00:02:20.548
So over in the bottom light here,

62
00:02:20.548 --> 00:02:23.096
we have this counter_two.xhtml.

63
00:02:23.096 --> 00:02:25.681
That's the actual HTML or JSF HTML

64
00:02:25.681 --> 00:02:28.686
that we'll use to render this form.

65
00:02:28.686 --> 00:02:30.699
So we'll have a command button,

66
00:02:30.699 --> 00:02:32.881
we have a value for increment,

67
00:02:32.881 --> 00:02:36.146
and the action is counterTwo.increment.

68
00:02:36.146 --> 00:02:37.742
So it's gonna call it increment method

69
00:02:37.742 --> 00:02:40.446
on our counter two bean.

70
00:02:40.446 --> 00:02:42.297
So now let's take a look at this counter two bean

71
00:02:42.297 --> 00:02:43.797
on the next slide.

72
00:02:46.823 --> 00:02:48.512
So here's our counter two bean.

73
00:02:48.512 --> 00:02:50.755
This bean will have application scope.

74
00:02:50.755 --> 00:02:53.074
So note here, when we settle the coding for it,

75
00:02:53.074 --> 00:02:55.105
we add a new annotation here,

76
00:02:55.105 --> 00:02:56.123
and this is something that's new,

77
00:02:56.123 --> 00:02:57.639
so after managed bean,

78
00:02:57.639 --> 00:03:00.371
we also say @ApplicationScoped.

79
00:03:00.371 --> 00:03:02.384
So what this means is that this bean

80
00:03:02.384 --> 00:03:05.081
will be created once in the application

81
00:03:05.081 --> 00:03:07.313
and shared by all users,

82
00:03:07.313 --> 00:03:08.511
and it will kind of exist

83
00:03:08.511 --> 00:03:10.370
while all users are in there using it.

84
00:03:10.370 --> 00:03:12.515
So it's kind of like global data.

85
00:03:12.515 --> 00:03:14.181
So whenever we call increment,

86
00:03:14.181 --> 00:03:17.362
it's gonna increment the value of this bean.

87
00:03:17.362 --> 00:03:18.959
Again, it's only created once,

88
00:03:18.959 --> 00:03:21.376
and it's shared by all users.

89
00:03:25.729 --> 00:03:27.309
Now let's go ahead and look at a demo here

90
00:03:27.309 --> 00:03:29.070
of the application scope.

91
00:03:29.070 --> 00:03:30.872
So I'll move back into Eclipse.

92
00:03:30.872 --> 00:03:32.684
I'll make use of an existing project

93
00:03:32.684 --> 00:03:35.156
from the previous video, bean-demo,

94
00:03:35.156 --> 00:03:36.705
and again, this code is also available

95
00:03:36.705 --> 00:03:40.372
in the downloads from earlier in the course.

96
00:03:44.931 --> 00:03:48.598
I'll expand the window here for web content.

97
00:03:49.740 --> 00:03:51.586
And I'll take a look at this file here,

98
00:03:51.586 --> 00:03:53.086
counter_two.xhtml.

99
00:03:55.810 --> 00:03:57.045
Now this is just a very simple page

100
00:03:57.045 --> 00:04:01.267
that we're gonna use for testing the application scope.

101
00:04:01.267 --> 00:04:03.240
On line 17, I'm simply gonna print out

102
00:04:03.240 --> 00:04:06.363
the actual value of the counter bean.

103
00:04:06.363 --> 00:04:08.909
And then lines 21 through 24,

104
00:04:08.909 --> 00:04:11.909
I have my command button or my form.

105
00:04:13.103 --> 00:04:16.969
So on line 23, I have action, counterTwo.increment.

106
00:04:16.969 --> 00:04:18.801
So I'm gonna call it the increment method

107
00:04:18.801 --> 00:04:20.718
on my counter two bean.

108
00:04:24.348 --> 00:04:25.387
Okay, so let's go ahead and look

109
00:04:25.387 --> 00:04:27.662
at the actual bean for counter two.

110
00:04:27.662 --> 00:04:29.966
So I move to my Java Resources,

111
00:04:29.966 --> 00:04:31.856
down to source.

112
00:04:31.856 --> 00:04:34.189
I'll choose CounterTwo.java.

113
00:04:36.406 --> 00:04:38.361
I'm gonna go ahead and expand this window here

114
00:04:38.361 --> 00:04:40.180
so I can see more of the code.

115
00:04:40.180 --> 00:04:42.069
So, on lines 6 through 8,

116
00:04:42.069 --> 00:04:43.750
we're gonna have this CounterTwo.

117
00:04:43.750 --> 00:04:45.360
It's a managed bean,

118
00:04:45.360 --> 00:04:48.965
and on line 7, we make it application scope.

119
00:04:48.965 --> 00:04:50.973
So that means that it's gonna be used or shared

120
00:04:50.973 --> 00:04:53.949
by all users of the application.

121
00:04:53.949 --> 00:04:55.532
Application scoped.

122
00:04:56.643 --> 00:05:00.539
On line 10, I have a value that's initialized to zero.

123
00:05:00.539 --> 00:05:01.739
And then here's that actual method

124
00:05:01.739 --> 00:05:03.227
that's called by the form,

125
00:05:03.227 --> 00:05:05.567
so it will simply say CounterTwo.increment.

126
00:05:05.567 --> 00:05:08.151
So on line 13, we have value++,

127
00:05:08.151 --> 00:05:11.088
basically increments the value by one.

128
00:05:11.088 --> 00:05:14.518
It's the same thing as saying value equals value plus one.

129
00:05:14.518 --> 00:05:16.261
Finally, we return student_two,

130
00:05:16.261 --> 00:05:17.424
the name of the actual page

131
00:05:17.424 --> 00:05:20.174
or the form that we want to show.

132
00:05:26.399 --> 00:05:29.260
Well, now let's go ahead and run the application.

133
00:05:29.260 --> 00:05:33.343
So I'll choose this file here, counter_two.xhtml.

134
00:05:35.449 --> 00:05:37.302
And what I'll do is I'll right-click,

135
00:05:37.302 --> 00:05:39.383
and I'll say Run As,

136
00:05:39.383 --> 00:05:41.883
and I'll choose Run on Server.

137
00:05:49.472 --> 00:05:50.821
So once the application runs,

138
00:05:50.821 --> 00:05:52.217
I have the basic page here,

139
00:05:52.217 --> 00:05:54.977
Counter Two Application Scope Test.

140
00:05:54.977 --> 00:05:56.601
What I'd like to do is actually look at this

141
00:05:56.601 --> 00:05:58.413
in a regular web browser.

142
00:05:58.413 --> 00:06:01.285
So I'm simply gonna copy this URL,

143
00:06:01.285 --> 00:06:04.755
just a right-click and choose copy,

144
00:06:04.755 --> 00:06:05.588
and then what I want to do

145
00:06:05.588 --> 00:06:09.755
is open up a FireFox browser and a Chrome browser.

146
00:06:11.455 --> 00:06:12.766
So in my FireFox browser,

147
00:06:12.766 --> 00:06:16.007
I dropped in the actual value here for the URL,

148
00:06:16.007 --> 00:06:17.582
and it's our test,

149
00:06:17.582 --> 00:06:20.264
and I simply hit the increment button a couple times,

150
00:06:20.264 --> 00:06:22.933
and the value's incrementing as desired.

151
00:06:22.933 --> 00:06:25.309
Now let me copy this URL one more time,

152
00:06:25.309 --> 00:06:27.892
and bring up my Chrome browser.

153
00:06:33.465 --> 00:06:35.920
So in the Chrome browser, I'll drop in the URL,

154
00:06:35.920 --> 00:06:37.670
so counter_two.xhtml.

155
00:06:42.291 --> 00:06:43.124
And one thing to note here

156
00:06:43.124 --> 00:06:44.634
is that even in the Chrome browser,

157
00:06:44.634 --> 00:06:46.373
the counter value starts at seven.

158
00:06:46.373 --> 00:06:50.308
Why? Because this bean is being shared application-wide.

159
00:06:50.308 --> 00:06:52.170
So even if I make any changes to that bean,

160
00:06:52.170 --> 00:06:55.541
it's being shared by any user of the application

161
00:06:55.541 --> 00:06:57.893
or all users of the application.

162
00:06:57.893 --> 00:06:59.625
Now let me resize this a bit,

163
00:06:59.625 --> 00:07:01.090
and put it side-by-side,

164
00:07:01.090 --> 00:07:02.673
just we can kind of see it in action.

165
00:07:02.673 --> 00:07:06.840
So just give me one second to line these windows up.

166
00:07:08.920 --> 00:07:10.667
All right, so now I have the windows lined up.

167
00:07:10.667 --> 00:07:13.397
On the left-hand side, I have the FireFox browser.

168
00:07:13.397 --> 00:07:15.078
We can do an increment.

169
00:07:15.078 --> 00:07:17.383
And then over on the right-hand side,

170
00:07:17.383 --> 00:07:18.769
I have the Chrome browser,

171
00:07:18.769 --> 00:07:20.007
so a different browser.

172
00:07:20.007 --> 00:07:21.611
But again, note here,

173
00:07:21.611 --> 00:07:24.035
this bean is being shared by all users,

174
00:07:24.035 --> 00:07:26.587
so note, you make any changes in one browser,

175
00:07:26.587 --> 00:07:29.110
the same change is reflected in the other browser.

176
00:07:29.110 --> 00:07:31.163
So again, the single bean,

177
00:07:31.163 --> 00:07:33.782
multiple users are making use of that bean.

178
00:07:33.782 --> 00:07:37.293
So we'll actually use this for sharing a common object,

179
00:07:37.293 --> 00:07:40.134
such as a database utility class.

180
00:07:40.134 --> 00:07:41.159
And we'll cover that

181
00:07:41.159 --> 00:07:43.235
when we get into the database section of the course.

182
00:07:43.235 --> 00:07:45.436
So there is a valid use case for this.

183
00:07:45.436 --> 00:07:46.782
And we'll see it when we make it

184
00:07:46.782 --> 00:07:49.949
to the database section of the course.

185
00:07:53.636 --> 00:07:56.530
Hey. So this video's going a little bit long,

186
00:07:56.530 --> 00:07:57.363
so what I'm gonna do

187
00:07:57.363 --> 00:08:00.840
is actually break it up into two separate videos.

188
00:08:00.840 --> 00:08:04.591
So we just finished information on application scope.

189
00:08:04.591 --> 00:08:07.411
In the next video, I'm gonna pick up,

190
00:08:07.411 --> 00:08:10.989
and we'll cover session scope and request scope.

191
00:08:10.989 --> 00:08:12.567
So I'll see you in the next video

192
00:08:12.567 --> 00:08:15.067
for session and request scope.

