Beginner Reference Manual • Chapter 10

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:

🍽️ Layman Analogy: Restaurant Customer & Waiter

• 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:

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:

// 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

Exercise 10.1: Explain the role of a Web Browser (Client) vs a Java Web Server (Backend) using the Restaurant Customer & Waiter analogy.
Step-by-Step Explanation for Beginners:
- 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!
Exercise 10.2: What is JSON and why is it used to send data between web apps and servers?
Step-by-Step Explanation for Beginners:
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!