Understanding Java Servlet Architecture
Java Servlets are software component that runs on a web server or an application server and be responsible to receive the request from the web server, process the request, and respond back to the server.
Servlets extends the capabilities of a server as they can respond to many types of requests, they act like web containers for hosting web applications on web servers. They support nearly all the client–server protocols, but is often used with HTTP and also known as HTTP servlet.
Architecture
Let’s understand the client-server architecture.

- The user sends HTTP requests to the web server
- The server has the web container containing servlet, which gathers data from the database and creates a response.
- The response created by servlet is sent through HTTP Response to the client browser.
The question arises that how is the servlet’s response converted to HTTP Response format? As the web server works purely on the HTTP protocol. Hence, this conversion from servlet’s response to HTTP response is taken care by the web container.
Web container
Web container is also known as servlet container or servlet engine. It provides the runtime environment for Java EE (j2ee) applications. The client/user can request only a static web page from the server. If the user wants to read the web pages as per input then the web container is used in java. Therefore, it implements the Servlet API and the services required to process HTTP requests.
Web container initiates the Servlet that matches the requested URL by invoking the service() method of Servlet class. The service() method which is invoked for a given HTTP request is handled in a separate thread within the web container protocol.

- The User sends an HTTP Request to the web server
- Web server forwards requests to Web Container
- Web Container forwards the request to the Servlet in form of the request object
- Servlet builds the response object and sends it back to the Web Container
- Web container transforms the response object to equivalent HTTP response and sends it to the web server
- The web server sends the response via HTTP response back to the client.
Lifecycle
Servlet life cycle consists methods that covers entire process from its creation till the destruction. Following are the lifecycle steps:
- init() is called only once. It is invoked only when the servlet is created. So, it’s used for one-time initialisations. Generally, the servlet is created when a user first invokes a URL corresponding to the servlet, but you can also specify that the which servlet should be loaded when the server is first started.
- service() method is the main method that performs the actual task. The web container (servlet container) calls the service() method to handle requests coming from the client. Each time the server receives a request for a servlet, the web container spawns a new thread and calls service(). This method checks the HTTP request type (GET, POST, PUT, DELETE, etc.) and calls doGet, doPost, doPut, doDelete, etc. methods as appropriate.
- destroy() method is invoked only once at the end of the life cycle of a servlet. This method gives the servlet a chance to close database connections, halt background threads, and perform other such cleanup tasks. After the destroy() method is executed, the servlet object is marked available for garbage collection.
- Finally, the servlet object is garbage collected by the garbage collector of the JVM.
So how does this all work together?

- First the concurrent HTTP requests coming to the server are forwarded to the web container.
- The web container initiates the servlet before invoking the service() method.
- Then the web container handles multiple requests by spawning multiple threads, each thread executing the service() method of a single instance of the servlet.
Hopefully you should have a basic understanding of the Java Servlet and its Architecture.
Thanks for reading.