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.