Skip to lesson content
Web Technologies › Level 24
LEVEL 24 · JSP

JSP — JavaServer Pages

Turn accepted controller models into readable server-side views.

☕ Java web📄 Server views🧩 EL + Tags🧪 Lab + MCQs
01

Learning Objectives

Page structure

Identify template text, directives and actions.

EL

Read accepted model properties and explicit scopes.

Tags

Render lists and branches without business scriptlets.

Output

Keep untrusted text literal in an HTML view.

02

JSP Executes on the Server

Separate rendering stages
Controller

Validate and prepare a model.

JSP view

Generate response content on the server.

HTTP result

Send HTML and response metadata.

Browser

Render HTML; execute browser scripts.

03

Translation, Compilation and Request Processing

Two kinds of failure
Page source

Directives, tags and expressions.

Translation

Generate and compile implementation.

Request execution

Evaluate the page with current data.

Response

Send the chosen representation.

04

Anatomy of a JSP Page

<%@ page contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" session="false" %>
<%@ taglib prefix="c" uri="jakarta.tags.core" %>
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Study report</title></head>
<body>
  <h1>Study report</h1>
  <p>Learner: <c:out value="${requestScope.lessonReport.learner}" /></p>
</body>
</html>
05

Directives and Character Encodings

<%@ page pageEncoding="UTF-8"
         contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="c" uri="jakarta.tags.core" %>
<%@ include file="fragments/banner.jspf" %>
06

Expressions, Scriptlets and Declarations

<%-- Syntax recognition only; avoid business logic in the page. --%>
<%= 2 + 3 %>                <%-- Java expression output --%>
<% int localCount = 3; %>   <%-- Scriptlet statement --%>
<%! private int count; %>   <%-- Shared member: concurrency concern --%>

<%-- Preferred presentation of an accepted model value: --%>
<c:out value="${requestScope.lessonReport.learner}" />
07

Expression Language and Model Properties

// Illustrative JavaBean getter within a report model class:
public String getLearner() {
    return learner;
}

// Controller fragment after validation and service work:
request.setAttribute("lessonReport", acceptedReport);

// In the view:
// ${requestScope.lessonReport.learner}
08

Scopes and Name Collisions

Scoped attribute lookup
Page

First unqualified candidate.

Request

Controller’s per-request model.

Session

Longer-lived client-associated data.

Application

Shared application attributes.

09

Core Tags: Loop Through Accepted Data

<ul>
  <c:forEach var="course" items="${requestScope.lessonReport.courses}">
    <li>
      <c:out value="${course.title}" />
      <c:choose>
        <c:when test="${course.completed}"> — Complete</c:when>
        <c:otherwise> — Pending</c:otherwise>
      </c:choose>
    </li>
  </c:forEach>
</ul>
10

Conditions and Empty States

<c:choose>
  <c:when test="${empty requestScope.lessonReport.courses}">
    <p>No courses in this report.</p>
  </c:when>
  <c:otherwise>
    <%-- Render the accepted list with c:forEach here. --%>
  </c:otherwise>
</c:choose>
11

Includes, Fragments and Reusable Views

<%-- Translation-time source inclusion: --%>
<%@ include file="fragments/banner.jspf" %>

<%-- Request-time output inclusion: --%>
<jsp:include page="/WEB-INF/views/fragments/help.jsp" />
12

MVC: Let the Controller Prepare the View

// Illustrative controller fragment; acceptedReport is prepared first.
request.setAttribute("lessonReport", acceptedReport);
request.getRequestDispatcher("/WEB-INF/views/study-report.jsp")
       .forward(request, response);
// Forward before committing response output.

Controller

Validate and choose the outcome.

Service

Prepare a bounded report model.

View

Render accepted presentation data.

Repository

Keep data access behind a boundary.

13

Literal Text and Output Contexts

<p>Learner: <c:out value="${requestScope.lessonReport.learner}" /></p>

<%-- JSP comments are not ordinary response HTML comments. --%>
<%-- Keep credentials and private diagnostics out of the source anyway. --%>

<!-- HTML comments can be present in the generated response. -->
14

Translation Errors, Data Errors and Stale Views

Library

Check Jakarta Tags dependency and URI compatibility.

Property

Match bean getters or map keys to the view contract.

Scope

Use the controller’s intended request attribute.

Outcome

Distinguish accepted empty output from missing/bad data.

15

Verify a JSP Application in Its Runtime

Model

Bounded data and deterministic summaries.

JSP integration

Compile directives, tags and expressions.

HTTP output

Check encoding, status and actual content.

Browser

Verify readability, keyboard use and literal text.

16

Premium Visualizer — From Controller Model to JSP View

Follow an accepted report request through validation, model preparation, internal forwarding, JSP presentation and HTML output. This approved five-stage player explains the architecture without executing Java or JSP.

JSP MODEL TO VIEW TRACE
Step 1 of 5
STEP 01

Accept the report request

Validate the incoming learner before preparing data.

GET /report?learner=Demo

What is happening?

The controller handles input; the JSP view is not a substitute for validation.

17

Premium Interactive — Model to JSP-Style View

Idle. JavaScript model; no JSP engine is running.

Lookup trace

No trace yet.

Scope fixtures · public teaching data

No scope lookup yet.

Accepted view model and derived summary

No accepted model.

Illustrative JSP template · not executed here

Response metadata

No response yet.

Generated HTML source · displayed literally

No response body yet.

Safe text-node preview

No preview yet.
18

Debugging — Check the Model Before the Markup

Attribute

Confirm the controller sets lessonReport in request scope.

Shape

Check learner, course list and boolean completion values.

Tags

Verify the actual library and compatible URI.

Output

Compare encoded source with literal visible text.

19

Interview Questions — Flip to Explain

QUESTION

Does the browser execute JSP directives?

Click or press Enter to explain
ANSWER

No. A JSP-capable server processes the page and sends generated response content.

QUESTION

Is JSP translation required for every request?

Click or press Enter to explain
ANSWER

No. Translation/compilation may be performed ahead of time or when needed; request processing uses the resulting implementation.

QUESTION

How does a JavaBean expose an EL learner property?

Click or press Enter to explain
ANSWER

Conventionally through a public getLearner method; map-backed models resolve keys instead.

QUESTION

Why use requestScope.lessonReport explicitly?

Click or press Enter to explain
ANSWER

It identifies the intended per-request model and avoids page/session/application name fallback.

QUESTION

What happens when a page attribute shadows an unqualified name?

Click or press Enter to explain
ANSWER

The page value is selected first. A later scope is not chosen merely because the selected value is malformed.

QUESTION

How do the two include mechanisms differ?

Click or press Enter to explain
ANSWER

The include directive combines source at translation time; jsp:include includes another resource’s output at request time.

QUESTION

Does c:out encode every possible output context?

Click or press Enter to explain
ANSWER

No. Its default XML escaping suits this plain HTML text example; scripts, CSS and URLs need context-specific handling.

QUESTION

Does a local JSP-style renderer verify a real JSP application?

Click or press Enter to explain
ANSWER

No. Actual translation, tag libraries, EL properties, forwarding and HTTP output require runtime integration tests.

20

MCQ Practice — Explain JSP View Decisions

PRACTICE

1. Where is a JSP processed?

PRACTICE

2. What does a taglib directive do?

PRACTICE

3. Which encoding setting describes the JSP source file?

PRACTICE

4. Which unqualified scoped attribute is searched first?

PRACTICE

5. Which expression explicitly selects the controller request model?

PRACTICE

6. What does c:forEach do in the report view?

PRACTICE

7. How is an accepted empty report handled in the lab?

PRACTICE

8. How does a translation-time include work?

PRACTICE

9. Where should business validation and JDBC access be organized?

PRACTICE

10. What happens to request attributes after a redirect automatically?

PRACTICE

11. Which preview technique keeps a learner label literal?

PRACTICE

12. Does the lab evaluate arbitrary JSP or EL source?

21

Extra Practice — Compare View Outcomes

Normal report

Default run: request model, three courses, one complete and two pending. Expect 200.

Empty model

Choose accepted empty. Expect 200, zero counts and a clear no-course message.

Missing model

Choose missing with qualified lookup. Expect 500 and no old course preview.

Scope fallback

Missing + unqualified + no page collision: session fixture wins. Remove session fixture: application wins.

Page shadow

Normal request + valid page collision + unqualified: page report wins. Qualified lookup still uses request.

Malformed shadow

Invalid page collision + unqualified: 500. A later valid request model is not substituted.

Literal learner

Use <Demo> & friends. Expect escaped HTML source, literal text and no injected element.

Rejected query

Try one-character learner or missing/repeated/extra query shape. Expect 400 before scope lookup; source/preview clear.

22

Quick Revision

Server view: JSP generates response content.
Translation: Source becomes an executable implementation.
Directives: Configure processing and libraries.
EL: Resolve model values, not browser scripts.
Explicit scope: Read the intended request attribute.
Loops: Render accepted course rows.
Branches: Separate empty and nonempty output.
Encoding: Match the actual destination context.
MVC: Keep business/data logic outside the view.
Integration: Verify the real JSP runtime separately.
23

Glossary — Flip to Learn

TERM

JSP / Jakarta Pages

Click to see meaning
DEFINITION

JSP / Jakarta Pages

A server-side page technology for generating response content.

TERM

Template text

Click to see meaning
DEFINITION

Template text

Literal page content contributed to the generated response.

TERM

Page directive

Click to see meaning
DEFINITION

Page directive

Page-level processing configuration such as encodings.

TERM

Taglib directive

Click to see meaning
DEFINITION

Taglib directive

A declaration connecting a prefix to a tag library.

TERM

Expression Language

Click to see meaning
DEFINITION

Expression Language

A language for evaluating model values and expressions in supported contexts.

TERM

Scoped attribute

Click to see meaning
DEFINITION

Scoped attribute

An application value associated with page, request, session or application scope.

TERM

JavaBean property

Click to see meaning
DEFINITION

JavaBean property

A conventional property exposed through suitable getter/setter methods.

TERM

Translation unit

Click to see meaning
DEFINITION

Translation unit

The JSP source and source inclusions processed together for translation.

TERM

Core tag action

Click to see meaning
DEFINITION

Core tag action

A tag-library action for operations such as output, iteration or conditional presentation.

TERM

MVC view

Click to see meaning
DEFINITION

MVC view

The presentation component that renders a prepared application model.

24

Final Challenge — Specify an Explicit JSP Report

25

Level 24 Complete?

Explain JSP’s server-side role, directives, EL and core tags; reproduce qualified/unqualified lookup differences and distinguish empty from missing/bad models. Identify the real-runtime checks this browser lab cannot perform. Completion is a local study marker.