1 00:00:00,240 --> 00:00:02,200 When you were earning about http, 2 00:00:02,200 --> 00:00:05,130 you probably heard about something called cookies. 3 00:00:05,130 --> 00:00:07,870 Now cookies are bits of data that are stored on the client and 4 00:00:07,870 --> 00:00:10,960 are sent on each request to a specific domain. 5 00:00:10,960 --> 00:00:15,550 Now, one of the hardest things about using cookies is avoiding the impulse to say, 6 00:00:15,550 --> 00:00:17,960 me want cookie, a la Cookie Monster. 7 00:00:19,930 --> 00:00:22,770 So now that I've got that out of the way, hopefully I won't do that again. 8 00:00:22,770 --> 00:00:23,700 I'm sorry about that. 9 00:00:25,170 --> 00:00:28,880 You can set a cookie on a response and read it on all future requests. 10 00:00:28,880 --> 00:00:31,030 That sounds exactly like what we wanna do. 11 00:00:31,030 --> 00:00:32,160 Let's go use some cookie power. 12 00:00:33,300 --> 00:00:34,870 Okay, so first thing's first, 13 00:00:34,870 --> 00:00:38,630 let's go extract that query parameter that we pulled here. 14 00:00:38,630 --> 00:00:40,800 Let's put that into its own variable. 15 00:00:40,800 --> 00:00:43,300 So again, what you do is you highlight that, and 16 00:00:43,300 --> 00:00:47,080 you choose Refactor, Extract, Variable. 17 00:00:47,080 --> 00:00:51,090 And what that will do is it will pull out a brand new variable line, and 18 00:00:51,090 --> 00:00:52,480 it's gonna try to name it. 19 00:00:52,480 --> 00:00:53,090 Did a great job. 20 00:00:53,090 --> 00:00:53,720 Username's fine. 21 00:00:53,720 --> 00:00:54,990 And it replaces it. 22 00:00:54,990 --> 00:00:59,750 So now, this variable is able, we're able to use this variable twice. 23 00:00:59,750 --> 00:01:02,680 It's the point of a variable, the local variable. 24 00:01:02,680 --> 00:01:07,720 So on the response object, the ability to set a cookie exists. 25 00:01:07,720 --> 00:01:11,650 So you do a cookie, and then you save the name of the cookie that you want it to be. 26 00:01:11,650 --> 00:01:13,780 So we want it to be called username. 27 00:01:13,780 --> 00:01:16,540 And we want to set it to that new variable that we just did. 28 00:01:18,320 --> 00:01:21,350 Okay, so now every request that gets sent to our domain, 29 00:01:21,350 --> 00:01:23,790 which in our case here is localhost. 30 00:01:23,790 --> 00:01:25,180 It will receive this data. 31 00:01:25,180 --> 00:01:26,040 Do you wanna see it? 32 00:01:27,090 --> 00:01:27,760 Cool, let's do it. 33 00:01:27,760 --> 00:01:29,736 So let's go come here. 34 00:01:29,736 --> 00:01:32,431 And we will reboot the server. 35 00:01:32,431 --> 00:01:35,215 And we'll fly over to our page. 36 00:01:35,215 --> 00:01:38,120 I have my network tools up. 37 00:01:38,120 --> 00:01:42,130 I'm gonna go ahead, and I'm gonna type craigsdennis. 38 00:01:42,130 --> 00:01:44,426 But before I click Sign in, I wanna show you something. 39 00:01:44,426 --> 00:01:48,550 So, this is shortend cuz my screen is smaller here, 40 00:01:48,550 --> 00:01:51,470 but at these little arrows there's a thing here called resources. 41 00:01:51,470 --> 00:01:52,510 Now look at resources. 42 00:01:52,510 --> 00:01:56,000 There is a thing called cookies and then there's a thing that says localhost. 43 00:01:56,000 --> 00:01:58,110 This site has no cookies because we haven't set it yet. 44 00:01:58,110 --> 00:01:58,870 Right? 45 00:01:58,870 --> 00:02:00,310 So I'm gonna set it and when I click send. 46 00:02:00,310 --> 00:02:03,080 It's gonna run through our sign-in. 47 00:02:03,080 --> 00:02:05,490 It's gonna pull the request off, and it's gonna set the cookie. 48 00:02:06,530 --> 00:02:07,220 Boom, and there it is. 49 00:02:07,220 --> 00:02:07,770 It's set. 50 00:02:07,770 --> 00:02:08,310 Okay?. So 51 00:02:08,310 --> 00:02:11,550 now you see that the user name is craigsdennis. 52 00:02:11,550 --> 00:02:15,350 And now, if I come back, and I hit the homepage, 53 00:02:15,350 --> 00:02:20,800 you'll see that the cookies are still there. 54 00:02:20,800 --> 00:02:25,950 And if we look back over here at our network tab. 55 00:02:25,950 --> 00:02:27,550 Let's look at that again. 56 00:02:27,550 --> 00:02:28,050 Hit All. 57 00:02:29,400 --> 00:02:31,680 Look at the local host page here. 58 00:02:31,680 --> 00:02:35,820 You can see that the cookie has, it's there, and it's available. 59 00:02:35,820 --> 00:02:42,750 Let's make the homepage aware of whether or not we've gathered their username. 60 00:02:42,750 --> 00:02:45,330 So we need to make a model. 61 00:02:45,330 --> 00:02:45,930 Right? 62 00:02:45,930 --> 00:02:48,090 So let's make that same model like we did before. 63 00:02:48,090 --> 00:02:50,990 So we'll just do the same thing, and we'll make a new map. 64 00:02:50,990 --> 00:02:56,286 And it's a new map of key values that are both strings. 65 00:02:56,286 --> 00:03:00,130 And it's called model 66 00:03:03,474 --> 00:03:06,123 And we'll make a new HashMap. 67 00:03:06,123 --> 00:03:07,083 It's call String, String. 68 00:03:12,043 --> 00:03:16,356 Okay, and we are going to put into our model, we're gonna put a new value 69 00:03:16,356 --> 00:03:19,629 called username, and we're gonna pull that cookie. 70 00:03:19,629 --> 00:03:22,480 So the way that you do that is just like you might think. 71 00:03:22,480 --> 00:03:24,460 You just say cookie, and then the name of the cookie. 72 00:03:24,460 --> 00:03:26,279 And that will get that value that's there. 73 00:03:28,939 --> 00:03:33,630 Okay, and now our index.hbs needs to know about the model. 74 00:03:34,750 --> 00:03:37,800 And now our template knows about the username value. 75 00:03:37,800 --> 00:03:39,480 So let's use it, right? 76 00:03:39,480 --> 00:03:41,259 Let's go and let's look at our. 77 00:03:46,820 --> 00:03:48,500 Resources. 78 00:03:48,500 --> 00:03:49,660 And we'll look in here. 79 00:03:49,660 --> 00:03:50,960 We'll look under index. 80 00:03:54,400 --> 00:03:58,060 And what we wanna do is we wanna only say hello if we know their name. 81 00:03:58,060 --> 00:04:03,710 So the way to do that in Handlebars is to use what's known as a block helper. 82 00:04:03,710 --> 00:04:06,650 So a black helper, you do a pound. 83 00:04:06,650 --> 00:04:10,480 And the block helper here in this case is something you're familiar with already. 84 00:04:10,480 --> 00:04:11,980 It's called if. 85 00:04:11,980 --> 00:04:14,470 So we're gonna say if username. 86 00:04:14,470 --> 00:04:17,240 It's a little bit different of an if than you're used to. 87 00:04:17,240 --> 00:04:20,690 It's if that exists in the model, that key exists in the model, 88 00:04:22,280 --> 00:04:26,170 then run this code that's inside these two if blocks. 89 00:04:26,170 --> 00:04:33,031 So we're gonna say h1, Welcome, username. 90 00:04:34,150 --> 00:04:35,120 Okay? 91 00:04:35,120 --> 00:04:40,610 And then if we don't know what it is, we wanna have a block called else. 92 00:04:40,610 --> 00:04:43,373 So, in here there is an else. 93 00:04:46,974 --> 00:04:52,200 Okay, and we can grab this code here, and we can move it up. 94 00:04:55,150 --> 00:04:58,480 So now what's happening is it comes in. 95 00:04:58,480 --> 00:05:02,020 It says if there is a username on the model, show this message, 96 00:05:02,020 --> 00:05:03,060 Welcome username. 97 00:05:03,060 --> 00:05:04,450 Otherwise show Welcome Students. 98 00:05:04,450 --> 00:05:08,450 I Better put the exclamation point here, so they both get exclmationed. 99 00:05:10,490 --> 00:05:13,740 All right, so let's go ahead and we'll click run. 100 00:05:13,740 --> 00:05:15,990 We'll do a run, and we'll restart the server. 101 00:05:17,820 --> 00:05:18,320 All right. 102 00:05:19,400 --> 00:05:21,720 Let's look at our index page. 103 00:05:23,770 --> 00:05:24,760 Awesome. 104 00:05:24,760 --> 00:05:27,470 So that's using the first part of the if, and it's using the cookie. 105 00:05:27,470 --> 00:05:29,870 So we can prove that by if we come over here to the Cookies. 106 00:05:30,950 --> 00:05:34,210 If you click the Cookies in Chrome, you can press Delete, and 107 00:05:34,210 --> 00:05:35,320 now the site has no cookies. 108 00:05:35,320 --> 00:05:38,440 And now if I refresh this page, we'll see our form. 109 00:05:40,040 --> 00:05:40,939 And we can put in our name. 110 00:05:49,249 --> 00:05:51,669 That prompt is back. 111 00:05:51,669 --> 00:05:53,770 We don't really need this page anymore, do we? 112 00:05:53,770 --> 00:05:55,520 We don't need this sign in page. 113 00:05:56,660 --> 00:05:57,900 We could just drop them here. 114 00:06:00,280 --> 00:06:00,980 Awesome. 115 00:06:00,980 --> 00:06:03,080 By adding cookies to our bag of tricks, 116 00:06:03,080 --> 00:06:09,150 we now have the core tool set used to make a web application appear to be stateful. 117 00:06:09,150 --> 00:06:11,090 You'll find these patterns of forum submission and 118 00:06:11,090 --> 00:06:13,630 storing values in cookies very relevant. 119 00:06:13,630 --> 00:06:15,270 Now that you know these terms, 120 00:06:15,270 --> 00:06:18,490 it's more a question of how each framework implements them. 121 00:06:18,490 --> 00:06:22,760 Try to remember, this is all just sitting on top of HTTP and providing a nice to use 122 00:06:22,760 --> 00:06:26,730 abstraction but really not doing to much more at it's core. 123 00:06:26,730 --> 00:06:30,420 You should be able to start understanding most web frameworks now. 124 00:06:30,420 --> 00:06:33,650 We should have all the tools we need to start building out the rest of our 125 00:06:33,650 --> 00:06:34,610 application. 126 00:06:34,610 --> 00:06:38,690 Let's go do that, and along the way I'll show you some handy Spark specific tricks 127 00:06:38,690 --> 00:06:39,950 right after these messages.