How Servlet Works

How Servlet Works in Java Application

In this article, I am going to discuss How Servlet Works in Java Application. Please read our previous article where we discussed First Java Servlet Application. At the end of this article, you will understand the following pointers in detail.

  1. How does the Servlet Work in Java Application?
  2. How web container handles the servlet request?
  3. What is written inside the public service method?
  4. What is written inside the protected service method?
  5. Servlets flow of execution
How Servlet Works?

First, the server checks if the servlet is requested for the primary time.

If yes, the web container handles the Service Request:

  1. loads the servlet class.
  2. instantiates the servlet class.
  3. then calls the init method passing the ServletConfig object

else

  1. it calls the service method passing request and response objects

The web container calls the destroy method when it must remove the servlet like at the time of stopping the server or un-deploying the project.

How web container handles the servlet request?

After identifying the servlet class name from the web.xml file container will recognize the respective servlet .class file under the classes folder then perform the following actions:

  1. The container will load the respective servlet class byte code to the memory.
  2. The Container will create an object for the loaded servlet.
  3. The container will create a ServletConfig object and access the init() method by passing the ServletConfig object reference.
  4. After the servlet initialization container will create a thread to access the service() method, for this, the container has to create request and response objects.
  5. The container will access the service() method and generate the required response object.
  6. After getting the response format protocol will carry that response format to the client.
  7. When the response is reached to the client, this container destroys the request and response objects.
  8. In servlet execution, the container will destroy the ServletConfig object just before destroying the servlet object.
  9. After Servlet de-instantiation, the container will eliminate the loaded servlet byte code from operational memory.
  10. ServletContext object will be destroyed at the time of server shut down.
What is written inside the public service method?

The public service method converts the ServletRequest object and ServletResponse object into the HttpServletRequest type and HttpServletResponse type respectively and then calls the service method passing these objects.

Internal Code:
public void service (ServletRequest req, ServletResponse res) throws ServletException, IOException
{
    HttpServletRequest request;
    HttpServletResponse response;
    try
    {
     request = (HttpServletRequest) req;
     response = (HttpServletResponse) res;
    }
    catch (ClassCastException e)
    {
     throw new ServletException ("non-HTTP request or response");
    }
    service (request, response);
} 
What is written inside the protected service method?

The protected service method checks the sort of request, if the request type is “GET”, it calls the doGet method, if the request type is post, it calls the doPost method, so on…

Internal Code:
protected void service (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
    String method = req.getMethod ();
    if (method.equals ("GET"))
    {
     long lastModified = getLastModified (req);
     if (lastModified == -1L)
     {
         doGet (req, resp);
     }
     //rest of the code  
     }
}
Servlets Flow of Execution

When we start the server, the main job of the container is to recognize each and every web application and to prepare the ServletContext object to each and every web application. While recognizing web application container will recognize the web.xml file under the WEB-INF folder then perform loading, parsing, and reading the content of the web.xml file.

While reading the content of the web.xml file, if the container identifies any context data in the web.xml file then the container will store context data in the ServletContext object at the time of creation. After the server startup when we send a request from the client to the server protocol will pick up the request then perform the following actions.

  1. The protocol will establish a virtual socket connection between client and server as part of the server IP address and port number which we specified in the URL.
  2. The protocol will prepare a request format having request header part and body part, where the header part will maintain request headers, and the body parts will maintain request parameters provided by the user.
  3. After getting the request format protocol will carry the request format to the main server.

Upon receiving the request from the protocol main server will check whether the requested data is in well-format or not, if it is in well-formed then the main server will bypass request to the container. Upon receiving the request from the main server container will pick up the application name and resource name from the request and check whether the resource is for any HTML page or JSP page or an URL pattern for a servlet.

If the resource name is any HTML page or JSP page then the container will pick up the application folder and send them as a response to the client. If the resource name is an URL pattern for a particular servlet available under the classes folder then the container will go to the web.xml file identifies the respective servlet class name on the basis of the URL pattern.

After identifying the servlet class name from the web.xml file container will recognize the respective servlet .class file under the classes folder then perform the following actions:

  1. Servlet Loading
  2. Servlet Instantiation
  3. Servlet Initialization
  4. Creating Request and Response objects (Request Processing)
  5. Generating Dynamic Response
  6. Dispatching Dynamic response to Client
  7. Destroying request and response objects
  8. Servlet De-instantiation
  9. Servlet Unloading
  10. Destroying ServletContext object

In the next article, I am going to discuss User Interface Forms Design in detail. Here, in this article, I try to explain How Servlet Works in Java Application. I hope you enjoy this “How Servlet Worksin Java Application article.

Leave a Reply

Your email address will not be published.