Home » Enterprise Java » jsp » Use Bean in JSP Page

About Byron Kiourtzoglou

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.

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:
SampleBean.java

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;
	}
	
	@Override
	public String toString() {
		return "SampleBean [param1=" + param1 + ", param2=" + param2 + "]";
	}

}

UseBean.jsp

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

<html>

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

<body>

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

</body>

URL:

http://myhost:8080/jcgsnippets/UseBean.jsp

Output:

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.

(+1 rating, 1 votes)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of