Servlet Application Architecture

A Servlet is a Java class that can be loaded dynamically into and run by a special web server. This Servlet-aware web server is called a Servlet container, which also was called a Servlet engine in the early days of the Servlet technology. Servlets interact with clients via a request-response model based on HTTP. Because Servlet technology works on top of HTTP, a Servlet container must support HTTP as the protocol for client requests and server responses. However, a Servlet container also can support similar protocols, such as HTTPS (HTTP over SSL) for secure transactions. Figure 1 below provides the architecture of a Servlet application.

Figure 1: The Servlet application architecture

In a JSP application, the Servlet container is replaced by a JSP container. Both the Servlet container and the JSP container often are referred to as the web container or Servlet/JSP container, especially if a web application consists of both Servlets and JSP pages.

As we can see in the Figure 1, a Servlet application also can include static content, such as HTML pages and image files. Allowing the Servlet container to serve static content is not preferable because the content is faster if served by a more robust HTTP server, such as the Apache web server or Microsoft’s Internet Information Server (IIS). As such, it is common practice to put a web server at the front to handle all client requests. The Web server serves static content and passes to the Servlet containers all client requests for Servlets. Figure 2 below shows a more common architecture for a Servlet application.

Figure 2: The Servlet application architecture employing an HTTP server.

 

Servlet’s working method (with detailed architecture)

A Servlet is loaded by the Servlet container the first time the Servlet is requested. The Servlet then is forwarded the user request, processes it (there may be some database processing), and returns the response to the Servlet container, which in turn sends the response back to the user. After that, the Servlet stays in memory waiting for other requests—it will not be unloaded from the memory unless the Servlet container detects a shortage of memory. Each time the Servlet is requested, however, the Servlet container compares the timestamp of the loaded servlet with the Servlet class file. If the class file timestamp is more recent, the Servlet is reloaded into memory. This way, we don’t need to restart the Servlet container every time we update our Servlet. The whole process is shown in Figure 3 below.

Figure 3:  Servlet detailed architecture

 

Steps :—

  1. The client (web browser) makes a request via HTTP protocol.
  2. The web Server receives the request and forwards it to the servlet.
  3. The servlet will receive the request and perform some processing (database call).
  4. The servlet will return a response back to the web Server.
  5. The web server will forward the response to the client in terms of HTML output.