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.
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; }
}
- The fields are private.
name,courseandageare the bean's properties. - There's a public constructor with no arguments, because JSP creates the object by calling it.
- Each property has a getter and setter,
getNameandsetNameand 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 aStudentobject calledstudent.<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
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
| Scope | Object | Lives | Seen by | Good for |
|---|---|---|---|---|
| page | PageContext | one JSP while it runs | that JSP only | a value that builds one page |
| request | HttpServletRequest | one request, travels with a forward | every JSP in that request | data a servlet hands to a JSP |
| session | HttpSession | until timeout or the browser closes | one browser | the logged-in user, a cart |
| application | ServletContext | until the app stops | every browser | app-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.
useBeancan't create the bean andstudent.jspfails 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
setPropertyline. 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
countstatic. Then all four counters share one number and show the same total.
StudyEasy AI Community
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
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.
