VOOZH about

URL: https://www.geeksforgeeks.org/java/servlet-container-in-java/

⇱ Servlet Container in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Servlet Container in Java

Last Updated : 18 May, 2026

A Servlet Container is a key component of Java web applications that provides a runtime environment for executing servlets. It acts as an intermediary between the web server and servlets, handling client requests and generating responses. It simplifies development by managing lifecycle, security, and request processing.

  • Controls servlet execution using init(), service(), and destroy() methods
  • Receives HTTP requests and routes them to the appropriate servlet.
  • Manages user sessions and provides authentication and authorization features.

Architecture of Servlet Container

The diagram shows how different components interact in a Java web application using a servlet container.

👁 Servlet_Container
Architecture of Servlet Container

Concepts of Servlet Container:

1. Client

  • The client (browser) sends an HTTP request.
  • The web server (labeled as Web Listener) receives this request.

2. Web Listener

  • The web server forwards dynamic requests to the servlet container.
  • The servlet container manages the processing and sends results back to the web server.

3. Servlet Container

  • The container identifies the correct servlet based on URL mapping.
  • It sends the request to the servlet for processing.

4. Servlet Processing

  • The servlet handles business logic.
  • If needed, it connects to the database using JDBC.

5. Database (Data Source)

  • The servlet interacts with the data source to fetch/store data.
  • Results are returned to the servlet.

6. Response Flow Back

  • Servlet ->Servlet Container -> Web Server -> Client
  • The processed response (HTML/JSON) is sent back to the user.

Example:

Output:

👁 servletoutput
Output

Explanation:

  1. Servlet Annotation (@WebServlet ("/hello")): This map the servlet to URL pattern /hello.
  2. deGet Method: It is handle the HTTP GET requests.
  3. response.setContentType ("text/html"): It is tell a browser that response is HTML.
  4. PrintWriter out = response.getWriter(): It is get the writer to output the response.
  5. out.println(...): It is used to send the HTML response to browser.

Popular Servlet Containers

Several servlet containers are widely used in Java web development to run servlets and manage the request-response cycle.

  • Apache Tomcat : A lightweight, open-source, and most widely used servlet container developed by the Apache Software Foundation.
  • Jetty :A fast and lightweight container, ideal for microservices and embedded applications.
  • GlassFish : A full-featured application server that includes a servlet container along with enterprise tools.
  • IBM WebSphere : A robust enterprise-level server with advanced features and high performance.
  • Oracle WebLogic : A powerful enterprise server widely used for large-scale applications.
Comment
Article Tags:
Article Tags: