Using star to call all setter methods of Java Bean

Introduction 

 
In this article, we will learn how to use asterisk (*) to call all setter methods of a java bean. Before going forward we should know what a java bean is?
  

Java bean

 
It is a development component for performing a task in an application. Any ordinary java class with a set of variables and methods can be called a java bean. The methods present in a java bean must follow a naming convention. A java bean can be used in a web application to store the state of the users.
  

Content of java bean

  
A java bean contains a set of properties and methods.
  
Property: - The variables present in the java bean are called properties. A java bean can contain five types of properties they are as below
  1. Simple property
    If a variable does not belong to an array or Boolean data type then it is called a single property. 
     
  2. Index property
    If the value of a property can be accessed or modified by using the index of an array then it is called an index property.  

  3. Boolean property
    If a property can store only true or false state then it is called an index property. 

  4. Bound property
    If the change in the value of a property must be reflected in other property value then it is called abound property.  
    Whenever the value of this property gets changed then an event occurs (Java.bean.propertychangedEvent) listener of the event implements condition for changing other properties value. 

  5. Constraint property
    If a condition is required to be met before changing the value of a property then it is called as constraint property. 

Methods

  
A java bean can contain two types of methods.
  1. Property modifier (setter method) 
  2. Property accessor (getter method)
Property modifier (setter method)
This method can be used to change the value of a property. Name of this method must contain the name of the property and set as its prefix. This method does not return any value. The datatype of its parameter must match with the data type of property.
  
Property accessor (getter method)
This method can be used to provide the value of a property to the user of a JavaBean. Name of this method must contain the name of the property and get as its prefix. The return type of this method must match with data type of the property. This method does not accept any parameters.
  

Advantages

  1. Reusability
    A java bean can be used by any number of applications. This can be used to perform a common task to required for a set of jsp.
     
  2. Code Abstraction
    If a JSP uses a java bean then it can avoid the use of scriptlets. A user of a java bean need not to know the implementation logic required for the JavaBean. A user uses the java bean by calling its methods.

Using * to call all setter methods of java bean

  
It is a process to call all the setter methods of the bean simultaneously. The property attribute of <jsp:setProperty> contains '*' as its value. This tag calls only those setter methods of the property whose names are available in the requested form field. Here <jsp:setProperty> tag cannot use value attribute.
  
Example
  
Create a folder named pack1 inside the classes folder of the context. Store the javabean file (mybean.java).
  
mybean.java file
  1. package pack1;  
  2.   
  3. public class mybean  
  4. {  
  5.  private String name, pass;  
  6.  public void setName(String n)  
  7.  {  
  8.   name = n;  
  9.  }  
  10.  public void setPass(String p)  
  11.  {  
  12.   pass = p;  
  13.  }  
  14.  public String getName()  
  15.  {  
  16.   return name;  
  17.  }  
  18.  public String getPass()  
  19.  {  
  20.   return pass;  
  21.  }  
  22. }     
Compile of bean:- javac mybean.java
  
Bean1.gif
  
beantest.jsp
  1. <html>   
  2. <body bgcolor="Orange">   
  3. <p> <h3><center>Please enter your user name and\   
  4. password</center></h3></p>  
  5.    
  6. <form>   
  7. <center>Username</center>   
  8. <center><input type ='text' name ="name"></center>   
  9. <center>Password</center>   
  10. <center><input type ='password' name ="pass"></center>   
  11. <center><input type ='submit' value="submit" ></center>   
  12. </form>  
  13.     
  14. <jsp:useBean class="pack1.mybean" id="mb1" />   
  15. <jsp:setProperty name="mb1" property="*"/>   
  16. <% if( request.getParameter("name") !=null)   
  17. {   
  18. %>   
  19. <h2>Name is: <jsp:getProperty name="mb1" property="name" /></h2>   
  20. <h2>Password is: <jsp:getProperty name="mb1" property="pass" /></h2>   
  21. <%  
  22. }   
  23. %>   
  24. </body>   
  25. </html>   
Note: - The variables names (name, pass) we have declared in the creation of the java, bean should be similar to the corresponding property values (name, pass).
  

Running the application

  
Run the tomcat then write the below link in the URL
  
http://localhost:8081/bean/beantest.jsp
  
Here bean is the Context path, which we mentioned in the server.xml file, which is present in (E:\Program Files\Apache Software Foundation\Tomcat 6.0\conf) directory. 
 
Bean2.gif
  
Thanks for reading