Nearby lessons

1 of 34

Servlet - Introduction

📌 What You Will Learn
  • Understand Static Response vs Dynamic Response
  • Understand Web Programming for Static Response
  • Understand How Servlet Technology works
  • See complete working code examples

Introduction is an essential part of the Java Servlet technology. This lesson explains Static Response vs Dynamic Response, Web Programming for Static Response and How Servlet Technology works with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid.

Static Response vs Dynamic Response

Static Response: A response that does not change from person to person or time to time.

  • Login page of Gmail
  • Home page of ICICI Bank

Dynamic Response: A response that varies from person to person and time to time.

  • Inbox page of Gmail (different for each user)
  • Balance information of ICICI Bank (changes over time)

Web Programming for Static Response

Static file flow:

  1. Client sends a request for a static file to the server
  2. Server searches whether the requested resource is available
  3. If available → server provides that file as response
  4. If not available → server returns 404 status code (resource not found)

Note: To serve static files, no processing is required at server side. Hence web server always loves to serve static files.

Web Programming for Dynamic Response

Dynamic response flow:

  1. Client sends a request to the web server
  2. Web server checks whether the request is for static or dynamic information based on URL
  3. If static → web server searches for the file and provides it (or returns 404)
  4. If dynamic → web server forwards the request to a helper application
  5. Helper application analyzes, processes, and generates the dynamic response
  6. Helper application forwards the response to web server → client

Helper applications (technologies for dynamic responses):

  • Servlet
  • JSP
  • ASP
  • PHP
  • CGI
  • Cold Fusion

Servlet: A server-side web component responsible for generating dynamic responses. Total servlet lifecycle is managed by the web container.

Note: Web container is a big assistant to the programmer. It reduces the complexity of programming by managing the total lifecycle of the servlet.

Types of Web Containers

There are 3 types of web containers:

1. Stand Alone Web Containers:

Both web server and web container are available as a single integrated component.

  • Eg: Tomcat

2. In-Process Web Containers:

Web container is connected to the web server as a plug-in. Both run in the same address space (same machine). Both need not be from the same vendor (e.g., Apache web server + WebLogic web container).

  • Suitable for small scale applications.

3. Out-of-Process Web Containers:

Web server and web container run on different machines. Web container is attached to the web server externally. We can configure front-end Apache web server to forward requests to the back-end IBM web container.

  • Best suitable for real-time applications.

Tomcat Components

Every web server internally contains a web container. The web container has two components:

ComponentAlso Known AsResponsibilityTomcat Name
Servlet ContainerServlet EngineManage and execute Servlet componentsCATALINA
JSP ContainerJSP EngineManage and execute JSP componentsJASPER

How Servlet Technology Works

Servlet follows the Single Instance Multi-Threaded Model:

  • For every Servlet, web container creates only one object
  • For every request, web container allocates a separate thread responsible to process that request
RequestThreadServlet Object
req - 1Thread - 1Single Servlet Object
req - 2Thread - 2
req - nThread - n
📝 Key Takeaways
  • Key ideas of Servlet - Introduction explained simply
  • Ready-to-use code examples
  • Exam-style questions at the end

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3