Use Bean in JSP Page

With this example we are going to demonstrate how to use a Bean in a JSP page. JavaServer Pages (JSP) is a server-side programming technology that enables the creation of dynamic, platform-independent method for building Web-based applications. JSP have access to the entire family of Java APIs, including the JDBC API to access enterprise databases. In short, to use a Bean in a JSP page you should:

  • Create a Java Bean. The Java Bean is a specially constructed Java class that provides a default, no-argument constructor, implements the Serializable interface and it has getter and setter methods for its properties.
  • Create a jsp page, using the <%code fragment%> scriptlet. It can contain any number of JAVA language statements, variable or method declarations, or expressions that are valid in the page scripting language.
  • Use the useBean action to declare the JavaBean for use in the JSP page. Once declared, the bean becomes a scripting variable that can be accessed by both scripting elements and other custom tags used in the JSP.
  • Use the getProperty action to access get methods and setProperty action to access set methods of the bean.

Let’s take a look at the code snippets of a sample Bean and a JSP page that uses it, below:

package com.javacodegeeks.snippets.enterprise;

import java.util.Date;

public class SampleBean {
	private String param1;
	private Date param2 = new Date();
	public String getParam1() {
		return param1;
	public void setParam1(String param1) {
		this.param1 = param1;
	public Date getParam2() {
		return param2;
	public void setParam2(Date param2) {
		this.param2 = param2;
	public String toString() {
		return "SampleBean [param1=" + param1 + ", param2=" + param2 + "]";



<%@ page language="java" contentType="text/html;charset=UTF-8" %>
<%@ page import="com.javacodegeeks.snippets.enterprise.SampleBean"%>


	<title>Java Code Geeks Snippets - Use a Bean in JSP Page</title>


	<jsp:useBean id="sampleBean" class="com.javacodegeeks.snippets.enterprise.SampleBean" scope="session">
	    <%-- intialize bean properties --%>
	    <jsp:setProperty name="sampleBean" property="param1" value="value1" />
	Sample Bean: <%= sampleBean %>
	param1: <jsp:getProperty name="sampleBean" property="param1" />
	param2: <jsp:getProperty name="sampleBean" property="param2" />





Sample Bean: SampleBean [param1=value1, param2=Thu Nov 17 21:28:03 EET 2011]
param1: value1 param2: Thu Nov 17 21:28:03 EET 2011

This was an example of how to use a Bean in a JSP page.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button