jsp

Use Bean in JSP Page

Photo of Byron KiourtzoglouByron KiourtzoglouNovember 11th, 2012Last Updated: September 9th, 2013
0 1,906 1 minute read

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 theuseBean 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 thegetProperty action to access get methods andsetProperty 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;}@Overridepublic 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 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.

Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
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 ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Byron KiourtzoglouByron KiourtzoglouNovember 11th, 2012Last Updated: September 9th, 2013
0 1,906 1 minute read
Photo of 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 atJava Code Geeks.

Related Articles

Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.