Nearby lessons
1 of 34Servlet - Introduction
- 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:
- Client sends a request for a static file to the server
- Server searches whether the requested resource is available
- If available → server provides that file as response
- 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:
- Client sends a request to the web server
- Web server checks whether the request is for static or dynamic information based on URL
- If static → web server searches for the file and provides it (or returns 404)
- If dynamic → web server forwards the request to a helper application
- Helper application analyzes, processes, and generates the dynamic response
- 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:
| Component | Also Known As | Responsibility | Tomcat Name |
|---|---|---|---|
| Servlet Container | Servlet Engine | Manage and execute Servlet components | CATALINA |
| JSP Container | JSP Engine | Manage and execute JSP components | JASPER |
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
| Request | Thread | Servlet Object |
|---|---|---|
| req - 1 | Thread - 1 | Single Servlet Object |
| req - 2 | Thread - 2 | |
| req - n | Thread - n |
- Key ideas of Servlet - Introduction explained simply
- Ready-to-use code examples
- Exam-style questions at the end