J2EE Tutorial - Java Bean

To bring out the differences between the different appraoaches, we will consider a simple case where the user types his name in the textbox

J2EE Tutorial - Java Bean


To bring out the differences between the different appraoaches,  we will consider a simple case where the user types his name in the textbox(text1) in the html form and submits it to the webserver. The JSP then invokes the greeter bean in the webserver . The greeter bean encapsulates the  'Business Logic' ,and so it  appends a greeting message to the name thus submitted and sends it back to the user.

What is a  Javabean?  A Javabean is just a java class  with the following requirements.

   i) It has a public no-args constructor

  ii) It has 'set' and 'get' methods for its properties.

  iii) It may have any general functions.

  iv) If required it must be Serializable.


  It is not necessary always that a bean should have properties.

   If there are no properties, we need not provide 'set' & 'get' methods either.

  ( Even the no-args constructor is provided by the compiler by default!)

  If  the bean uses library classes alone, it is automatically serializable.

  In that case, it becomes  just a class for encapsulating some functionality (ie) business logic.


   Such a bean , may or may not have a visual representation. As the field has  moved away from the desktop, to webserver, there is no point in providing a visual represenation for a component which works in server side as the user cannot interact with it directly. (Such a bean  used by jsp  in webserver, is exactly similar to ASP component). ( So  no worry about jar file ,BDK, BeanBox etc traditionally associated with JavaBean)

   Let us now create  such a bean. The Tomcat server searches for any classes in  c:\tomcat\webapps\root\web-inf\classes  folder. We create a subfolder under classes folder and name it 'ourbeans'.

   Therefore, we specify  the package information in the first line of our jspbean as  :

   package   ourbeans;


package ourbeans;

public  class greeterbean


  public greeterbean()   {  }

  public String  greetme(String s)


  return   "How are you?...."+s;




To compile the above file :

> javac greeterbean.java

We get greeterbean.class

We move this class file to:



We will now create  greeter.htm & greeter.jsp and place them in:





   <form  method=post  action=greeter.jsp>

   <input   type=text  name='text1'>

   <input   type=submit>







    <jsp:useBean   id="greeter1"  class="ourbeans.greeterbean"  />


   String   s = request.getParameter("text1");  

     String   a = greeter1.greetme(s);





  In the above example, the business logic was invoked through an instance of 'greeter' bean class. So, it is object-oriented approach. Where was the method invoked? It was invoked in the Webserver.Where was the object available? The object was available in webserver itself.(ie) 'locally'.

This point is worth noting carefully. For  this reason, the jspbean method cannot be classified as 'Distributed Object ' Technology. Because, in Distributed Object Technology, the object on which the method is invoked locally, should be available in a remote server and not locally .( Why is this distinction so important? Is it important atall? If we are able to answer these questions  to ourselves convincingly, we have truly grasped the essence of Enterprise computing  but   not otherwise. That is why this point is being laboured.

Share on Google+Share on Google+

J2EE Tutorial - Java Bean

Posted on: May 24, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: J2EE Tutorial - Java Bean   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
Aatish Pandya
June 11, 2011
get/set Property usage not Explained?

Sir, The usage of setProperty and getProperty mathods and param tag usage, etc. is not explained along with example?
July 5, 2011

We cannot call instance methods in Java beans directly from a jsp page. It gives error. I tried this small application. it doesnt work.
rahul sharma
October 14, 2011
on line tarining

sir, I would like to learn java bean from your team, what have you any faclity for online training by yout team , if yes so please contact me
March 13, 2012

package com.ilptsi.um.dbutility; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Connection; public class UtilityDb { public static Connection con=null; public static java.sql.Statement stm=null; // Getting connection reference public static Connection getConnectionRef() { try { Class.forName("oracle.jdbc.driver.OracleDriver"); String url="jdbc:oracle:thin:@"; String username="H118ORAUSER1D"; String password="tcshyd"; con=DriverManager.getConnection(url, username, password); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return con; } public static java.sql.Statement getStatement() { con=getConnectionRef(); try { stm=con.createStatement(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return stm; } public static void closeResources() { try { stm.close(); con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
April 8, 2012

any one send how to retrive bean values using Arraylist
Maulik Tanna
May 20, 2014
JavaBean Methods

Sir, I would like to know the all the methods of JavaBean ? Please reply.. Methods of Javabean ?