From the course: Java EE: Servlets and JavaServer Pages (JSP)

Understanding the purpose of servlets

From the course: Java EE: Servlets and JavaServer Pages (JSP)

Start my 1-month free trial

Understanding the purpose of servlets

- [Woman] Let's take a look at why do we need a servlet. Let's understand a diagram of client server architecture over the web. Every website that you access is going to be accessed from the web browser. That's the web client. And this web client is going to make a request to the web server. The web server is going to act on it and produce a response and give it back to the web client. So the communication with any website that you make today is nothing but a series of requests and responses, a chain of requests and responses that go on. And it could be concurrent requests. There are millions of user accessing your website all over the globe and there could be multiple concurrent parallel requests coming in from multiple browsers. Now, this entire chain of requests and response that gets exchanged between the web client and the web server is nothing but over the H-T-T-P protocol. Hypertext transfer protocol is the only communication strategy between the web client and the server. They do not understand any other language except H-T-T-P. Now, if your web server needs to act and process a request, it needs to be fed a set of instructions. Let's understand, what option do we have for server-side programming? We could use Java Standard-Edition A-P-Is, which is code Java, but code Java A-P-Is are designed only to develop stand-alone applications. Yes, you do have applets but applets work in the browser side, not on the server side. Let's take a look at another option. This Common Gateway Interface scripts, which is C-G-I. Now these are nothing but programs which will help web servers generate dynamic webpages because that's what we need. Every page that you access today is customized according to the kind of user you are. So you need to generate dynamic content. However, C-G-I is going to be a costly process. It's not feasible because every request that you make, C-G-I creates a new process for every new request. Imagine yourselves on a website, you click a button, or you enter something into a text box, or you check a radio button, or you click a hyperlink, every action that you're doing is a request. If you are going to create a new precess for every new request, it means you're setting up a new environment, a new context and there are a lot of resources from the machine allocated. So this is terrible going to pull down the performance of your application. Moreover, C-G-I is a traditionally older way of coding. If you look at the other platforms, which offer you server-side options, you have the dot NET platform, which has got A-S-P.NET A-P-I. Then you have Node J-S, which is the latest server-side programming using Java script. You have P-H-P et cetera. However, we are into the enterprise world of Java, and we need an A-P-I so that we can code server-side programming features. And that is exactly where servlets pitch in. So let us take a look at the features of servlets. Servlets offer you a robust server-side programming option to generate dynamic web content. And they're going to be way more efficient as opposed to C-G-I scripts because servlets create a new thread for every new request. We know the multi-threading capability of Java platform, where you have multiple threads spawned and each thread will be allocated a task to perform. These threads can run in parallel and hence can improve the performance of your application considerably. This is what servlets employ when they work. Now, since they're servlets and released as a part of the Java community, they can work with all the J-V-M features. Platform independence where you can file a program only once then run it on any other O-S. Or you have object orientation, which will help you to simulate the real world entities into the programming model. We talked about multi-threading, and the garbage collection, which runs at a scheduled time in order to free up memory space and make sure application run faster. Now, if you look at the servlet specification, the J-S-R, which is the Java Specification Request, the number 315 was released by Java community for version 3.0 as a part of Java Enterprise Edition number six, which works with J-D-K 1.6 and then there is J-S-R 340, which is for version 3.1, which was released as a part of Java E-E seven. Please understand one more point that servlets can work with the entire Java Standard-Edition A-P-Is. Be it object orientation, exception handling, collections, Generix, multi-threading, inner classes and even J-D-B-C. The servlets are going to work in conjunction with your code Java A-P-Is to help you build a wonderful web application.

Contents