Web Applications with Servlets, JSP & JSON
An intuitive entry point into web development with HTTP web requests, Servlets, web container lifecycles, JSP web pages, and JSON data formats.
Have you ever wondered what happens behind the scenes when you visit a website or log into a web app? In this final chapter, we discover how Java powers backend web servers using Servlets, JSP pages, and JSON data.
10.1. 1. How the Web Works & Java Servlets
Definition: A Servlet is a server-side Java class running inside a Web Container (e.g. Apache Tomcat) that receives HTTP requests from web browsers and sends back dynamic responses.
Core Working Principles:
- HTTP Request & Response: Web browser sends an HTTP Request (GET/POST) $\rightarrow$ Java Web Server receives request $\rightarrow$ Servlet generates HTTP Response HTML.
- Servlet Lifecycle: `init()` (one-time setup) $\rightarrow$ `service()` (handles incoming user requests) $\rightarrow$ `destroy()` (cleanup on server shutdown).
• Web Browser (Client): The customer sitting at a table ordering food.
• HTTP Request: The food order written on a ticket.
• Servlet (Backend Waiter): Receives the order ticket, brings it to the kitchen (Java Backend), and returns a plate of food (HTTP Response) to the customer!
5-Marks Exam Scoring Points
- Servlets are server-side Java components processing HTTP web requests.
- Web Containers (Tomcat) manage Servlet lifecycles.
- service() handles incoming requests on worker threads.
10.2. 2. Dynamic Web Pages with JSP (JavaServer Pages)
Definition: JSP extends Servlets by allowing developers to write HTML web layouts embedded with dynamic Java tags.
Core Working Principles:
- JSP Translation: When requested, the web server automatically translates `.jsp` files into standard Java Servlets behind the scenes!
- Separation of Concerns: Lets web designers build clean HTML layouts while Java handles backend data.
| Web Technology | Primary Role | Best For |
|---|---|---|
| Java Servlets | Java Code generating web responses | Backend Controllers & Business Logic |
| JSP Pages | HTML layouts with embedded Java tags | Frontend Web Presentation Templates |
5-Marks Exam Scoring Points
- JSP pages are translated into Java Servlets by the web container automatically.
- JSP simplifies authoring web page presentation layouts.
- Scripting tags and Expression Language display dynamic Java data.
10.3. 3. Sharing Data with JSON (JavaScript Object Notation)
Definition: JSON is a simple, lightweight text format used by modern web apps and mobile phones to exchange data with Java servers.
Core Working Principles:
- Universal Text Format: Easy for humans to read and simple for computers to parse.
- Key-Value Format: Uses double quotes for key names and values e.g.
{"name": "Alex", "age": 20}.
// Example JSON Data Payload sent between Web Apps and Java
{
"studentId": 101,
"name": "Alex",
"isEnrolled": true
}
5-Marks Exam Scoring Points
- JSON is a universal, human-readable text format for sharing data.
- Used extensively in modern REST web APIs and mobile apps.
- Java converts JSON text into Java objects using libraries like Jackson or Gson.
✍️ Chapter 10 Reference Exercises & Review Problems
- The **Web Browser** is like a restaurant customer who places an order (HTTP Request).
- The **Java Servlet** is like the waiter who takes the order, asks the kitchen (Java code/Database) to prepare the meal, and delivers the finished dish (HTTP Response HTML) back to the customer's browser!
JSON (JavaScript Object Notation) is a simple, human-readable text format (`{"key": "value"}`). It is used because every programming language (Java, JavaScript, Python, C#) can easily read and write plain text, making it the universal standard for sharing data across the web!