What is a Servlet Container ?
A Servlet is a class that you will use to receive HTTP requests as methods and reply back with the response (usually HTML). A Servlet container is a program running in server which provides mechanism for processing client request and respond back to the client (an example is a web browser). The basic responsibilities of a Servlet container are opening of the socket, the transformation framework to turn HTTP into Java API calls, and a number of interfaces which allow you to plug in your Servlet code.
To know what a Servlet container is, we need to know about Web Server first.
What is a Web Server ?
A web server is a computer system that processes requests via HTTP, the basic network protocol used to distribute information on the World Wide Web. The primary function of a web server is to store, process and deliver web pages to clients. The communication between client and server takes place using the Hypertext Transfer Protocol (HTTP). Pages delivered are most frequently HTML documents, which may include images, style sheets and scripts in addition to text content.
An user agent, commonly a web browser or web crawler, initiates communication by making a request for a specific resource using HTTP and the server responds with the content of that resource or an error message if unable to do so. As a simple example, a user type in a URL (e.g. www.tuturself.com) in browser (a client), and get a web page to read. So what the server does is sending a web page to the client typically a real file on the server's secondary storage, but this is not necessarily the case and depends on how the web server is implemented. Now let us check
What is a Servlet Container ?
As we see here, the user/client can only request static webpage from the server. This is not good enough, if the user wants to read the web page based on his input. The basic idea of Servlet container is using Java to dynamically generate the web page on the server side. So servlet container is essentially a part of a web server that interacts with the Servlets. Servlet container is the container for Servlets.
What is a Servlet ?
A Servlet is an interface defined in javax.servlet package.It declares three essential methods for the life cycle of a Servlet – init(), service(), and destroy(). They are implemented by every Servlet(defined in SDK or self-defined) and are invoked at specific times by the server. Servlet classes which implements the javax.servlet.Servlet interface runs within a web container in an web server, servicing multiple client requests concurrently forwarded through the server and the web container.
A browser and a Servlet communicate using the HTTP protocol (a stateless request/response based protocol). The web browser establishes a socket connection to the host server in the URL , and sends the HTTP request. A servlet receives 2 Objects when it accepts a call from its client. A “ServletRequest”, which encapsulates client request from the client and the “ServletResponse”, which encapsulates the communication from the Servlet back to the client.
In addition to both HTTP request and response, HTTP headers are informational additions that convey both essential and non-essential information.
For example: HTTP headers are used to convey MIME (Multipurpose Internet Mail Extension) type of an HTTP request and also to set and retrieve cookies etc.
The Web container is responsible for managing the servlet’s life cycle. The Web container creates an instance of the servlet and then the container calls the init() method. At the completion of the init() method the servlet is in ready state to service requests from clients. The container calls the servlet’s service() method for handling each request by spawning a new thread for each request from the Web container’s thread pool . Before destroying the instance the container will call the destroy() method. After destroy() the servlet becomes the potential candidate for garbage collection. The following figure is explaining the life cycle of a Servlet.
Like any Java program, the servlet runs within a JVM. To handle the complexity of HTTP requests, the servlet container comes in. The servlet container is responsible for servlets’ creation, execution and destruction.
How Servlet container and web server process a request ?
1. Web server receives HTTP request
2. Web server forwards the request to servlet container
3. The servlet is dynamically retrieved and loaded into the address space of the container, if it is not in the container.
4. The container invokes the init() method of the servlet for initialization(invoked once when the servlet is loaded first time)
5. The container invokes the service() method of the servlet to process the HTTP request, i.e., read data in the request and formulate a response. The servlet remains in the container’s address space and can process other HTTP requests.
6. Web server return the dynamically generated results to the correct location
The six steps are marked on the following diagram:
The role of JVM
Using servlets allows the JVM to handle each request within a separate Java thread, and this is one of the key advantage of Servlet container. Each servlet is a Java class with special elements responding to HTTP requests. The main function of Servlet contain is to forward requests to correct servlet for processing, and return the dynamically generated results to the correct location after the JVM has processed them. In most cases servlet container runs in a single JVM, but there are solutions when container need multiple JVMs.