Unit 3: Web Application development using JSP & Servlets
Servlet Overview
A servlet is a server-side Java program used to create dynamic web applications. It runs on a web server or application server and handles requests from clients (browsers) and sends responses back.
Why Servlets are Used
- To process user requests
- To generate dynamic web content
- To handle form data
- To interact with databases
- To build secure and scalable web applications
Advantages of Servlets
| Advantage | Description |
|---|---|
| Platform independent | Runs on JVM |
| Efficient | Uses multithreading |
| Secure | Server-side execution |
| Persistent | Loaded once, reused |
Servlet Architecture
Servlet architecture explains how a servlet works inside a web server.
Main Components
- Client (Browser) – Sends request
- Web Server – Receives request
- Servlet Container – Manages servlet lifecycle
- Servlet – Processes request
- Response – Sent back to client
Request–Response Flow
- Client sends HTTP request
- Web server forwards request to servlet container
- Container loads servlet (if not loaded)
- Servlet processes request
- Response is sent back to browser
Diagram Description (Exam Writing)
Servlet Interface
The Servlet interface is part of javax.servlet package.
All servlets must implement this interface directly or indirectly.
Methods of Servlet Interface
| Method | Purpose |
|---|---|
init() | Initializes servlet |
service() | Handles requests |
destroy() | Cleans up resources |
getServletConfig() | Returns configuration |
getServletInfo() | Returns information |
Example
In practice, servlets usually extend HttpServlet instead of implementing Servlet directly.
Servlet Life Cycle
The Servlet Life Cycle describes the stages a servlet goes through from creation to destruction.
Life Cycle Phases
- Loading and Instantiation
- Initialization (
init()) - Request Handling (
service()) - Destruction (
destroy())
Life Cycle Diagram (Exam Format)
Explanation
| Phase | Description |
|---|---|
| init() | Executed once |
| service() | Executed for each request |
| destroy() | Executed once before removal |
Handling HTTP GET Requests
The GET method is used to request data from the server.
Data is sent through the URL.
Characteristics of GET
- Data visible in URL
- Limited data size
- Less secure
- Used for fetching data
Method Used
Example
Handling HTTP POST Requests
The POST method is used to send data securely to the server.
Characteristics of POST
- Data not visible in URL
- No size limitation
- More secure
- Used for form submission
Method Used
Example
GET vs POST Comparison
| Feature | GET | POST |
|---|---|---|
| Data visibility | Visible in URL | Hidden |
| Security | Less | More |
| Data size | Limited | Unlimited |
| Usage | Fetch data | Submit data |
Summary Table
| Topic | Key Point |
|---|---|
| Servlet | Server-side Java |
| Architecture | Client–Server model |
| Life Cycle | init → service → destroy |
| GET | Retrieve data |
| POST | Submit data |
Exam Tips
- Write servlet life cycle steps clearly
- Always mention
HttpServlet - Draw request–response flow diagram
- Compare GET and POST in table form
Redirecting Requests to Other Resources
Redirecting means sending a client request from one resource to another (Servlet, JSP, or HTML page).
Types of Redirection
1. Request Dispatching (Forwarding)
- Done on the server side
- URL in browser does not change
Method Used
2. Response Redirection
- Done on the client side
- URL changes
Method Used
Comparison Table
| Forward | Redirect |
|---|---|
| Server side | Client side |
| URL unchanged | URL changes |
| Faster | Slower |
Session Tracking
Session Tracking is used to maintain user information across multiple requests.
Why Session Tracking is Needed
- HTTP is a stateless protocol
- Server does not remember previous requests
Session Tracking Techniques
| Technique | Description |
|---|---|
| Cookies | Stored on client |
| URL Rewriting | Data in URL |
| Hidden Fields | Hidden form data |
| HttpSession | Server-side session |
Cookies
A cookie is a small piece of data stored on the client’s browser.
Types of Cookies
- Persistent cookies
- Non-persistent cookies
Creating a Cookie
Reading a Cookie
Advantages and Limitations
| Advantages | Limitations |
|---|---|
| Simple | Less secure |
| Saves user preferences | Limited size |
Session Tracking with HttpSession
HttpSession stores user data on the server side.
Creating a Session
Retrieving Session Data
Invalidating Session
Advantages
- More secure
- Can store complex objects
- Widely used in real applications
Introduction to Java Server Pages (JSP)
JSP is a server-side technology used to create dynamic web pages using HTML and Java code.
JSP Features
- Easier than Servlets
- HTML-centric
- Platform independent
- Supports MVC architecture
Java Server Pages Overview
How JSP Works
- Browser sends request
- JSP is translated into Servlet
- Servlet is compiled
- Response sent to browser
JSP Architecture Flow
A First Java Server Page Example
Example: Hello JSP
Explanation
<% %>→ Scriptlet tagout→ Implicit object
Implicit Objects in JSP
Implicit objects are predefined objects available in JSP.
List of Implicit Objects
| Object | Purpose |
|---|---|
| request | Client request |
| response | Server response |
| out | Output stream |
| session | Session data |
| application | Application context |
| config | Servlet config |
| page | JSP page |
| pageContext | Context info |
| exception | Error handling |
Scripting in JSP
JSP scripting elements allow Java code in JSP.
Types of Scripting Elements
| Element | Syntax | Use |
|---|---|---|
| Scriptlet | <% %> | Java code |
| Expression | <%= %> | Output |
| Declaration | <%! %> | Variables, methods |
Standard Actions in JSP
Standard actions are predefined JSP tags.
Common JSP Standard Actions
| Action | Purpose |
|---|---|
<jsp:include> | Include resource |
<jsp:forward> | Forward request |
<jsp:useBean> | Create bean |
<jsp:setProperty> | Set bean |
<jsp:getProperty> | Get bean |
Directives in JSP
Directives provide instructions to the JSP container.
Types of Directives
| Directive | Purpose |
|---|---|
| page | Page-level settings |
| include | Include files |
| taglib | Custom tags |
Example
Custom Tag Libraries
Custom tags allow developers to create their own JSP tags.
Advantages
- Reduces Java code in JSP
- Improves readability
- Encourages reusability
Components
- Tag handler class
- TLD file
Example Usage
Summary Table
| Topic | Key Concept |
|---|---|
| Cookies | Client-side tracking |
| HttpSession | Server-side tracking |
| JSP | Dynamic pages |
| Implicit Objects | Predefined objects |
| Custom Tags | Reusable logic |
Exam Tips
- Always mention HTTP is stateless
- Compare Cookies and HttpSession
- Write JSP lifecycle steps
- Use tables and diagrams