Web Technologies
Comprehensive exam revision notes for BCA-303N Unit 5 (JavaServer Pages (JSP) & JavaBean Architecture). Includes Section A (2M), Section B (5M), and Section C (15M) questions, answers, and diagrams.
Web Technologies
JavaServer Pages (JSP) & JavaBean Architecture
Core Definitions & Key Concepts
What is JavaServer Pages (JSP) and how does it relate to Servlets
JSP is a server-side presentation technology that allows developers to write standard HTML embedded with dynamic Java tags. At runtime, the JSP engine translates the JSP file into an equivalent Java Servlet source file (`.java`), which is then compiled into bytecode (.class).
The JSP Lifecycle phases
- Translation: JSP page is translated into a Java Servlet source file (
_jsp.java). - Compilation: Servlet source is compiled into bytecode (
_jsp.class). - Loading & Instantiation: Servlet class is loaded into memory.
- Initialization:
jspInit()is invoked once. - Execution:
_jspService(req, res)is called for each client request. - Destruction:
jspDestroy()cleans up resources when unloaded.
Name the three types of JSP Directives
<%@ page ... %>: Defines page-dependent attributes (language, contentType, import, errorPage).<%@ include ... %>: Static include of another file at translation time.<%@ taglib ... %>: Declares custom tag libraries (such as JSTL).
What are the three core JSP Scripting Elements
- Scriptlet (`<% ... %>`): Arbitrary Java statements executed inside
_jspService(). - Expression (`<%= ... %>`): Evaluates an expression and prints to output (no semicolon).
- Declaration (`<%! ... %>`): Declares class-level instance variables and methods outside
_jspService().
List the 9 Implicit Objects in JSP
request(HttpServletRequest)response(HttpServletResponse)out(JspWriter)session(HttpSession)application(ServletContext)config(ServletConfig)pageContext(PageContext)page(Object / this)exception(Throwable- only on error pages).
What constitutes a valid JavaBean class
- Must implement
java.io.Serializable. - Must have a public zero-argument constructor.
- All properties must be private and accessible via standard public
getProp()andsetProp()methods.
What are the three standard JSP Action tags for JavaBeans
<jsp:useBean id="..." class="..." scope="..."/>: Locates or instantiates a bean.<jsp:setProperty name="..." property="..."/>: Sets property value.<jsp:getProperty name="..." property="..."/>: Retrieves property value and writes to output.
The four scopes in JSP
- `page`: Accessible only within the current JSP page.
- `request`: Accessible across pages handling the same request (
forward). - `session`: Accessible across multiple requests within the user's active session.
- `application`: Accessible to all users across the entire web application.
What is the JSP Expression Language (EL) syntax and purpose
EL uses the syntax ${expression} (e.g., ${student.name}) to access JavaBean properties, implicit objects, and collection elements simply and cleanly without requiring Java scriptlets.
What is JSTL (JSP Standard Tag Library)
JSTL is a standardized collection of custom JSP tags providing core functions (loops <c:forEach>, conditionals <c:if>), XML processing, formatting (dates/numbers), and SQL access.
Conceptual & Architectural Analysis
The Translation and Compilation Lifecycle of a JSP Page
[ Client Request: index.jsp ]
|
v
[ JSP Engine / Container ]
|
+---> Checks: Is Translated Servlet up to date?
| If NO:
| 1. [ Translation Phase ]: Translates index.jsp -> index_jsp.java
| 2. [ Compilation Phase ]: Compiles index_jsp.java -> index_jsp.class
|
v
[ Load & Initialize ]
|
+---> jspInit() (Executed once)
|
v
[ Request Handling ]
|
+---> _jspService(request, response) (Invoked for every request)
|
v
[ Container Shutdown ]
|
+---> jspDestroy() (Executed once)Compare JSP Scripting Elements: Scriptlet vs Expression vs Declaration
| Element | Syntax | Generated Location in Servlet | Example |
|---|---|---|---|
| Scriptlet | <% ... %> | Inside _jspService() method | <% int count = 10; %> |
| Expression | <%= ... %> | Inside _jspService() wrapped in out.print(...) | <%= Math.sqrt(16) %> |
| Declaration | <%! ... %> | Outside _jspService() as Class Member | <%! private int visits = 0; %> |
JSP Directive Tags (page, include, taglib) with Attributes
1. Page Directive (<%@ page ... %>):
Configures page-wide attributes:
1<%@ page language="java" contentType="text/html; charset=UTF-8"2 import="java.util.List, com.mjpru.Student"3 isErrorPage="false" errorPage="error.jsp" session="true" %>2. Include Directive (<%@ include file="header.jsp" %>):
Merges target file into current page during translation (static include).
3. Taglib Directive (<%@ taglib uri="..." prefix="c" %>):
Imports tag libraries such as JSTL core (xmlns:c).
The 4 Scopes of <jsp:useBean> with Lifespan Comparison
| Scope | Lifespan | Stored In | Shared With |
|---|---|---|---|
| `page` | Duration of single JSP page execution | PageContext | Current page only |
| `request` | Lifetime of the HTTP request | HttpServletRequest | Current page and forwarded JSPs |
| `session` | User session duration (browser session) | HttpSession | All pages visited by same user |
| `application` | Server uptime (application lifetime) | ServletContext | All users across entire web app |
JSTL Core Tags (<c:if>, <c:forEach>, <c:choose>) with Examples
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!-- Conditional Rendering -->
<c:if test="${score >= 50}">
<p style="color:green;">Status: Passed</p>
</c:if>
<!-- Multi-way Branching -->
<c:choose>
<c:when test="${marks >= 75}"><p>Distinction</p></c:when>
<c:when test="${marks >= 60}"><p>First Class</p></c:when>
<c:otherwise><p>Second Class / Pass</p></c:otherwise>
</c:choose>
<!-- Iteration over Collections -->
<c:forEach var="item" items="${productList}">
<li>${item.name} - ₹${item.price}</li>
</c:forEach>In-Depth Analysis & Implementation
Develop a Complete Enterprise JavaBean and JSP Application Demonstrating <jsp:useBean>, Property Population, and EL Rendering
Complete JavaBean and JSP Implementation
1. JavaBean Class: StudentBean.java
1package com.mjpru.bca.beans;2
3import java.io.Serializable;4
5public class StudentBean implements Serializable {6 private static final long serialVersionUID = 1L;7 private int rollNo;8 private String studentName;9 private double marks;10
11 public StudentBean() {} // Mandatory no-arg constructor12
13 public int getRollNo() { return rollNo; }14 public void setRollNo(int rollNo) { this.rollNo = rollNo; }15
16 public String getStudentName() { return studentName; }17 public void setStudentName(String studentName) { this.studentName = studentName; }18
19 public double getMarks() { return marks; }20 public void setMarks(double marks) { this.marks = marks; }21
22 public String getGrade() {23 if (marks >= 80) return "A+";24 if (marks >= 60) return "A";25 if (marks >= 50) return "B";26 return "C";27 }28}2. JSP Page: student_entry.jsp (Input Form)
1<!DOCTYPE html>2<html>3<head><title>Student Registration</title></head>4<body>5 <h2>Enter BCA Student Details</h2>6 <form action="student_display.jsp" method="POST">7 Roll Number: <input type="text" name="rollNo"><br><br>8 Student Name: <input type="text" name="studentName"><br><br>9 Marks (out of 100): <input type="text" name="marks"><br><br>10 <input type="submit" value="Register Student">11 </form>12</body>13</html>3. JSP Page: student_display.jsp (Processing & Display via Bean & EL)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!-- 1. Instantiate or Locate JavaBean in Request Scope -->
<jsp:useBean id="student" class="com.mjpru.bca.beans.StudentBean" scope="request"/>
<!-- 2. Automatically populate all bean properties matching HTTP POST parameter names -->
<jsp:setProperty name="student" property="*"/>
<!DOCTYPE html>
<html>
<head>
<title>Student Profile Display</title>
<style>
.profile-card { border: 1px solid #0284c7; padding: 20px; border-radius: 8px; max-width: 400px; }
</style>
</head>
<body>
<div class="profile-card">
<h2>Student Examination Record</h2>
<!-- Render properties using modern Expression Language (EL) -->
<p><strong>Roll Number:</strong> ${student.rollNo}</p>
<p><strong>Student Name:</strong> ${student.studentName}</p>
<p><strong>Marks:</strong> ${student.marks} / 100</p>
<p><strong>Assigned Grade:</strong> <span style="color:blue;">${student.grade}</span></p>
</div>
</body>
</html>Error Handling in JSP: errorPage and isErrorPage Directives with Custom Error Handler Architecture
Declarative Error Handling in JSP
[ Business JSP (calculate.jsp) ] -- Throws Runtime Exception --> [ Container ]
|
v
[ Client Browser ] <--- Renders User-Friendly Message <--- [ error_handler.jsp ]1. Business Page (calculate.jsp):
Specifies the error handling page via the page directive:
1<%@ page errorPage="error_handler.jsp" %>2<% 3 int a = 100;4 int b = 0;5 int c = a / b; // Causes java.lang.ArithmeticException: / by zero6%>2. Custom Error Handler Page (error_handler.jsp):
Must declare isErrorPage="true", which enables access to the implicit exception object:
1<%@ page isErrorPage="true" contentType="text/html; charset=UTF-8" %>2<!DOCTYPE html>3<html>4<head><title>System Error Occurred</title></head>5<body style="font-family:sans-serif; background:#fff1f2;">6 <h2 style="color:#e11d48;">Application Error</h2>7 <p>We apologize for the inconvenience. Our engineering team has been notified.</p>8 <div style="background:#fff; border:1px solid #fecdd3; padding:15px; border-radius:6px;">9 <p><strong>Exception Type:</strong> <%= exception.getClass().getName() %></p>10 <p><strong>Error Message:</strong> <%= exception.getMessage() %></p>11 </div>12</body>13</html>Compare Static Include (<%@ include %>) vs Dynamic Include (<jsp:include>) with Architectural Trade-offs
Static Include vs Dynamic Include
| Criteria | Static Include (<%@ include file="..." %>) | Dynamic Include (<jsp:include page="..." />) |
|---|---|---|
| Execution Phase | Translation Phase (Source file level) | Runtime / Request Phase (Invocation level) |
| Mechanism | Copies raw source code text of child into parent file before compilation | Intercepts at runtime via RequestDispatcher.include() |
| Output Servlets | Produces a single translated Servlet class | Produces two separate independent Servlet classes |
| Dynamic Parameters | Cannot pass parameters (<jsp:param> invalid) | Can pass runtime parameters via <jsp:param> |
| File Changes | Modification in included file may not recompile parent automatically | Changes in child page reflect immediately on next request |
| Best Used For | Static copyright banners, common HTML headers | Dynamic user profile widgets, navigation bars with active state |
Important Exam Points & Quick Revision
Examiner high-yield focus: JSP Lifecycle (guaranteed 10 marks), 9 Implicit objects table, and Static vs Dynamic include comparison.
100% left on this page — finishing it closes the gap for JavaServer Pages (JSP) & JavaBean Architecture.
JavaServer Pages (JSP) & JavaBean Architecture still counts as a weak topic until you finish this note and answer its questions.
Practise this unitTest Your Understanding
Now that you've read through these notes, practice unit MCQs to identify weak points and solidify your learning before exams.