What is Servlet Container and How It Works

What is a Servlet Container and How It Works? What is the web container in Java? Here we will discuss the web server and web container in Java. In Java, Servlet and JSP are there to develop web applications. Therefore we have Servlet Container and JSP Container. Previously in the web application, we learned that web server software is required to develop the web application. The web server software internally contains the web container.  Java-based Web Server contains Servlet Container and JSP Container. Let us discuss it in detail.

To execute standalone Java applications we need JVM, to execute HTML code we need the HTML interpreter (browser), to execute JavaScript code we need the JavaScript Engine. Similarly, to execute Servlet components (code), we need a Servlet Container or Servlet Engine. For JSP components we need both Servlet and JSP containers.

All Java web containers (like Servlet Container, JSP Container) internally run on the top of JVM/JRE. Without JVM/JRE single line of Java code can’t execute, if it is executing it means either directly or indirectly JVM is involved.

The Servlet component is a Java-based component, whereas JSP components are tags-based components that are internally servlet components. The industry is using both servlets, JSP technologies together in Java web application development. Since JSP is internally using servlet components therefore JSP container runs on top of the Servlet Container.

Every Java-based Web Server software like tomcat gives one set of built-in Servlet Container and JSP Container. We don’t need to arrange them separately, it came along with web server software. Java Web Container = Servlet Container + JSP Container

HTML, JavaScript component code goes to the browser for execution from the webserver because they are client-side web components. Servlet, JSP components are server-side web components so they execute in the web server itself by taking the support of Servlet Container and JSP Container respectively.

What is Container?

The container is a software program that runs continuously and manages the whole life cycle of a given component/program from birth to death or object creation to object destruction. When to create an object, when to destroy an object, when to call which method on that object, everything will be taken care by the container.

Servlet Container manages the life cycle of servlet component i.e. loading of servlet component class, creating objects, calling methods, managing and destroying objects will be taken care by servlet container.

The container is like an aquarium managing the whole life cycle of fishes called components. Everything required for the survival of the fishes will be taken care by the aquarium. In other words, a Servlet Container is like an aquarium managing the whole life cycle of fishes called Servlet. Similarly, JSP Container is like an aquarium managing the whole life cycle of fishes called JSP.

Web Server vs Web Container

When a web container manages the whole life cycle of a component then what is the responsibility of the webserver? Taking requests from clients (browser), hand-overing requests to containers, gathering results from containers, and delivering those results as responses to the browser is the responsibility of the webserver. But executing the server-side web component, and generating output after processing the request is the responsibility of the web container. Both together should work, then only complete request processing will happen or complete execution of the web component will take place.

For the JSP component, both JSP Container and Servlet Container together manage the whole life cycle of JSP components.

When we open a login page, then

  • The web server takes the request, and hands over the request to the web container.
  • Web container map the request to the login servlet component, code is executed, the output is generated.
  • The web server takes the result/output and delivers it back to the browser.

Now, one login form is displayed on the browser and the end-user can enter the username and password to go to the next step.

We can understand web servers and web containers through an example. Assume we (customer) go to the restaurant, gives an order, the waiter takes the order and handover the order to the chef, chef receives that order, and prepare the food with the help of raw materials based on the customer requirement when food is ready it is adverted to the waiter, the waiter gives the food to the customer. 

In our case, the browser is like a customer, the waiter is like a web server, the chef works as a web container, the raw materials and recipes work as components, Gas/heat is like JVM and without it, no food can be served.

Demon Process

The web server and its web container run as a daemon process (24×7) it means once started then it won’t stop until we stop it.

Generally, in a normal Java application when we execute a .class file then the process is started and once application execution is over and output comes then the process is over. Here the process is available for a very limited time period.

cmd> java App [Here process started]
     Output came.
     [ Process ended.]
cmd> 

But in the case of a web server and web container, the process is continuously running. So, they can take requests, process the requests by executing web components, and deliver responses 24×7. Internally they run as a daemon process.

What is the daemon process? The process that runs continuously once started and will be stopped only when it is stopped manually is called the daemon process. Example:- Windows task manager, web server process, and e.t.c.

Java Web Server Architecture

Java Web Server Architecture and Servlet Container
  • When a Servlet component is requested from the browser, it will go to the Servlet Container for execution.
  • When a JSP component is requested from the browser, it will go to JSP container + Servlet container for execution.
  • When HTML files, JavaScript file components are requested, then their code comes to the browser for execution.

Middleware Services

In the above diagram Logging, Auditing, Security, and JDBC con pooling are the middleware services.  Middleware services are not minimum logics of application development, they are additional, optional, and configurable (ability to enable or disable) logics of the application. Without middleware services also web applications can run. But when we develop web applications by applying middleware services it becomes more perfect and accurate.

Example:-

  • Security:- To protect the application (Authentication + Authorization)
  • Auditing:- Keeping track of user activities.
  • Logging:- Keeping track of code flow.
  • Con pooling:- Keeping a bunch of readily available JDBC con objects.

Authentication + Authorization means checking the access permissions of a user on resources/components, checking the identity of a user.

Note:-

  • One web server can contain one or more web applications.
  • We can apply these middleware services on one or more deployed web applications of web servers.

Responsibilities of Web Server

  1. Listening to client requests continuously by having a daemon process.
  2. Taking requests and handover them to the web container.
  3. To provide middleware services.
  4. Providing Web Container (Servlet container + JSP container).
  5. Providing an environment to deploy or undeploy the web application and other activities on web applications like stopping, reloading (stop + start) of the web application.
  6. Provide JRE/JVM.
  7. Taking the results/outputs from a web container and sending them to the browser as responses. and e.t.c.

Responsibilities of Web Container

  1. Taking requests from Web Server and mapping them to appropriate web components.
  2. Managing the whole life cycle of server-side web components like servlet, JSP components.
  3. Sending client-side web components code when requested.
  4. Executing server-side web components code when requested.
  5. Provides a special garbage collector for destroying objects. (ServletContainer will not use JVM’s garbage collector, it will use its own garbage collector).
  6. Takes care of intercommunication between web components. One web component communicates with another web component and this task is taken care by the web container. 
  7. Gives the output/results for server-side web components to Web Server.
  8. Applying configured middleware services on web components of web applications and e.t.c.

Servlet Container

A servlet container is nothing but a compiled, executable program. The main function of the servlet container is to load, initialize, and execute servlets. The servlet container is the official reference implementation for the Java Servlet and Java server pages technologies. The Java servlet and Java server page specifications were developed by Sun Microsystems under the Java community process.

A servlet container handles a large number of requests as it can hold many active servlets, listeners, and e.t.c. It is interesting to note here that the container and the objects in a container are multithreaded. So each object must be thread-safe in a container as the multiple requests are being handled by the container due to the entrance of more than one thread to an object at a time.

A servlet container may run stand-alone i.e. without a web server or even on another.

Now let us see what a servlet container does for each HTTP request for a servlet, in general:

  • The servlet container loads the servlet class and calls the init() method of the servlet as soon as the servlet is called for the first time.
  • For each request, construct an instant of javax.servlet.ServletRequest and an instance of javax.servlet.ServletResponse.
  • Invoke the servlet’s service() method by passing the ServletRequest and ServletResponse objects.
  • When the servlet class is shut down, it calls the servlet to destroy() method and unloads the servlet class.

Different Types of Containers

Different types of Containers based on the technology in which they are developed

  • Servlet Container:- Developed based on Servlet technology.
  • JSP Container:- Developed based on Servlet technology and JSP technology.
  • IOC/Spring Container:- Developed in spring framework.

Different types of containers based on the location of the container with respect to server,

  • Standalone Container:- The server and container together come as a single software program. Example:- ServletRunner (very old)
  • In Process Container:- The container resides inside the server as a separate unit which can be added or deleted based on the requirement. Example:- Servlet container of tomcat server, JSP Container of tomcat server.
  • Out of Process Container:- Here container resides outside of the server as a separate unit but linked with the server. Example:- Servlet container linked with IIS (.net server), JSP container linked with IIS.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *