 Hello everyone. So I'm Mr. Duttadry Ghandmal from Walchit Institute of Technology, Sallapur. Today we are going to see introduction and life cycle of servlet. So this is one of the important concepts in Java programming language. Why there is need of servlet? What is servlet? And all these things we are going to study in this particular video. What student will be able to understand at the end of the session? Students will be able to describe the use and need of servlets, differentiate various components in life cycle of servlets. This is what students will be able to understand at the end of this particular video. So starting with what is servlet? So what do you think? What is servlet? Here I have given the clear description about servlet. You can go through this particular video, you can pause here and read everything. So I will be explaining it in brief. Servlet is a Java programming language. So it is basically used to improve the capability of server. So we know why servers are used. Servers are used to interact with clients and provide service to a client. So processing and request reply kind of things are done with client server programming. So to extend the capability of server to use server to its maximum capacity, servlets are introduced. So servlet is one programming language in Java, which is developed through Java programming language. And in this, it will extend the capability of servers. So by means of how? By means of request response programming model. We know that server client means request response. So request response is going to work over here and it is going to request, it is going to reply. So this complete processing is possible, request response is possible using servlets. It is commonly used to applications hosted by web servers. So web related things, so client server basically used in web browsers, web servers and so client basically accesses through web browser. So to provide service to those web browsers, web servers are used. So server basically, servlets are basically installed on web servers. Servlet is a web component that is deployed on server site to create a dynamic web page. So using servlet dynamic web request and web page creation is possible. So otherwise if you just create a simple HTML and store it in a server, whenever a request comes that HTML page will go. But here what is different? It is dynamic. So using servlet we can generate dynamic web pages. And servlet is which client side or server side? Servlet is server side programming language which is used for developing web applications. Communication basically happens through HTML and XML. We know that whenever a web application comes into picture, the communication is basically through HTML and XML. Functioning of a servlet. So servlet is technology to create web applications as we have seen now. So basically this request response happens with help of HTTP request and HTTP response. So these protocols are used to provide service to using web browsers to clients. This is a detailed architecture explaining how a user workstation will request HTTP request to web server. Web server is going to respond with help of HTTP response. So what exactly web server is going? What exactly is going to happen when a request comes? This is what the yellow and green boxes highlight. So when a request comes, web container will come into picture. And inside web container we have a servlet. That servlet page responds. That servlet object is responded and it will come as a HTTP response to web server. And web server will fire that particular HTTP response back to user. So this is what will happen inside web server. So we are interested in this servlet. So servlet is located where servlet is located inside web container and web container is present inside web server. Once you understand the architecture, it is very easy to understand. So basically it is not directly. Otherwise if it is a simple HTML, what will happen? HTML request will come, it will respond. There is no processing at all. Because of this processing dynamic web page, we are able to provide. What is web application? Web application is an application accessible from the web. So this is general slide explaining web application to the students who don't know anything about web application. So how web applications are developed using various languages like JSP, servlets, HTML, CSS, etc. So this execution basically happens at web server and the response is sent back to client. So this is related to web application. You can more Google it about it. But yeah, this is sufficient to understand servlets. Now before servlet, there was something called as common gateway interface. Why there is need of servlet? We'll try to understand using this slide. Common gateway interface, why it was used. It is a standard for a web server to pass a web client demand to web application program. So it is again like a servlet, which was earlier than servlet. They were introduced earlier than servlet. It responds to a client, same like servlet. It is piece of web's hypertext transfer protocol HTTP. So it again works on HTTP. So we will understand what there are some drawbacks with CGI. We will come to it soon. Drawbacks of CGI. What are the drawbacks? So here you can see when a request comes, first of all, look at this diagram. When a request comes, what web server is going to do? Web server is going to allocate one CGI shell. And it is going to process it and respond it. Again, there is a second request. Again, there is one more CGI shell. Third request, third shell. So likewise, there are different requests. Each request is handled differently. So each request is given a separate process. So drawback is that each new process is started for each request. So therefore, you can see that basically load at this processor will increase, processor load will increase, and respond time will be slower. Platform view is basically our CC++ Perl. Need of servlet. So that drawback of CGI is overcome in servlet. How? When a request comes, instead of creating a new process, it will create a thread. We know that thread are lightweight. So servlet works on the principle of threading. It will create a thread, provides a response. Creates a thread, provides a response. Third request, creates a thread, provides a response. So here you can observe that processor load is reduced drastically. This is the advantage of using servlet. There are some drawbacks with CGI for which servlets were introduced. So what is advantage? Better performance, portability, robust, and they are secure. This is about servlet. Now here you can pause the video and answer this question. Which architecture provides better performance? CGI or servlet? Second, which architecture response time is poor? CGI or servlet? You can answer both one by one. You can pause and think, rewind video and answer it. Okay? Now the answer for this is, first one is servlet and second one is CGI. So which architecture provides better performance? CGI or servlet? Servlet is going to provide better architecture as we have seen because it uses threading. Which architecture response time is poor? CGI response time, architecture response time is poor because it is going to create process individually. Then moving ahead to servlet lifecycle. So every time it is very important, whenever you learn any technology, you should understand its lifecycle. Then only you can write a good program. So once you understand this lifecycle, you can write an optimized code. As I said, servlet. Now I am inside web server. Inside web server, there is a block called as web container. You can see this, the dark green web container. Inside web container, we have something called as servlet, the slide green part. Now this servlet is having its own lifecycle. Whenever you write a servlet program, it has to go through this lifecycle. So what this lifecycle says, this lifecycle is having five phases. Load, create, init, service and destroy. So basically it is going to work one by one. It is going to load. It is going to create. It is going to initialize. It is going to provide service. At the end, it has to destroy. So in Java, it is very, very important to understand that whenever object is created, destroying should be there. So Java provides, that is a special feature of Java that it destroys object whenever object is created. So dynamic destroying is there. Now servlet lifecycle one by one, if I look into it, loading the servlet class. So first of all, loading of servlet class has to be done from web browser. So first of all, it will load the servlet class, then create the servlet class. Once you are loaded with the servlet class, you have to create it. That means your object is getting created in the second part. What is going to happen in third part? All the resources required for their particular servlet are going to get allocated. So that is init method. So initialization is going to happen over here. Then this is what, when we are going with programming language, when we are going to write a program, this syntax is important. Public wide init, servlet conflict and through servlet exception. So this we will come to know later. You can now make a note of it. Next service method. What service method is going to do? It is going to provide the actual service. It is going to display the dynamic page. It is going to interact with the user. This is a servlet request and response code. This method is a wide service method, having two parameters, servlet request and servlet response. It is very, very important to understand. Through servlet exception and IO exception. This line we will be going through. Don't worry about it. Just make a note of it for now. When I teach you exactly how to do the program, then these things will become very easy. Then destroy method. So as object is getting created, it has to be destroyed. What destroy method is going to have? Simple public wide destroy. Okay. Now, how the sample servlet program is going to look? Very excited to see this public class first servlet, implement servlets. So these three methods that we are talking about, very, very important in it. There is one parameter servlet config. There is service method that it is having two parameters, servlet request and response. And third is destroy. E is the standard architecture for any servlet program. This you have to make a note of it. import javax.servlet.star import java.io.star These are the highlight things which you have to keep it in mind. These are some of the references which I have used for servlet. This is just an introduction to servlet. That's it. This is to understand how servlet works, why there is need of servlet and differentiate various life cycles of servlet. That's it from this video. In next video we will see actual working with servlet programming. I hope you understood. Thank you.