LearnAI ToolsCareerPractice BuildsPlayContact
Lesson 1920 min read

JavaBeans in JSP

Learn how JavaBeans and the jsp:useBean, jsp:setProperty, and jsp:getProperty actions keep business logic out of JSP markup.

Introduction

Throughout this course you have stored plain values as request and session attributes. Real applications usually group related data into an object instead — a Student with a name, email, and age, rather than three separate loose attributes. JSP has first-class support for exactly this pattern through JavaBeans and three tightly related actions: jsp:useBean, jsp:setProperty, and jsp:getProperty.

What You Will Learn
  • What makes a plain Java class a "JavaBean".
  • How jsp:useBean creates or finds a bean instance.
  • How jsp:setProperty and jsp:getProperty work with it.
  • Why beans keep business logic out of JSP markup.

What Is a JavaBean?

A JavaBean is simply a Java class that follows three conventions: it has a public no-argument constructor, its fields are private, and each field is exposed through public getX()/setX() methods. That is the entire contract — no special interface or annotation required.

StudentBean.java
// StudentBean.java
package com.programinds.beans;
public class StudentBean {
private String name;
private String email;
private int age;
public StudentBean() { } // required no-arg constructor
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
}

jsp:useBean

jsp:useBean either finds an existing bean instance in the given scope, or creates a new one if none exists yet, and binds it to an id you can reference for the rest of the page.

<jsp:useBean id="student" class="com.programinds.beans.StudentBean" scope="request" />

jsp:setProperty

jsp:setProperty calls the matching setter on the bean. The property name maps to the setter by JavaBean naming convention — property="name" calls setName().

<jsp:setProperty name="student" property="name" value="Alex Parker" />
<jsp:setProperty name="student" property="email" value="alex@mail.com" />
<jsp:setProperty name="student" property="age" value="27" />
Auto-Populating from Request Parameters

A single tag, <jsp:setProperty name="student" property="*" />, sets every bean property whose name matches a submitted form field automatically — a huge shortcut when a form maps directly onto a bean.

jsp:getProperty

jsp:getProperty calls the matching getter and prints its return value directly into the page output.

<p>Name: <jsp:getProperty name="student" property="name" /></p>
<p>Email: <jsp:getProperty name="student" property="email" /></p>
<p>Age: <jsp:getProperty name="student" property="age" /></p>
Rendered Output

Click Run to see what this code prints.

EL Reads Beans Too

Once a bean exists in scope, EL can read it directly without jsp:getProperty at all: ${student.name} and ${student.email} work identically and are the more common modern style.

A Complete Example

Combining useBean with the wildcard setProperty gives a compact pattern for handling a form submission directly on a JSP page.

register.jsp
<!-- register.jsp -->
<jsp:useBean id="student" class="com.programinds.beans.StudentBean" scope="request" />
<jsp:setProperty name="student" property="*" />
<h2>Registration Received</h2>
<p>Name: ${student.name}</p>
<p>Email: ${student.email}</p>
<p>Age: ${student.age}</p>

Bean Scopes

The scope attribute of jsp:useBean controls how long the bean instance lives and where it is visible, mirroring the four EL scopes you already know.

ScopeLifetimeTypical Use
pageThis page only, discarded after renderTemporary, page-local computation
requestThis single request, including forwardsForm processing, passing data to a view
sessionUntil session ends or times outLogged-in user profile, cart
applicationEntire application lifetimeShared configuration, caches

Common Mistakes

Avoid These Mistakes
  • Forgetting the no-argument constructor — jsp:useBean will fail to instantiate the class without one.
  • Mismatching property names and setter names, e.g. property="Name" when the method is setName (case matters for the first letter only in the property name, not the method).
  • Putting real business logic, like database calls, directly inside a JavaBean used purely for data — keep beans as simple data carriers.
  • Using page scope when you actually need the bean to survive a forward, which requires at least request scope.

Best Practices

  • Keep JavaBeans as plain data holders — no database or network calls inside them.
  • Prefer EL (${bean.property}) over jsp:getProperty in new code — it is shorter and equally standard.
  • Match scope to how long the data genuinely needs to live.
  • Use the property="*" shortcut only when form field names exactly match bean property names.

Frequently Asked Questions

No, they are unrelated despite the similar name. A JavaBean here is just a simple class following getter/setter conventions; EJB is a much heavier, separate technology.

No, it always calls the public no-argument constructor, which is why that constructor is required.

No. jsp:setProperty automatically converts common types like int, boolean, and double from the incoming String value.

Key Takeaways

  • A JavaBean is a plain class with a no-arg constructor and getter/setter pairs.
  • jsp:useBean finds or creates a bean instance under a given id and scope.
  • jsp:setProperty and jsp:getProperty call the matching setters and getters.
  • Beans let JSP handle structured data without embedding business logic in markup.

Summary

JavaBeans give your data a clean, reusable shape. Next, you will look at custom tags — the mechanism JSP provides for packaging reusable markup logic, the way JSTL's own tags were built.

Next Lesson →

Custom Tags Basics