Blog about Programming Languages & Coding

Blog about Programming Languages & Coding
Contents for Computer Science, IT, B.Sc. CS & IT, M.Sc. CS & IT, MCA, BE CS & IT, ME CS & IT , Interview Questions, Books and Online Course Recommendations from Udemy, Coursera, etc

Java Bean


JavaBean


A JavaBean is reusable, platform independent component that can be manipulated visually in a builder tool.


In computing, based on the Java Platform, JavaBeans are classes that encapsulate many objects into a single object (the bean). In simple words JavaBean is nothing but a Java class. When these JavaBeans are used in other applications, the internal working of such components are hidden from the application developer.


Example:

All Swing and AWT classes are JavaBeans. GUI components are ideal JavaBeans.

Components of JavaBeans


The classes that contained definition of beans is known as components of JavaBeans. These classes follows certain design conventions. It includes properties, events, methods and persistence. There are two types of components, GUI based and non GUI based. For instance JButton is example of a component not a class.


Properties (date members): Property is a named attribute of a bean, it includes color, label, font, font size, display size. It determines appearance, behavior and state of a bean.


Methods: Methods in JavaBeans are same as normal Java methods in a class. It doesn’t follow any specific naming conventions. All properties should have accessor and getter methods.


Events: Events in JavaBeans are same as SWING/AWT event handling.

Persistence: Serializable interface enables JavaBean to store its state.


JavaBean has no argument constructor.

beanComponent.png

     JavaBean component

JavaBeans Properties

JavaBean property can be access by the user of the object, it can be read, write, read only or write only. We can access these JavaBeans properties with the help of getPropertyName() method also known as getter or accessor and setPropertyName() method known as setter or mutator written in implementation class of bean.


getPropertyName(): For example, if property name is Title, your method name would be geTitle().

setPropertyName(): For example, if property name  is Title, your method name would be setTitle().


Advantages of JavaBeans

Following are some advantages of JavaBeans:

  1. Reusability in different environments.

  2. Used to create applet, servlet, application or other components.

  3. JavaBeans are dynamic, can be customized.

  4. Can be deployed in network systems


Example of JavaBeans

Before going to write a JavaBean, here are some basic rules. A JavaBean should be public, should has no argument default constructor and should implement serializable interface. Keep these basic rules in mind before writing a JavaBean.


public class StudentsBean implements java.io.Serializable {

   private String firstName = null;

   private String lastName = null;

   private int age = 0;


   public StudentsBean() {

   }

   public String getFirstName(){

      return firstName;

   }

   public String getLastName(){

      return lastName;

   }

   public int getAge(){

      return age;

   }

   public void setFirstName(String fn){

      firstName = fn;

   }

   public void setLastName(String ln){

      lastName = ln;

   }

   public void setAge(int a){

      age = a;

   }

}


Accessing JavaBeans

The useBean action declares a JavaBean for use in a JSP. 

<html>

<body>

      <jsp:useBean id = "students" class = "StudentsBean"> 

         <jsp:setProperty name = "students" property = "firstName" value = "KING"/>

         <jsp:setProperty name = "students" property = "lastName" value = "KHAN"/>

         <jsp:setProperty name = "students" property = "age" value = "52"/>

      </jsp:useBean>

        

              <p>Student First Name: 

         <jsp:getProperty name = "students" property = "firstName"/>

      </p>      

      <p>Student Last Name: 

         <jsp:getProperty name = "students" property = "lastName"/>

      </p>      

      <p>Student Age: 

         <jsp:getProperty name = "students" property = "age"/>

      </p>

    </body>

</html>


Java Bean Java Bean Reviewed by Asst. Prof. Sunita Rai on March 27, 2022 Rating: 5

No comments:

Powered by Blogger.