A walking tour of JavaBean





Software components are to software what integrated circuits (ICs) are to electronics: "black boxes" that encapsulate functionality and provide services based on a specification. Software reusability has long been a holy grail of software engineering and a major goal of object-oriented programming. Software components are designed to be reusable, even interchangeable.

JavaBeans are reusable software components for Java that can be manipulated visually in a builder tool.Practically, they are classes written in the Java programming language conforming to a particular convention. They are used to encapsulate many objects into a single object (the bean), so that the bean can be passed around rather than the individual objects.

Java Bean

A Java Bean is a java class that should follow following conventions:
  • It should have a no-arg constructor.
  • It should be Serializable.
  • It should provide methods to set and get the values of the properties, known as getter and setter methods.

Serialization

In computer science, in the context of data storage, serialization is the process of translating data structures or object state into a format that can be stored (for example, in a file or memory buffer) or transmitted (for example, across a network connection link) and reconstructed later (possibly in a different computer environment)

Serializable Objects

To serialize an object means to convert its state to a byte stream so that the byte stream can be reverted back into a copy of the object. A Java object is serializable if its class or any of its superclasses implements either the java.io.Serializable interface or its subinterface, java.io.Externalizable. Deserialization is the process of converting the serialized form of an object back into a copy of the object.

Why use Java Bean?

According to Java white paper, it is a reusable software component. A bean encapsulates many objects into one object, so we can access this object from multiple places. Moreover, it provides the easy maintenance.

JavaBeans Properties

A JavaBean property is a named attribute that can be accessed by the user of the object. The attribute can be of any Java data type, including the classes that you define.

A JavaBean property may be read, write, read only, or write only. JavaBean properties are accessed through two methods in the JavaBean's implementation class −

S.No.Method & Description
1
getPropertyName()
For example, if property name is firstName, your method name would be getFirstName() to read that property. This method is called accessor.
2
setPropertyName()
For example, if property name is firstName, your method name would be setFirstName() to write that property. This method is called mutator.
A read-only attribute will have only a getPropertyName() method, and a write-only attribute will have only a setPropertyName() method.

JavaBeans Example

Consider a student class with few properties −

package com.SuvenConsultants;

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 firstName){
      this.firstName = firstName;
   }
   public void setLastName(String lastName){
      this.lastName = lastName;
   }
   public void setAge(Integer age){
      this.age = age;
   }
}
How to Access Java Bean Class

package com.SuvenConsultants;
public class Suven{  
public static void main(String args[]){  
  
StudentsBean sc=new StudentsBean ();//object is created  
  
sc.seFirsttName("Rocky");//setting value to the object  
sc.seLasttName("Jagtiani");  
System.out.println(e.getFirstName());  
  
}}  
The useBean action declares a JavaBean for use in a JSP. Once declared, the bean becomes a scripting variable that can be accessed by both scripting elements and other custom tags used in the JSP. The full syntax for the useBean tag is as follows:
<jsp:useBean id = "bean's name" scope = "bean's scope" typeSpec/>

Here values for the scope attribute can be a page, request, session or application based on your requirement. The value of the id attribute may be any value as a long as it is a unique name among other useBean declarations in the same JSP.

Following example shows how to use the useBean action −
<html>
   <head>
      <title>useBean Example</title>
   </head>
   
   <body>
      <jsp:useBean id = "date" class = "java.util.Date" /> 
      <p>The date/time is <%= date %>
   </body>
</html>
You will receive the following result − −
The date/time is Thu Sep 30 11:18:11 GST 2017 

Accessing JavaBeans Properties

Along with action, you can use the action to access the get methods and the action to access the set methods. Here is the full syntax −

<jsp:useBean id = "id" class = "bean's class" scope = "bean's scope">
   <jsp:setProperty name = "bean's id" property = "property name"  
      value = "value"/>
   <jsp:getProperty name = "bean's id" property = "property name"/>
   ...........
</jsp:useBean>
The name attribute references the id of a JavaBean previously introduced to the JSP by the useBean action. The property attribute is the name of the get or the set methods that should be invoked.
Following example shows how to access the data using the above syntax −

<html>
   <head>
      <title>get and set properties Example</title>
   </head>
   
   <body>
      <jsp:useBean id = "students" class = "com.SuvenConsultants.StudentsBean"> 
         <jsp:setProperty name = "students" property = "firstName" value = "Rocky"/>
         <jsp:setProperty name = "students" property = "lastName" value = "Jagtiani"/>
         <jsp:setProperty name = "students" property = "age" value = "39"/>
      </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>
The following result will be displayed −

Student First Name: Rocky

Student Last Name: Jagtiani

Student Age: 39

Benefits of JavaBeans:

  • A Bean obtains all the benefits of Java's "write-once, run-anywhere" paradigm. 
  •  The properties, events, and methods of a Bean that are exposed to an application builder tool can be controlled. 
  • A Bean may be designed to operate correctly in different locales, which makes it useful in global markets.
  •  Auxiliary software can be provided to help a person configure a Bean. This software is only needed when the design-time parameters for that component are being set. It does not need to be included in the run-time environment.
  •  The configuration settings of a Bean can be saved in persistent storage and restored at a later time. 
  • A Bean may register to receive events from other objects and can generate events that are sent to other objects.


Want To Learn Java?

Comments

  1. Your contents are completely awesome and share worthy. I really appreciate your efforts that you put on this. Keep sharing.
    JAVA Training institute in delhi

    ReplyDelete
  2. This is my first time I visit here. I found so many entertaining stuff in your blog, especially its discussion. From the tons of comments on your articles

    Learn AutoCAD Training Course with Placement

    ReplyDelete
  3. Great Work. I always use to follow your valuable contents. Thanks for sharing this kind of debt knowledge.

    Best Institute for JAVA Training Course in Delhi, India

    ReplyDelete
  4. I really enjoyed while reading your article, the information you have mentioned in this post is really good. I am waiting for your upcoming post

    Best Institute for MS Office Training Course in Delhi, India

    ReplyDelete
  5. Gone through your blog it is very knowledgeable and have very interesting fact. Dreamsoft is the 20 years old consultancy providing fake experience certificate in Noida To get fake experience certificate in Noida you can call at 9599119376 or can the visit https://experiencecertificates.com/experience-certificate-provider-in-Noida.html

    ReplyDelete

  6. Good to hear you views about I totally agree to your views. For filling the gap in the courier Dreamsoft will provide the Genuine experience certificate in Noida, so here is your turn to grab the opportunity and to take your experience certificate in Noida. The one who is interested for the experience certificate in Noida may contact at 9599119376 or can visit our website at https://experiencecertificates.com/experience-certificate-provider-in-Noida.html

    ReplyDelete

Post a Comment

Popular posts from this blog

PPC - SomeOne SpenDing - SomeOne EarNing

Web scrapping tools,Sooo Muuch Data - Analysis Needed !

Must kNow Web Dev toolS - 2OI7