[MUSIC] Servlets, as we've seen, provide a mechanism for our code to receive HTTP requests and then respond to them in some way. So, let's take at a, look at a code segment to begin implementing this video service that we talked about originally. That we want to be able to send commands to it to add videos and we want to be able to query the list of videos that have been sent to the server. So, this is a very simple servlet. What we see is that we have a class up here that extends the HttpServlet. And so, what that does is that automatically creates a class that has a super class with the doGet, doPost and other methods for the HTTP methods. So we can go and override some of those methods to begin adding logic to our server. Now, one of the important things, obviously, is this servlet is providing the parent HttpServlet class is providing a number of different functions to our class, and we're just overriding a certain subset of those functions like the doGet method that aren't implemented. So we're just providing extra logic to implement our business-specific things. In this case, the things related to video that we are layering on top of the servlet framework. So, what we've got is we've got a servlet, and then it has a list of the videos that have been sent to the server. Now, this is obviously a very simple example. We're only storing things in memory, so if you shut the server down, the servlet's not going to remember what videos have been uploaded. But we want to start with something simple so that we can build up and understand some of the things that servlets give us, as well as some of the limitations and why we might want to use another framework. Particularly, the spring framework, which we're going to talk about later. So we're storing a list of the videos that have been uploaded. And videos are just simply an object that tracks information about a video, its name, its URL and its duration. So, we have this list. And then on our doGet method right here, if someone goes and sends a get request, what's going to happen is this doGet method on our servlet is going to be invoked. And what we can see is is that our servlet and its doGet method, the first thing it does is it sets the content type. [COUGH] And the idea behind this is the servlet wants to tell the client how to interpret the result it's sending back. And for this case, we're just doing something simple. We're just going to send black, back plain text, so what we're doing is we're setting the content type to plain text. And what this is is the MIME type that's going to be send back to the client. So when the client receives this, it will know, I don't need to interpret [INAUDIBLE] this as HTML, I just need to interpret it as plain text. Similarly, I, I don't need to interpret this as a video or an image or anything else. It knows exactly what to do with it. And then a little later down in our servlet, what we see is that we're looping through the list of videos that we have in the servlet. And then for each video, we're simply writing out a string that's being sent back to the client. In this case, we're writing back the name and URL of each video and we're just separating these by new lines, so you'll essentially have a plain text list of the videos that are being currently stored on the server. Now, this is a very, very simple example and we can see it's relatively straightforward to go and implement this basic logic for storing a list, looping through the list and displaying the results to the client, sending them back in a plain text format. The doGet method of the servlet's pretty simple. But when we look at the doPost part where we're actually going to send some data to the servlet in order to store a new video, things start to get a little bit more complicated. So let's look at the work that we're having to do in this doPost method that isn't ideal and is going to help motivate why we're going to use another framework built on top of servlets that provides us a greater level of abstraction. And will help us to construct cloud-based services from mobile devices even faster. But let's take a look at this doPost method to help understand why we're going to do that later. So, if we look at the doPost method, we've got a, a simple doPost method that we've overrided from the super class here, which is HttpServlet. And this is part of the same servlet that we were just looking at. And then we've got something interesting going on. The first thing that we're doing is, because we want the client to be able to send us information about a video that we need to save, the client is going to in encode that information either as query parameters or as a body of an HTTP post in this case. Because we've got a post request. So, the first thing we have to do is we have to extract that data that the client is sending to us in order to get it out and do something with it. So, one of the very first things we see over here is we have extraction of data from the client, so we've got a series of method calls that are being used to get out information like the duration of the video or the name of the video. So, once we've done that, [COUGH] the next thing that the post does is it actually goes and it sets the content type [COUGH] of the response. Because we want the, the client to know that we're just going to send plain text back, just like we did in the doGet method, and we don't want it misinterpreted. We're going to send a response back that's plain text hat tells the client what happened on the server and gives it a little bit more information than just the HTTP response code that we're going to be sending. Now, things start to get a little bit more complicated because we have the situation here where we have to worry about the fact of what happens if the client fails to send the parameter that we're expecting, or the client sends a value that isn't what we need. So, for example, we're checking not only for these values not being present and null. So if up here when we're extracting the values, if one of those values doesn't exist, if we're asking for a parameter that wasn't provided by the client, it's going to return null. So down here, we have to do error checking to make sure that all of those parameters are there. So this is error checking. We gotta make sure that the client sent us all of the information that we actually need. And if for some reason, the client failed to send us a parameter that we need, we need to tell the client about that. We need to have an error code sent back to the client and we need to have an error message sent back to the client. So in this case, we're sending back the 400 status code, the, the 400 response code which tells the client that its request was malformed. It was missing data in its request that was needed. And then down in the error message, we're just telling it here is the, the data that we expected to see in that request that wasn't there. So we were expecting a duration and some other things. And you'll notice that we're starting to build sort of our own protocol on top of HTTP. We're specifying things that we expect and the client has to know about them, and the client has to make sure that it encodes them into its request and provides them in either the query parameters or the post body. Now, assuming that we get past all this [INAUDIBLE] checking, we don't have any empty strings. Then we're going to go down and we're actually finally going to start doing some actual logic related to the video. That is, we want to create a video with the data we've been given and then we want to add it to the list. But even when we get down here, we've still got a little bit more error checking to do and some conversion. So, if we see here, one of the things that we're having to do is we're having to actually go and take this string that was extracted up here for the duration because all of these parameters are being sent as strings in the HTTP request and we have to convert it into the native type. In this case, we have to convert it into a long, which is what we expect in Java to have. So we actually have to do the parsing and extraction of that long before we can do anything else. So this is going to be a data type conversion that we're doing. And this adds a little bit more complexity to what we have to get, get done. On top of this, all of these things are then being extracted, and once we finally get all of this data out of the request, you know, this simple process or what should be a simple process of getting all this data out. Once we finally do that, then we actually go and construct the video that we want to store in memory in this list. And then finally, only down at the very end, do we get to actually do some real logic. And we can actually create and add the video, in this case, to the list. So it took us a whole lot of work to extract parameters, to error check the parameters, to convert them to the correct types. So before we could finally create an object that we needed to and do something with that object. And then finally, we tell the client, hey, good job you did, it worked. We got your video, we've added it to the list of videos that's there. Now, this is a very simply servlet. And you can imagine, for complex cloud services that are supporting mobile devices, it could take a lot of work if we were having to do all of these things for every single different piece of logic that we wanted. This was a very simple piece of logic. And yet, we were having to do a lot of work to go from, really, what this is about, is going from HTTP to Java. And then making sure that we have correct values, in our Java that we've extracted. Doing any conversions we need to. And then finally at the end, doing logic related to our actual application. And we would really like to be focusing down here on this part of the application, but you can see that in this case, most of our code is focused on this part. And that's a lot of work that we'd really like to not have to do.