﻿WEBVTT

1
00:00:00.613 --> 00:00:03.198
<v Narrator>Hey, welcome back.</v>

2
00:00:03.198 --> 00:00:05.863
This is part two on our discussion on Bean Scopes,

3
00:00:05.863 --> 00:00:10.030
so here we'll pick up with Session Scope and Request Scope.

4
00:00:12.624 --> 00:00:15.102
The next scope we have is Session Scope.

5
00:00:15.102 --> 00:00:16.834
Now, this scope is used when you need

6
00:00:16.834 --> 00:00:19.110
to keep track of a user's actions.

7
00:00:19.110 --> 00:00:20.740
So the bean is created once for

8
00:00:20.740 --> 00:00:23.991
the user's browser session, and it's unique for this user.

9
00:00:23.991 --> 00:00:25.569
Again, you want to keep track of

10
00:00:25.569 --> 00:00:27.777
a user's individual actions, so

11
00:00:27.777 --> 00:00:29.992
as an example, a shopping cart,

12
00:00:29.992 --> 00:00:32.524
online banking, or online exam,

13
00:00:32.524 --> 00:00:34.582
here you need to keep track of information

14
00:00:34.582 --> 00:00:37.385
for a given user; a specific user,

15
00:00:37.385 --> 00:00:40.537
not for everyone in the application.

16
00:00:40.537 --> 00:00:44.704
And that's a very practical use of the Session Scope.

17
00:00:45.678 --> 00:00:46.968
Alright, so let's take a look at

18
00:00:46.968 --> 00:00:49.867
a Session Scope code example, so again,

19
00:00:49.867 --> 00:00:51.816
we're gonna return to this form

20
00:00:51.816 --> 00:00:53.474
to interact with the bean, where we have

21
00:00:53.474 --> 00:00:56.340
a counter value and an increment button.

22
00:00:56.340 --> 00:00:58.334
So in the code here for this file:

23
00:00:58.334 --> 00:01:02.341
counter_one.xhtml, we display the counter value,

24
00:01:02.341 --> 00:01:04.382
and then move down to the form,

25
00:01:04.382 --> 00:01:06.270
and we simply have a command button

26
00:01:06.270 --> 00:01:08.119
with the value of Increment, and

27
00:01:08.119 --> 00:01:11.639
then the action here will say counterOne.increment.

28
00:01:11.639 --> 00:01:14.675
So we're gonna call a method on this CounterOne bean.

29
00:01:14.675 --> 00:01:16.081
Okay, so let's go ahead and look at

30
00:01:16.081 --> 00:01:18.128
the source code here for our bean.

31
00:01:18.128 --> 00:01:20.501
So, for our bean, to make use of Session Scope

32
00:01:20.501 --> 00:01:22.660
we add a special annotation on it,

33
00:01:22.660 --> 00:01:25.326
say @SessionScoped, and so here's

34
00:01:25.326 --> 00:01:29.301
our bean called CounterOne, so public class CounterOne.

35
00:01:29.301 --> 00:01:31.516
And we'll have our normal method for increment,

36
00:01:31.516 --> 00:01:33.423
which will simply increment the value

37
00:01:33.423 --> 00:01:35.381
of the counter by one, and will return

38
00:01:35.381 --> 00:01:38.380
the actual name of the page that we wanna show, CounterOne.

39
00:01:38.380 --> 00:01:40.336
So the key here is that annotation

40
00:01:40.336 --> 00:01:42.350
at the beginning: @SessionScoped,

41
00:01:42.350 --> 00:01:43.889
so this bean will be created once

42
00:01:43.889 --> 00:01:45.670
for the user's browser session,

43
00:01:45.670 --> 00:01:48.820
and it's unique for this given user.

44
00:01:48.820 --> 00:01:50.818
Alright, so let's go ahead and take a look at

45
00:01:50.818 --> 00:01:54.068
a code example using the Session Scope.

46
00:01:55.322 --> 00:01:56.749
Now, let's go ahead and take a look

47
00:01:56.749 --> 00:01:59.003
at the Session Scope demo, so again,

48
00:01:59.003 --> 00:02:00.864
moving back into Eclipse, making

49
00:02:00.864 --> 00:02:02.729
use of the same project as before,

50
00:02:02.729 --> 00:02:05.748
bean-demo, I move into the web content directory,

51
00:02:05.748 --> 00:02:09.915
and now I'll take a look at the file counter_one.xhtml.

52
00:02:11.090 --> 00:02:13.006
So this is my example where I'll simply

53
00:02:13.006 --> 00:02:15.506
make use of the Session Scope.

54
00:02:16.946 --> 00:02:18.928
So on line 17, I simply display

55
00:02:18.928 --> 00:02:22.907
the value of the bean at that time, counterOne.value,

56
00:02:22.907 --> 00:02:25.490
on lines 21-24, I have the form

57
00:02:26.520 --> 00:02:28.900
for my increment button, and the action

58
00:02:28.900 --> 00:02:32.274
here is counterOne.increment, so we'll

59
00:02:32.274 --> 00:02:36.357
call the increment method on the CounterOne bean.

60
00:02:39.529 --> 00:02:40.727
Okay, so let's go ahead and look

61
00:02:40.727 --> 00:02:42.859
at the code here for the CounterOne bean.

62
00:02:42.859 --> 00:02:45.783
So, moving up here, CounterOne.java,

63
00:02:45.783 --> 00:02:49.365
let me expand the window here so we can see everything,

64
00:02:49.365 --> 00:02:52.305
and on lines six-eight, that's our Managedbean.

65
00:02:52.305 --> 00:02:54.452
Session Scope CounterOne.

66
00:02:54.452 --> 00:02:55.880
The important thing to notice here

67
00:02:55.880 --> 00:02:58.081
is in line seven, SessionScoped, so

68
00:02:58.081 --> 00:03:00.443
that means that this bean will be created,

69
00:03:00.443 --> 00:03:03.888
and it will be unique for a given user of our application.

70
00:03:03.888 --> 00:03:06.212
So each user will have their own bean

71
00:03:06.212 --> 00:03:09.212
that'll have their own counter value.

72
00:03:09.212 --> 00:03:11.785
On line 10 I set up the value=0,

73
00:03:11.785 --> 00:03:14.681
and lines 12-16 I have the increment method

74
00:03:14.681 --> 00:03:16.678
that we've seen before, so on line 13

75
00:03:16.678 --> 00:03:20.845
we say value++, we simply increment the value by one.

76
00:03:22.484 --> 00:03:25.060
Now let's go ahead and run the application.

77
00:03:25.060 --> 00:03:27.406
I'll move down to my form here,

78
00:03:27.406 --> 00:03:30.406
counter_one.xhtml, I'll right click,

79
00:03:31.827 --> 00:03:34.410
I'll say Run As, Run on Server.

80
00:03:39.793 --> 00:03:41.173
And this will run the application

81
00:03:41.173 --> 00:03:44.221
and it'll show us our Session Scope test page.

82
00:03:44.221 --> 00:03:48.057
Again, I'd like to go ahead and copy this URL,

83
00:03:48.057 --> 00:03:52.869
and I wanna open it up in one of our real browsers.

84
00:03:52.869 --> 00:03:57.036
So here I have Firefox on the left and Chrome on the right.

85
00:03:58.470 --> 00:04:02.637
I'll go ahead and paste the URL into the Firefox browser,

86
00:04:03.671 --> 00:04:05.117
and now I just do an increment,

87
00:04:05.117 --> 00:04:06.543
and so notice here it's incrementing,

88
00:04:06.543 --> 00:04:08.794
and it works out fine as desired.

89
00:04:08.794 --> 00:04:10.170
Now let me do a similar thing,

90
00:04:10.170 --> 00:04:13.670
dropping this URL into the Chrome browser,

91
00:04:17.136 --> 00:04:18.336
and I'll go through, and note here

92
00:04:18.336 --> 00:04:20.923
Counter value is zero,

93
00:04:20.923 --> 00:04:23.656
because this Chrome browser represents a new user,

94
00:04:23.656 --> 00:04:26.327
so each user, or in this case, each browser

95
00:04:26.327 --> 00:04:29.341
has its own Counter value, so we're

96
00:04:29.341 --> 00:04:32.696
using a different browser to simulate a different user,

97
00:04:32.696 --> 00:04:34.691
so it's almost like each user's in,

98
00:04:34.691 --> 00:04:37.129
is accessing our application, with

99
00:04:37.129 --> 00:04:38.884
their own shopping cart, and so they

100
00:04:38.884 --> 00:04:40.398
have their own unique value.

101
00:04:40.398 --> 00:04:42.128
And this is great, so this is primarily

102
00:04:42.128 --> 00:04:44.167
for using it with like a shopping cart,

103
00:04:44.167 --> 00:04:46.839
or an online test, or like online banking.

104
00:04:46.839 --> 00:04:48.242
You wanna make sure the user's actions

105
00:04:48.242 --> 00:04:50.825
are unique for that given user.

106
00:04:52.015 --> 00:04:53.130
Now let's take a look at our next

107
00:04:53.130 --> 00:04:55.468
example using a Request Scope.

108
00:04:55.468 --> 00:04:57.189
So with Request Scope, a new bean

109
00:04:57.189 --> 00:04:59.510
is created for every web request,

110
00:04:59.510 --> 00:05:01.884
so these beans are simply short-lived.

111
00:05:01.884 --> 00:05:04.523
It's commonly used for submitting form data,

112
00:05:04.523 --> 00:05:06.466
and it's really a one-time use,

113
00:05:06.466 --> 00:05:08.076
once you submit the data, it's

114
00:05:08.076 --> 00:05:10.425
used for that one request and then thrown away.

115
00:05:10.425 --> 00:05:13.040
They don't keep track of it over any period of time.

116
00:05:13.040 --> 00:05:15.130
An important thing to note here is that this is

117
00:05:15.130 --> 00:05:19.447
the default Scope for a bean if you don't specify it.

118
00:05:19.447 --> 00:05:21.757
Alright, so let's look at a code example here.

119
00:05:21.757 --> 00:05:24.029
We're gonna start off with our Counter value again,

120
00:05:24.029 --> 00:05:25.597
with our Increment button, and so

121
00:05:25.597 --> 00:05:28.717
on our code here for counter_three.xhtml,

122
00:05:28.717 --> 00:05:32.540
we have the command action: counterThree.increment.

123
00:05:32.540 --> 00:05:36.707
So it'll call the increment method on our counterThree bean.

124
00:05:40.541 --> 00:05:43.509
So here's the code example for this Request Scope.

125
00:05:43.509 --> 00:05:46.781
Here, a new bean's gonna be created for every request.

126
00:05:46.781 --> 00:05:49.572
So we have our bean called CounterThree,

127
00:05:49.572 --> 00:05:53.046
and then, note the new annotation here: @RequestScoped,

128
00:05:53.046 --> 00:05:54.522
so this basically says that this bean

129
00:05:54.522 --> 00:05:56.639
will only be used for this one web request,

130
00:05:56.639 --> 00:05:58.090
and then we'll throw it away.

131
00:05:58.090 --> 00:06:00.427
And again remember, this is the default scope

132
00:06:00.427 --> 00:06:03.427
if you don't explicitly specify one.

133
00:06:05.305 --> 00:06:07.322
Well, let's go ahead and look at a code demo

134
00:06:07.322 --> 00:06:10.072
here of using this Request Scope.

135
00:06:12.132 --> 00:06:14.223
Let's go ahead and move back into Eclipse,

136
00:06:14.223 --> 00:06:16.209
and let's take a look at this Request demo.

137
00:06:16.209 --> 00:06:17.902
So again, we're gonna use the same

138
00:06:17.902 --> 00:06:20.670
project as before: bean-demo.

139
00:06:20.670 --> 00:06:21.720
And I'm gonna look at this file

140
00:06:21.720 --> 00:06:25.150
here called counter_three.xhtml.

141
00:06:25.150 --> 00:06:26.720
So this is the file that we'll use

142
00:06:26.720 --> 00:06:28.970
for our Request Scope Test.

143
00:06:30.096 --> 00:06:32.156
So again, follow the similar format,

144
00:06:32.156 --> 00:06:36.729
on line 17 we simply display the value of the counter,

145
00:06:36.729 --> 00:06:39.396
lines 21-24 is our form, and our

146
00:06:40.295 --> 00:06:43.500
action is counterThree.increment,

147
00:06:43.500 --> 00:06:47.667
so we'll call the increment method on the CounterThree bean.

148
00:06:51.680 --> 00:06:53.699
Now let's go look at that CounterThree bean.

149
00:06:53.699 --> 00:06:57.449
So in my source directory, CounterThree.java,

150
00:06:58.503 --> 00:07:01.048
I'll expand the window here,

151
00:07:01.048 --> 00:07:03.716
lines six-eight is our class counter bean

152
00:07:03.716 --> 00:07:05.385
which is a ManagedBean.

153
00:07:05.385 --> 00:07:07.419
Note: on line seven we have it listed

154
00:07:07.419 --> 00:07:10.629
as RequestScoped, so this bean will only be used

155
00:07:10.629 --> 00:07:13.043
for the current web request, and then thrown away,

156
00:07:13.043 --> 00:07:16.126
so it'll create a new one every time.

157
00:07:18.560 --> 00:07:21.523
So then we move down, say private value=0,

158
00:07:21.523 --> 00:07:24.184
we have our increment method, just like we've seen before,

159
00:07:24.184 --> 00:07:27.198
returns counter_three, and that's pretty much it.

160
00:07:27.198 --> 00:07:28.620
Now the important thing on line seven

161
00:07:28.620 --> 00:07:30.872
is that, if you don't specify a Scope,

162
00:07:30.872 --> 00:07:33.147
by default, it's Request Scope,

163
00:07:33.147 --> 00:07:34.679
so all of the examples that we've used

164
00:07:34.679 --> 00:07:37.463
in the previous videos, we didn't specify a Scope,

165
00:07:37.463 --> 00:07:39.779
so they all defaulted to Request Scope.

166
00:07:39.779 --> 00:07:43.946
So Request Scope is good for using one-time only form data.

167
00:07:47.337 --> 00:07:49.562
Alright, so let's go ahead and run the application.

168
00:07:49.562 --> 00:07:52.880
I'll choose the file counter_three.xhtml,

169
00:07:52.880 --> 00:07:55.781
I'll do a right click, I'll choose Run As,

170
00:07:55.781 --> 00:07:58.281
and I'll choose Run on Server.

171
00:08:03.456 --> 00:08:05.387
And this will fire it up, again we'll see

172
00:08:05.387 --> 00:08:07.339
our information here in the browser,

173
00:08:07.339 --> 00:08:09.256
I'll go ahead and copy this URL,

174
00:08:09.256 --> 00:08:11.861
just like I did before, now I'll

175
00:08:11.861 --> 00:08:16.028
move back to my regular browsers, Chrome and Firefox,

176
00:08:17.098 --> 00:08:18.835
and what I'll do here is I'll just drop

177
00:08:18.835 --> 00:08:21.430
in that URL, so I'll paste the URL into

178
00:08:21.430 --> 00:08:24.853
the Firefox browser, and then I'll just test it out,

179
00:08:24.853 --> 00:08:26.487
I'll hit Increment, and note here,

180
00:08:26.487 --> 00:08:27.769
what's really interesting here is

181
00:08:27.769 --> 00:08:30.178
that the Counter value stays at one,

182
00:08:30.178 --> 00:08:32.635
so it's doesn't keep track of the Counter Value

183
00:08:32.635 --> 00:08:35.171
over multiple web requests, so remember,

184
00:08:35.171 --> 00:08:37.981
the Request Scope, it's a one-time use only,

185
00:08:37.981 --> 00:08:40.616
they use it for one web request and throw it away,

186
00:08:40.616 --> 00:08:42.963
so effectively here, the bean is being thrown away

187
00:08:42.963 --> 00:08:44.889
and starting from scratch every time,

188
00:08:44.889 --> 00:08:47.703
so there's no history that's maintained, nothing per user,

189
00:08:47.703 --> 00:08:50.963
per application, it's simply per web request.

190
00:08:50.963 --> 00:08:53.390
So this is great for sending one-time form data,

191
00:08:53.390 --> 00:08:54.959
when you don't need to send any history.

192
00:08:54.959 --> 00:08:59.126
So most of your standard forms will be Request Scope.

193
00:09:02.914 --> 00:09:05.925
Well great, we can go ahead and wrap up this video.

194
00:09:05.925 --> 00:09:09.364
So in summary, we defined the Scopes for ManagedBeans,

195
00:09:09.364 --> 00:09:11.561
we also made use of the three most common

196
00:09:11.561 --> 00:09:16.320
Scopes: Application Scope, Session Scope, and Request Scope,

197
00:09:16.320 --> 00:09:19.605
and we followed each of them up with a real example.

198
00:09:19.605 --> 00:09:23.438
So this is some really good stuff, great work.

