Single logo
  • Courses
  • Blog
  • About
Join on SkoolJoin on Skool

In this topic

JSP and Servlets
  • Servlets and JSP: What a Servlet Container Does and How a JSP Becomes a Servlet
  • Set Up Tomcat 11 and a Maven Web Project in IntelliJ IDEA
  • The Servlet Request Lifecycle: @WebServlet, web.xml, init, doGet and destroy
  • Servlet Request Parameters, and Forward vs Redirect
  • Run a Java Web App in Docker: Tomcat 11 With One Compose Command
  • Sessions, Cookies and Filters in Java Servlets: Log In Once, Stay Logged In
  • Servlets and JSP: What a Servlet Container Does and How a JSP Becomes a Servlet
  • JSP Scripting: Expressions, Scriptlets, Declarations, Comments and Includes
  • JavaBeans and JSP Scopes: Page, Request, Session and Application
  • JSP Expression Language Step by Step: ${...} for Beans, Maps, Lists, Params and Scopes
  • JSTL Core Tags: c:set, c:out, c:if, c:choose and c:forEach
  • JSTL Functions and Formatting: fn:length, fn:toUpperCase, fn:contains, fmt:formatNumber and fmt:formatDate
  • Maintaining and Migrating Legacy JSP With AI: Explain the Page, Refactor to a DAO, Servlet and JSTL, Then Move It to React
  • MVC and the Front Controller With Servlets and JSP
  • Forms With JSP and Servlets: Handle the POST and Validate on the Server
  • Mini App With Servlets and JSP: Login, Logout and a Shared Web Template
  • SQLite and a JDBC Connection Pool in a Java Web App
  • Users CRUD With Servlets and JSP, Part 1: Add and List Through a DAO
  • Users CRUD With Servlets and JSP, Part 2: Update and Delete
  1. Blog
  2. /
  3. JSP and Servlets
  4. /
  5. JSP

JavaBeans and JSP Scopes: Page, Request, Session and Application

A Student bean filled from a form with jsp:useBean and setProperty, then one Counter class kept in four scopes. Three visits, two browsers and an include page show exactly who sees which number.

Chaand Sheikh

·27 November 2026·Updated 28 November 2026JSP

What you will learn

  • Write a JavaBean that JSP tags can create, fill and read
  • Fill a bean from a form with jsp:useBean and setProperty property="*"
  • Predict what page, request, session and application scope counters show across visits and browsers
  • See why page scope is per JSP and request scope is shared by included JSPs

Before you start

  • JSP Scripting: Expressions, Scriptlets, Declarations
  • Forms With JSP and Servlets

A JavaBean is a class with a standard shape, usually standing for one thing, such as a student with a name, a course and an age. JSP tags know that shape, so they can create a bean, fill it from a form and read it back without you writing any mapping code. Then there's the question of where the bean lives and who can see it. That's its scope, and JSP gives you four.

A plain class, three rules

package org.studyeasy.web;

// A JavaBean: a plain class that JSP tags can create and fill by name
public class Student {

    // private fields: the bean's properties
    private String name;
    private String course;
    private int age;

    // public and no arguments: jsp:useBean calls this to create it
    public Student() { }

    // getName / setName: the tags find a property by these names
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }

    public String getCourse() { return course; }
    public void setCourse(String course) { this.course = course; }

    public int getAge() { return age; }
    // the form sends text: JSP turns "22" into the int 22 for us
    public void setAge(int age) { this.age = age; }
}
  1. The fields are private. name, course and age are the bean's properties.
  2. There's a public constructor with no arguments, because JSP creates the object by calling it.
  3. Each property has a getter and setter, getName and setName and so on.

Follow these and the tags can work with the object just by property name.

Field names = property names

<!DOCTYPE html>
<html>
<head><title>New student</title></head>
<body>
<h2>New student</h2>
<!-- each input name matches a bean property: name, course, age -->
<form method="post" action="student.jsp">
    <p>Name: <input name="name" value="Emma Clarke"></p>
    <p>Course: <input name="course" value="Web Development"></p>
    <p>Age: <input name="age" value="22"></p>
    <button>Save</button>
</form>
</body>
</html>

The form posts straight to student.jsp, with no servlet involved yet. The input names are name, course and age, exactly the bean's property names. That match is what makes the mapping work.

useBean, setProperty and getProperty

<%@ page contentType="text/html;charset=UTF-8" %>
<%-- create a Student object called "student" (or reuse one) --%>
<jsp:useBean id="student" class="org.studyeasy.web.Student"/>
<%-- property="*": copy every form field into the property of that name --%>
<jsp:setProperty name="student" property="*"/>
<html>
<head><title>Student saved</title></head>
<body>
<h2>Student saved</h2>
<%-- getProperty calls the getter and prints the value --%>
<p>Name: <jsp:getProperty name="student" property="name"/></p>
<p>Course: <jsp:getProperty name="student" property="course"/></p>
<p>Age: <jsp:getProperty name="student" property="age"/></p>
<p>Next year: <%= student.getAge() + 1 %></p>
</body>
</html>

Click Save on the form:

Student saved
Name: Emma Clarke
Course: Web Development
Age: 22
Next year: 23
  • <jsp:useBean id="student" class="...Student"/> gives you a Student object called student.
  • <jsp:setProperty name="student" property="*"/> copies every form field into the property of the same name. JSP calls the setters for you.
  • <jsp:getProperty name="student" property="name"/> calls the getter and prints the value.

The form sends text, but setAge takes an int, so JSP converted "22" to 22. That's why student.getAge() + 1 prints 23.

One counter, four scopes

For scopes we need a second bean:

package org.studyeasy.web;

// A second bean: one number and a way to add one to it
public class Counter {

    private int count;

    public Counter() {
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public void increment() {
        count++;
    }
}

Now the same class, stored in four different places:

<%@ page contentType="text/html;charset=UTF-8" %>
<%-- the SAME bean class, kept in four different places --%>
<jsp:useBean id="pageHits" class="org.studyeasy.web.Counter"
             scope="page"/>
<jsp:useBean id="requestHits" class="org.studyeasy.web.Counter"
             scope="request"/>
<jsp:useBean id="sessionHits" class="org.studyeasy.web.Counter"
             scope="session"/>
<jsp:useBean id="appHits" class="org.studyeasy.web.Counter"
             scope="application"/>
<%
    // useBean found the counter that already lives there, or made a new one
    pageHits.increment();
    requestHits.increment();
    sessionHits.increment();
    appHits.increment();
%>
<html>
<head><title>Four scopes</title></head>
<body>
<h2>Four scopes</h2>
<p>page: <jsp:getProperty name="pageHits" property="count"/></p>
<p>request: <jsp:getProperty name="requestHits" property="count"/></p>
<p>session: <jsp:getProperty name="sessionHits" property="count"/></p>
<p>application: <jsp:getProperty name="appHits" property="count"/></p>
</body>
</html>

The trick is in useBean: it first looks in the given scope. If a counter is already there, it uses that one; if not, it makes a new one. After three visits in one browser:

page: 1
request: 1
session: 3
application: 3

Four scopes, two browsers

Now open the page in two browsers: three visits in browser A, two in browser B, then A again.

A, visit 3:  page: 1  request: 1  session: 3  application: 3
B, visit 2:  page: 1  request: 1  session: 2  application: 5
A, visit 4:  page: 1  request: 1  session: 4  application: 6
One application box at the top, shared by everyone, shows appHits 6. Below it two session boxes: browser A with sessionHits 4 and a JSESSIONID cookie, browser B with sessionHits 2. Inside each, a request and a page that start at 1 on every visit. application (ServletContext): one for the whole app appHits = 6 session of browser A sessionHits = 4, cookie JSESSIONID session of browser B sessionHits = 2 requestrequestHits = 1 pagepageHits = 1 requestrequestHits = 1 pagepageHits = 1
Application is shared by everyone, a session belongs to one browser, and request and page start again on every visit.

Browser A carries a JSESSIONID cookie, and that's how Tomcat finds its session. Browser B gets its own session, so its session counter starts from 1 and reaches 2. The application counter is shared, so it counts both browsers: 5, then 6 when A comes back.

What resets what:

  • Refresh in the same browser: page and request stay at 1, session and application go up.
  • A new browser: session starts again at 1, application carries on.
  • Restart the app: everything starts again, because even application scope lives only until the app stops.

For a real hit counter you'd save the number in a database. This example is for seeing the scopes.

Page vs request: who sees the value?

Page and request both show 1 above, so they look the same. They aren't. Here is a header with one counter of each:

<%@ page contentType="text/html;charset=UTF-8" %>
<jsp:useBean id="pageCount" class="org.studyeasy.web.Counter" scope="page"/>
<jsp:useBean id="requestCount" class="org.studyeasy.web.Counter" scope="request"/>
<% pageCount.increment(); requestCount.increment(); %>
<p>header.jsp | page: <%= pageCount.getCount() %> | request: <%= requestCount.getCount() %></p>

main.jsp and footer.jsp are the same apart from the name. shop.jsp includes all three in one request, plus a button that fetches main.jsp again with a little JavaScript:

<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head><title>Shop</title></head>
<body>
<%-- one request runs all three pages --%>
<jsp:include page="header.jsp"/>
<jsp:include page="main.jsp"/>
<jsp:include page="footer.jsp"/>

<button onclick="askAgain()">Ask for main.jsp again</button>
<p id="answer"></p>
<script>
    async function askAgain() {
        const response = await fetch("main.jsp");   // a NEW request
        document.getElementById("answer").innerHTML = await response.text();
    }
</script>
</body>
</html>

Open shop.jsp and click the button:

header.jsp | page: 1 | request: 1
main.jsp   | page: 1 | request: 2
footer.jsp | page: 1 | request: 3
(button)   main.jsp | page: 1 | request: 1

Every JSP gets its own page counter, so header, main and footer each show 1. All three run in the same request, so they share one request counter: 1, 2, 3. The button sends a new request, which gets a new request counter, so it's back to 1 and 1. An include is the same request; a reload or a fetch is a new one.

The four scopes

ScopeObjectLivesSeen byGood for
pagePageContextone JSP while it runsthat JSP onlya value that builds one page
requestHttpServletRequestone request, travels with a forwardevery JSP in that requestdata a servlet hands to a JSP
sessionHttpSessionuntil timeout or the browser closesone browserthe logged-in user, a cart
applicationServletContextuntil the app stopsevery browserapp-wide settings and totals

Pick the smallest scope that works. The wider the scope, the more people share the value.

Common mistakes

  • A private constructor. useBean can't create the bean and student.jsp fails with "The value for the useBean class attribute ... is invalid".
  • A form field name that doesn't match a property, such as courseName. There's no error at all; the page just shows "Course: null".
  • Forgetting the setProperty line. Nothing is copied: Name: null, Course: null, Age: 0.
  • Leaving out scope="session". The default is page, so the counter is new on every visit and stays at 1.
  • Making count static. Then all four counters share one number and show the same total.

Keep going in JSP and Servlets · 9 of 19

Previous
JSP Scripting: Expressions, Scriptlets, Declarations, Comments and Includes
Next post
JSP Expression Language Step by Step: ${...} for Beans, Maps, Lists, Params and Scopes
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

Learn AI engineering, share what you build, and connect with other learners.

  • Free resources: lesson files, notebooks and code

  • Ask questions and join engineering discussions

  • Meet real people learning and building with AI

Join free on Skool

Related course on Udemy

FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE

Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)

Related posts

Servlets and JSP: What a Servlet Container Does and How a JSP Becomes a Servlet

A JSP looks like HTML with a little Java, yet Tomcat runs it as a servlet. See what a servlet container does for each request, what the generated hello_jsp.java looks like, what a .war holds, and Tomcat vs TomEE.

JSP Scripting: Expressions, Scriptlets, Declarations, Comments and Includes

The JSP tags you'll read in older Java web apps: expressions, scriptlets, declarations, page imports, two kinds of comment, and the include directive versus the jsp:include action, each shown with what Tomcat turns it into.

JSP Expression Language Step by Step: ${...} for Beans, Maps, Lists, Params and Scopes

Expression Language prints a value in a JSP with ${...} and no Java on the page. Read a Student bean, a price map, a tag list and ?city=Boston, run small checks, and see the order EL searches the scopes.

JSTL Core Tags: c:set, c:out, c:if, c:choose and c:forEach

Five JSTL core tags let a JSP decide and repeat with no Java on the page. Add the jars to pom.xml, then use c:set, c:out (and see why it is safer than plain EL), c:if, c:choose and c:forEach on three orders.

On this page

  • A plain class, three rules
  • Field names = property names
  • useBean, setProperty and getProperty
  • One counter, four scopes
  • Four scopes, two browsers
  • Page vs request: who sees the value?
  • The four scopes
  • Common mistakes
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

Learn AI engineering, share what you build, and connect with other learners.

  • Free resources: lesson files, notebooks and code

  • Ask questions and join engineering discussions

  • Meet real people learning and building with AI

Join free on Skool

Related course on Udemy

FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE

Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)

Single logo

Empowering Education, One Course at a Time

About
About usContact us
Legal
Terms of UsePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.