In this lab we're going to examine cookies. We were asked to create a single application that's just a dynamic web project. You were actually provided a starter project if you prefer to pull in and you were to name the project Lab03 Cookies, which we have done. Once we have that project done, the next thing we were supposed to do was check the index.html file that we brought in, and take a look at it. Let's see what it has. It has a link here to CookieMonster. Notice the action of the form as a post, it's goes to a mapping of CookieMonster and then it has this little submit button. We basically have two things on this page. We have a link that's going to call the Cookie Monster Servlet we're going to develop, and that's going to call its doGet method. Then we have a form that post to that same Cookie Monster Servlet, and that's going to call the doPost method. That was really the takeaway, you need to make sure you understand that very clearly so that you understand the mapping. Then the next thing we were supposed to do is make a Cookie Monster Servlet which we've done in the package com.servlet.example, and here's our Cookie Monster Servlet. You'll notice that in our doGet method, we were asked to create a new cookie which we did, we made a new instance of cookie, and then we added that cookie to our response. Now notice our cookie name and our cookie value those will be important later on. After that all we do is put a simple append here just to show that the page was served, there's nothing else we're going to look at in the page besides that. That's when we do our doGet, when we click on the link for our servlet and we do our doGet then we'll come to this method and we'll go ahead and add the cookie and the response, so we should be able to see it in the response. The next thing you were asked to do is run your application. We can do that quickly and just see what's going on. I'll pull the page over so that you can see it. In my browser when I ran that HTML page, indeed I'm seeing there's a link to the Cookie Monster Servlet, and there's a post to the Cookie Monster Servlet. Now we're ready to examine things. What we're going to do is we're going to take a look at the headers and the request and response and see if we can tell what's going on. Now there's a couple of ways to do it, in previous lessons we've looked at the TCPIP monitor, but today we're going to just take a look at what we call the F12 methods in Chrome. If you're opening this in Chrome or if you're doing your browser, you can do a web search and find the way to do the same thing, but inside of Chrome we just simply hit F12 and that opens up our settings. Sometimes you have to hit this little Control R down below just to see the index page, and then you can see how the request response headers are going back and forth. In this case remember all we've requested is an HTML page. We did a request to the index.html, we got a response back, and you can see the response in request headers going across. We have our HTML page. Now when we click on our link to our Cookie Monster Servlet what's going to happen? Well, we now have gone to CookieMonster and we sent a request over, but notice what happened on the response. Our doGet method has fired, so our response set a response header with a cookie1 that was our name, and our value, mycookievalue, of course we could have added a lot more to that. We're seeing our response header indeed had the cookie. Now what happens if we go back and we call our doPost? Now I'm going to run the doPost link and when we do that indeed we go back to our CookieMonster Servlet again, but this time we ran the doPost method and you can see it's a post method. What happened this time? Well, you'll notice that in our request header down here, our cookie was sent across, so cookie1 was sent back to the server. Now you've seen the round trip, when we ran the doGet, the CookieMonster Servlet said, I want to add a cookie, I want to remember who you are, I want to give you a little information. Then when we came back to the site again with a new request, in this case a post, we were able to see that indeed our request header had that information. Taking a quick look at the code again just to make sure we remember all of this, in the DoGet method we simply instantiated the cookie, gave it a key and a value. We added the cookie to the response and we were good to go. In the doPost method, what we did was we were trying to read the cookies coming in from the request and we called the request.getCookies loop through the array of cookies that were returned, and we just printed out what they were. Now in this case we would have printed all of them, but there were only one and you can see down below that it's value was mycookievalue.