Servlets and Servlet Container

A servlet is a technology that is used to build web application. A web application is an application program stored on a remote server and users can access it from a browser.

Now the question is How the browser on our device is generating a web page when we type the URL.

When we enter the domain name to generate a web page, the domain name is converted into a 32-bit number(four 8 bits numbers separated by .) which is called an IP address or Internet protocol address.

Now you are the client and you are sending a request to the server, the server when receives your request to generate the "STATIC" website, accepts the request, and starts sending the response i.e website files.However the data files are not received all at once, instead, they are received in chunks.

If we are sending a request to the server to generate a dynamic page, the dynamic page is built at runtime i.e the page is built with changes, and the page is built when the request is obtained by the Servlet Container.

What is a Servlet Container?

The servlet container is part of the web server which can be run in a separate process. There are many servlet containers such as Jboss and Apache Tomcat WebServer.

A Servlet Containers contains Servlets.Servlets are Java Objects that have special methods for handling incoming HTTP requests. It handles various HTTP requests by using various methods and also provides the runtime environment for servlet execution. They are responsible for managing the lifecycle of servlets, handling incoming requests, and dispatching them to the appropriate servlets. Servlet containers act as an intermediary layer between web servers and servlets, making them an essential component of Java web applications.

Common servlet containers include Apache Tomcat, Jetty, and Oracle WebLogic, among others. These containers implement the Servlet API specifications and provide additional features like load balancing, session management, security, and clustering. They handle low-level details of network communication, multithreading, and resource management, allowing developers to focus on writing servlets and application logic.

What is CGI? How is servlet different from CGI

CGI (Common Gateway Interface) is a standard protocol that allows web servers to communicate with external programs or scripts to generate dynamic web content. It was one of the early methods used for server-side scripting and dynamic web page generation. When it comes to building dynamic web applications, servlets offer a powerful alternative to the Common Gateway Interface (CGI) approach. Servlets provide a robust and efficient solution by following a well-defined lifecycle managed by the servlet container. Let's dive deeper into the servlet lifecycle and explore how it offers significant advantages over CGI.

  1. Initialization: The servlet container loads the servlet class and creates an instance.the container calls the servlet's init() method.These tasks may include establishing database connections, loading configuration parameters, or initializing resources required by the servlet.

In contrast, CGI scripts are invoked each time a request is made, leading to repetitive and inefficient initialization overhead. Servlets, on the other hand, are initialized once during the container startup, and subsequent requests can be efficiently handled without reinitializing the servlet.

Difference between Java Servlet and CGI - GeeksforGeeks

Difference between Java Servlet and CGI - GeeksforGeeks

  1. Request Handling: When a client sends a request, The container creates a separate thread or reuses an existing one.

Unlike CGI, where a new process is spawned for each request, servlets can handle multiple requests concurrently within the same process

  1. Thread Safety: Servlets are designed to be thread-safe, ensuring that multiple threads can safely access and execute the servlet code simultaneously.

CGI scripts, often require explicit synchronization mechanisms to handle concurrent requests properly.

  1. Persistence and Reusability: Servlets persist in memory throughout their lifecycle, allowing them to retain state and provide efficient request processing.Additionally, servlets can store data in the session scope, enabling session management and maintaining user-specific information across multiple requests.

CGI scripts, on the other hand, are transient and do not retain state between requests. Each CGI script invocation starts from scratch.

  1. Destruction: When the container shuts down or the servlet is no longer needed, the destroy() method of the servlet is called. This method provides an opportunity to release resources, close database connections, or perform any necessary cleanup tasks.

In CGI, there is no explicit lifecycle management for script termination or cleanup.

Servlets' initialization, request handling, thread safety, persistence, and destruction capabilities provide significant advantages in terms of performance, scalability, and code simplicity.

By leveraging servlets and servlet containers, developers can build high-performing and scalable web applications that can handle multiple concurrent requests efficiently.So, whether you are building a simple web application or a complex enterprise system, servlets and the servlet lifecycle offer a solid foundation for efficient and scalable Java web development.