0% found this document useful (0 votes)
3 views17 pages

Web Java Guide

The document is a beginner's study guide on Web Technologies and Java, covering five essential topics: User-Browser-Server Interaction, Servlet Lifecycle, JDBC Database Connection, Full Stack Architecture (React + Java + MongoDB), and Spring Boot in Microservices. Each topic includes definitions, processes, and code examples to illustrate key concepts. The guide aims to provide foundational knowledge for learners in web development and Java programming.

Uploaded by

abdulkalamak0602
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views17 pages

Web Java Guide

The document is a beginner's study guide on Web Technologies and Java, covering five essential topics: User-Browser-Server Interaction, Servlet Lifecycle, JDBC Database Connection, Full Stack Architecture (React + Java + MongoDB), and Spring Boot in Microservices. Each topic includes definitions, processes, and code examples to illustrate key concepts. The guide aims to provide foundational knowledge for learners in web development and Java programming.

Uploaded by

abdulkalamak0602
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Web Technologies & Java — Beginner's Study Guide

Web Technologies & Java


Complete Beginner Study Notes
15 Marks Each • 5 Essential Topics

# Topic Marks
1 User–Browser–Server Interaction 15
2 Servlet Lifecycle 15
3 JDBC Database Connection 15
4 Full Stack Architecture (React + Java + MongoDB) 15
5 Spring Boot in Microservices 15

Page 1 | 15 Marks Each | For Beginner Learners


Web Technologies & Java — Beginner's Study Guide

Topic 1: User–Browser–Server Interaction [15 Marks]

1. User–Browser–Server Interaction

1.1 What is it? (Simple Definition)


When you open a website (like [Link]), a lot of things happen behind the scenes. Your
browser (Chrome, Firefox) talks to a server (a powerful computer on the internet) to get the web
page. This back-and-forth communication is called User–Browser–Server Interaction.

1.2 The Three Main Players


Player What it is Real-World Analogy
User The person using the website Customer at a restaurant
Browser Software that displays web pages (Chrome, Firefox) Waiter who takes your order
Server A computer that stores and sends web pages Kitchen that prepares your food

1.3 Step-by-Step: What Happens When You Visit a Website


Let's say you type [Link] and press Enter. Here is what happens:

• Step 1 — You (User) type a URL in the browser and press Enter.
• Step 2 — Browser sends an HTTP Request to the server. (Like: 'Please give me the
Amazon homepage')
• Step 3 — The request travels over the internet using TCP/IP protocol.
• Step 4 — DNS Lookup happens: The URL '[Link]' is converted to an IP address
like [Link].
• Step 5 — The Server receives the request, finds the page, and sends back an HTTP
Response.
• Step 6 — The Browser receives the HTML, CSS, JavaScript files.
• Step 7 — Browser renders (draws) the web page on your screen.

1.4 HTTP Request and Response (The Language They Speak)


HTTP stands for HyperText Transfer Protocol. It is the rules for how browsers and servers
communicate.

An HTTP Request contains:


• Method: GET (fetch data), POST (send data), PUT (update), DELETE (remove)

Page 2 | 15 Marks Each | For Beginner Learners


Web Technologies & Java — Beginner's Study Guide

• URL: The address of the resource


• Headers: Extra info like browser type, cookies
• Body: Data sent (only in POST/PUT requests)

An HTTP Response contains:


• Status Code: 200 = OK, 404 = Not Found, 500 = Server Error
• Headers: Info about the response (content type, etc.)
• Body: The actual HTML/data sent back

1.5 Simple Code Example


A basic HTTP GET request looks like this:

GET /[Link] HTTP/1.1


Host: [Link]
Accept: text/html

--- Server Response ---


HTTP/1.1 200 OK
Content-Type: text/html

<html>
<body><h1>Welcome!</h1></body>
</html>

1.6 Stateless Nature of HTTP


IMPORTANT: HTTP is STATELESS. This means the server does NOT remember who you are
between requests. That's why websites use Cookies and Sessions to remember you (like keeping you
logged in).

1.7 Summary Table


Term Full Form Meaning
HTTP HyperText Transfer Protocol Rules for web communication
URL Uniform Resource Locator Web address (like [Link])
DNS Domain Name System Converts domain name to IP address
GET — Request to fetch/read data
POST — Request to send/submit data

Page 3 | 15 Marks Each | For Beginner Learners


Web Technologies & Java — Beginner's Study Guide

200 — Success status code


404 — Page not found status code

Page 4 | 15 Marks Each | For Beginner Learners


Web Technologies & Java — Beginner's Study Guide

Topic 2: Servlet Lifecycle [15 Marks]

2. Servlet Lifecycle

2.1 What is a Servlet?


A Servlet is a Java program that runs on a web server and handles HTTP requests from browsers.
Think of it as a Java function that is called every time someone opens a web page or submits a
form.

In simple terms: A Servlet is the Java equivalent of a waiter — it takes requests from users and gives
back responses.

2.2 Where Does Servlet Fit?


Servlets run inside a Servlet Container (also called a Web Container). Examples: Apache Tomcat,
JBoss. The container manages the lifecycle of the Servlet — it decides when to create it, use it, and
destroy it.

2.3 The 5 Stages of Servlet Lifecycle


Stage Method Who Calls It When It Happens
Called
1. Loading & (no method) Container When server starts or first request
Instantiation (Tomcat) arrives
2. Initialization init() Container Once after object is created
3. Request Handling service() Container Every time a request comes
4. HTTP Methods doGet() / service() method Based on request type
doPost()
5. Destruction destroy() Container When server shuts down

2.4 Stage-by-Stage Explanation

Stage 1 — Loading & Instantiation


• The web container (Tomcat) loads the Servlet class.
• It creates ONE object (instance) of the Servlet class using new.
• This happens only once during the lifetime of the application.

Page 5 | 15 Marks Each | For Beginner Learners


Web Technologies & Java — Beginner's Study Guide

Stage 2 — Initialization: init() Method


• Called exactly ONCE after the Servlet object is created.
• Used to set up resources: database connections, config files.
• Receives a ServletConfig object with initialization parameters.

Stage 3 — Request Handling: service() Method


• Called EVERY TIME a user sends a request to this Servlet.
• This is where the main work happens.
• The container passes HttpServletRequest (user's request) and HttpServletResponse (what
you send back).
• The service() method reads the HTTP method and calls doGet() or doPost() accordingly.

Stage 4 — HTTP Methods: doGet() / doPost()


• doGet() is called when user types a URL or clicks a link (GET request).
• doPost() is called when user submits a form (POST request).
• You override these methods to write your business logic.

Stage 5 — Destruction: destroy() Method


• Called ONCE when the server is shutting down.
• Used to release resources: close database connections, save data.

2.5 Code Example


import [Link].*;
import [Link].*;
import [Link].*;

public class HelloServlet extends HttpServlet {

// Stage 2: Called ONCE when Servlet is first loaded


public void init(ServletConfig config) throws ServletException {
[Link]("Servlet Initialized!");
}

// Stage 3 & 4: Called for every GET request


protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {

[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<h1>Hello, World!</h1>");
}

Page 6 | 15 Marks Each | For Beginner Learners


Web Technologies & Java — Beginner's Study Guide

// Stage 5: Called ONCE when server shuts down


public void destroy() {
[Link]("Servlet Destroyed!");
}
}

2.6 Key Points to Remember


• init() and destroy() are called ONLY ONCE.
• service() / doGet() / doPost() are called for EVERY request.
• Only ONE instance of Servlet is created — multiple threads use the same object
simultaneously.
• The Servlet container (Tomcat) controls the entire lifecycle — not the developer.

Memory Trick: Lifecycle = LOAD → INIT → SERVICE → DESTROY (Like a job: Hire → Train →
Work → Retire)

Page 7 | 15 Marks Each | For Beginner Learners


Web Technologies & Java — Beginner's Study Guide

Topic 3: JDBC Database Connection [15 Marks]

3. JDBC Database Connection

3.1 What is JDBC?


JDBC stands for Java Database Connectivity. It is a Java API (a set of ready-made classes and
methods) that allows Java programs to connect to and interact with databases like MySQL, Oracle,
PostgreSQL, etc.

Real-world analogy: JDBC is like a universal remote control. Just like one remote can control different
TV brands, JDBC can connect to different databases using the same Java code.

3.2 JDBC Architecture


Layer Component Role
Your Java App JDBC API ([Link] package) Your code that uses JDBC
Middle Layer JDBC Driver Manager Manages all database drivers
Driver Layer JDBC Driver (e.g., MySQL Connector) Translates Java calls to DB-specific calls
Database MySQL / Oracle / PostgreSQL Actual data storage

3.3 The 6 Steps to Connect Java to a Database

Step 1 — Load the JDBC Driver


Tell Java which database you are using (MySQL, Oracle, etc.)
[Link]("[Link]");
// This loads the MySQL JDBC driver into memory

Step 2 — Create a Connection


Establish a connection to the database using URL, username, and password.
String url = "jdbc:mysql://localhost:3306/myDatabase";
Connection con = [Link](url, "root", "password");
// Now we are connected to the database!

Step 3 — Create a Statement

Page 8 | 15 Marks Each | For Beginner Learners


Web Technologies & Java — Beginner's Study Guide

Create a Statement object to send SQL queries to the database.


Statement stmt = [Link]();
// Statement is like a messenger that carries SQL to the database

Step 4 — Execute the Query


Send your SQL query. Use executeQuery() for SELECT, executeUpdate() for
INSERT/UPDATE/DELETE.
// For SELECT (reading data):
ResultSet rs = [Link]("SELECT * FROM students");

// For INSERT/UPDATE/DELETE (modifying data):


int rows = [Link]("INSERT INTO students VALUES (1, 'Alice', 90)");

Step 5 — Process the Results


Read the results row by row using ResultSet.
while ([Link]()) {
int id = [Link]("id");
String name = [Link]("name");
int marks = [Link]("marks");
[Link](id + " " + name + " " + marks);
}

Step 6 — Close the Connection


Always close the connection to free up resources.
[Link]();
[Link]();
[Link]();
// Like hanging up the phone after a call!

3.4 Complete Working Example


import [Link].*;

public class JDBCExample {


public static void main(String[] args) throws Exception {

// Step 1: Load Driver


[Link]("[Link]");

// Step 2: Connect
Connection con = [Link](
"jdbc:mysql://localhost:3306/school", "root", "1234");

Page 9 | 15 Marks Each | For Beginner Learners


Web Technologies & Java — Beginner's Study Guide

// Step 3: Statement
Statement stmt = [Link]();

// Step 4: Execute Query


ResultSet rs = [Link]("SELECT * FROM students");

// Step 5: Process Results


while ([Link]()) {
[Link]([Link](1) + " " + [Link](2));
}

// Step 6: Close
[Link]();
[Link]("Done!");
}
}

3.5 Statement vs PreparedStatement


Feature Statement PreparedStatement
Query compilation Compiled every time Compiled ONCE, reused
Performance Slower (for repeated queries) Faster
SQL Injection Vulnerable (unsafe) Safe (parameters are escaped)
Use case One-time queries Queries executed many times

Always use PreparedStatement when inserting user input into SQL. It prevents SQL Injection attacks
(a major security threat).

Page 10 | 15 Marks Each | For Beginner Learners


Web Technologies & Java — Beginner's Study Guide

Topic 4: Full Stack Architecture (React + Java + MongoDB) [15 Marks]

4. Full Stack Architecture (React + Java + MongoDB)

4.1 What is Full Stack?


Full Stack means developing BOTH the frontend (what users see) and the backend (server logic +
database). A Full Stack application has 3 main layers — often called the Three-Tier Architecture.

4.2 The Three Tiers Explained


Tier Technology What It Does Location
Frontend (Presentation) [Link] UI that users see and interact with Browser
Backend (Application) Java (Spring Boot) Business logic, API endpoints Server
Database (Data) MongoDB Stores and retrieves data Database Server

4.3 How They Connect — The Request Flow


Here is what happens when a user submits a form on a React website:

• Step 1 — User fills a form in the React app (running in browser).


• Step 2 — React sends an HTTP Request (API call) to the Java backend using fetch() or
Axios.
• Step 3 — The Java backend (Spring Boot REST Controller) receives the request.
• Step 4 — Java processes the request (validation, business logic).
• Step 5 — Java connects to MongoDB and saves/retrieves data.
• Step 6 — MongoDB returns the data to Java.
• Step 7 — Java sends back a JSON response to React.
• Step 8 — React updates the screen with the new data.

4.4 React Frontend — What it Does


React is a JavaScript library for building user interfaces. Key concepts:

• Components: Reusable UI pieces (like Lego blocks). Example: Header, LoginForm,


ProductCard.
• State: Data that the component stores and displays. When state changes, the UI updates
automatically.
• Props: Data passed from parent component to child component.

Page 11 | 15 Marks Each | For Beginner Learners


Web Technologies & Java — Beginner's Study Guide

• Axios / fetch: Used to make HTTP API calls to the Java backend.

// React component that fetches data from Java backend


function StudentList() {
const [students, setStudents] = [Link]([]);

[Link](() => {
fetch("[Link]
.then(res => [Link]())
.then(data => setStudents(data));
}, []);

return (
<ul>
{[Link](s => <li key={[Link]}>{[Link]}</li>)}
</ul>
);
}

4.5 Java Spring Boot Backend — What it Does


Spring Boot creates REST APIs. A REST API is a URL endpoint that returns JSON data.

// Java Spring Boot REST Controller


@RestController
@RequestMapping("/api/students")
public class StudentController {

@Autowired
private StudentRepository repository; // connects to MongoDB

// GET all students


@GetMapping
public List<Student> getAllStudents() {
return [Link]();
}

// POST - add new student


@PostMapping
public Student addStudent(@RequestBody Student s) {
return [Link](s);
}
}

4.6 MongoDB — What it Does


MongoDB is a NoSQL database that stores data as JSON-like documents (called BSON). Unlike
SQL, there are no tables — only collections and documents.

Page 12 | 15 Marks Each | For Beginner Learners


Web Technologies & Java — Beginner's Study Guide

SQL Term MongoDB Equivalent Example


Database Database school_db
Table Collection students
Row Document { "name": "Alice", "marks": 90 }
Column Field name, marks, id

// A MongoDB document (student record):


{
"_id": "64abc123",
"name": "Alice",
"age": 20,
"marks": [85, 90, 78],
"address": {
"city": "Mumbai",
"pin": "400001"
}
}

4.7 Architecture Summary


Layer Port Technology Communicates Via
React Frontend 3000 [Link] + Axios HTTP to Backend
Java Backend 8080 Spring Boot REST API Mongoose Driver to DB
MongoDB Database 27017 MongoDB Responds with JSON

Page 13 | 15 Marks Each | For Beginner Learners


Web Technologies & Java — Beginner's Study Guide

Topic 5: Spring Boot in Microservices [15 Marks]

5. Spring Boot in Microservices

5.1 What Are Microservices?


Microservices is an architecture style where a big application is broken into small, independent
services. Each service does ONE specific job and can be developed, deployed, and scaled
independently.

Analogy: Imagine [Link]. Instead of one giant program, it has separate services: User Service
(login), Product Service (catalog), Order Service (cart/checkout), Payment Service, Delivery Service.
Each runs independently!

5.2 Monolithic vs Microservices


Feature Monolithic App Microservices App
Structure One big application Many small services
Deployment Deploy entire app for any change Deploy only changed service
Scaling Must scale entire app Scale only the busy service
Failure One bug can crash everything One service fails, rest keep running
Technology Same language/framework Each service can use different tech
Example One WAR file for everything [Link], [Link]

5.3 What is Spring Boot?


Spring Boot is a Java framework that makes it very easy and fast to build microservices. It provides:

• Auto-configuration: Spring Boot auto-sets up things like database, server, etc.


• Embedded Server: No need to install Tomcat separately — it's built in.
• REST API support: Easily create HTTP endpoints with just annotations.
• Spring Data JPA/MongoDB: Easy database connections.
• Actuator: Built-in health check and monitoring endpoints.

5.4 Key Components in a Spring Boot Microservices Setup

Page 14 | 15 Marks Each | For Beginner Learners


Web Technologies & Java — Beginner's Study Guide

Component Tool Purpose


API Gateway Spring Cloud Gateway / Netflix Zuul Single entry point for all requests
Service Discovery Eureka Server Services find each other by name, not IP
Load Balancer Ribbon / Spring Cloud LoadBalancer Distributes traffic across instances
Config Server Spring Cloud Config Central configuration for all services
Circuit Breaker Resilience4j / Hystrix Handles failures gracefully
Message Queue RabbitMQ / Kafka Async communication between services

5.5 How It Works — Request Flow


Let's say a user wants to place an order on an e-commerce app:

• Step 1 — User sends request → API Gateway (one entry point for all).
• Step 2 — API Gateway routes to Order Service.
• Step 3 — Order Service checks Eureka: 'Where is Product Service?'
• Step 4 — Order Service calls Product Service to verify item availability.
• Step 5 — Order Service calls Payment Service to process payment.
• Step 6 — After payment, Order Service sends a message to Delivery Service via Kafka.
• Step 7 — Each service saves data to its OWN database (Database per Service pattern).
• Step 8 — Response is sent back to user through the API Gateway.

5.6 Simple Spring Boot Microservice Code


Creating a User Service as a microservice is as simple as:
// [Link]
@SpringBootApplication
@EnableEurekaClient // Register this service with Eureka
public class UserServiceApplication {
public static void main(String[] args) {
[Link]([Link], args);
}
}

// [Link]
@RestController
@RequestMapping("/users")
public class UserController {

@GetMapping("/{id}")
public User getUser(@PathVariable Long id) {
return [Link](id);
}

Page 15 | 15 Marks Each | For Beginner Learners


Web Technologies & Java — Beginner's Study Guide

@PostMapping
public User createUser(@RequestBody User user) {
return [Link](user);
}
}

// [Link]
[Link]=8081
[Link]=user-service
[Link]=[Link]

5.7 Eureka Service Discovery — How Services Find Each Other


Problem: When you have 50 microservices, services need to know each other's address. Eureka
solves this.

• Each service registers itself with Eureka Server on startup with its name and port.
• When Service A needs to call Service B, it asks Eureka: 'Give me the address of order-
service'.
• Eureka returns the address and Service A makes the call.
• If a service goes down, Eureka removes it from the registry automatically.

// To call another service using Feign Client (easy way!)


@FeignClient(name = "product-service") // uses service name, not IP!
public interface ProductClient {

@GetMapping("/products/{id}")
Product getProduct(@PathVariable Long id);
}

5.8 Summary — Spring Boot Microservices Quick Reference


Concept What it means Spring Tool Used
Microservice Small, independent service Spring Boot
Service Registry Address book for services Eureka Server
API Gateway Front door for all requests Spring Cloud Gateway
Inter-service call One service calling another Feign Client / RestTemplate
Config Management Centralized settings Spring Cloud Config
Fault Tolerance Handle service failures Resilience4j
Async Communication Non-blocking messaging Kafka / RabbitMQ

Page 16 | 15 Marks Each | For Beginner Learners


Web Technologies & Java — Beginner's Study Guide

Key Rule: Each microservice should have its OWN database. Never share a database between two
microservices — this defeats the purpose of independence!

Page 17 | 15 Marks Each | For Beginner Learners

You might also like