Life Cycle of Servlet –
- Loading –
- It is the responsibility of the classloader to load the servlet class.
- When the web container receives the first request for the servlet, it loads the servlet class.
- During this step, a Servlet container creates a ServletContext object. An interface that defines the set of methods that a servlet can use to communicate with the servlet container is known as ServletContext.
- Instantiation –
- After loading the servlet class, the web container creates the instance of a servlet.
- The servlet life cycle creates the servlet instance only once.
- Initialization –
- The init method initializes the servlet.
- The init method is responsible to create or load the data that will be used throughout the life of the servlet.
- Syntax – public void init(ServletConfig config)
- Service –
- Each time the web container receives the request for the servlet, it calls the service method.
- The service() method is the main method to perform the actual task i.e. to handle requests coming from the client and to write the formatted response back to the client.
- The container calls the service method and this method invokes doGet, doPost, doPut, doDelete, etc. methods as appropriate.
- Syntax – public void service(ServletRequest request, ServletResponse response)Â
- Destruction –
- It calls the destroy method only once at the end of the life cycle of a servlet.
- The web container calls the destroy method before removing the servlet instance from the service to give a chance to –
- clean up any resource
- close database connections
- halt background threads
- write cookie lists
- hit counts to disk
- perform other such cleanup activities
- Syntax – public void destroy() Â
Advantages of Servlet –
- Better Performance as it works on threads.
- Portability
- Robust
- Secure
- Platform Independent
Flow of Servlet Execution –
- The client sends the request to the webserver.
- Then, the web server receives the request.
- Then, the request is passed to the corresponding servlet by the webserver.
- Then, the servlet processes the request and generates a response in the form of output.
- Then, the servlet sends the response back to the webserver.
- Then, the response is sent back to the client by the web server and the client browser displays it on the screen.
How Servlet handles the request ?
- The web container maps the request with the servlet in the web.xml file.
- So, the web container creates request (HttpServletRequest) and response (HttpServletResponse) objects for this request.
- So for that request, the web container will create or allocate a thread and call the Servlet’s service() method and passes the request, response objects as arguments.
- Based on HTTP Request Method(Get, Post etc) sent by the client, the service() method, then decides which servlet method, doGet() or doPost() to call.
- The Servlet sends back the response to the client using the response object.
- And, after the service() method is executes, the thread dies. Then, the web container deletes the request and response objects and is ready for garbage collection.
import java.io.*;
import javax.servlet.*;
public class ServletLifeCycle implements Servlet {
@Override
public void init(ServletConfig config) throws ServletException {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public void service(ServletRequest request, ServletRespnse response) throws ServletException, IOException {
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}