πŸƒWhat is Servlet in Java ?

Describe about servlet in java

Servlets are a component of the JEE framework used for web development. Servlet is a class that handles requests, processes them and reply back with a response. Servlets are under the control of another Java application called a Servlet Container. Servlets need to be registered first so that a container, When an application running in a web server receives a request, the Server hands the request to the Servlet Container – which in turn passes it to the target Servlet.

For servlet life cycle including 3 part :

  • init()

  • service()

  • destroy()

init()

  1. Loads the servlet class

  2. Creates an instance of the servlet class

  3. Initializes it by calling the init method

service()

This method is only called after the servlet's init() method has completed successfully.The Container calls the service() method to handle requests coming from the client, interprets the HTTP request type (GET, POST, PUT, DELETE, etc.) and calls doGet, doPost, doPut, doDelete, etc. methods as appropriate. destroy()

Called by the Servlet Container to take the Servlet out of service.This method is only called once all threads within the servlet's service method have exited or after a timeout period has passed. After the container calls this method, it will not call the service method again on the Servlet.

Reference: https://www.baeldung.com/intro-to-servlets

Last updated