.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> General >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Using star to call all setter methods of java bean

Posted By:satyapriyanayak       Posted Date: December 01, 2014    Points: 200    Category: General    URL: http://www.dotnetspark.com  

In this article we will know 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 as 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 as properties. A java bean can contains five types of properties they are as below

1.Simple property: - If a variable is not belongs to an array or Boolean data type then it is called as single property.

2.Index property: - If value of a property can be accessed or modified by using index of an array then it is called as index property.

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

4.Bound property: - If change in value of a property must be reflected into other property value then it is called as bound property.


Whenever 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 required to get satisfied before changing value of a property then it is called as constraint property. Whenever value of this property required to get changed then an event occurs.


Methods

A java bean can contain two types of method.

1.Property modifier (setter method)

2.Property accesser (getter method)


Property modifier (setter method): - This method can be used to change value of a property. Name of this method must contain name of the property and set as its prefix. This method does not return any value.datatype of its parameter must match with data type of property.
Property accesser (getter method): - This method can be used to provide value of a property to the user of a javabean. Name of this method must contain name of the property and get as its prefix. Return type of this method must match with data type of the property. This method does not accept any parameter.


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. User of a java bean need not to know implementation logic required for the javabean. 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 as pack1 inside the classes folder of the context.Store the javabean file (mybean.java).

mybean.java file

package pack1;
public class mybean
{
  private String name,pass;
  public void setName(String n)
   {
      name=n;
    }
     public void setPass(String p)
   {
      pass=p;
    }
  public String getName()
    {
     return name;
      }
    public String getPass()
    {
     return pass;
      }
  }

Compile of bean:- javac mybean.java


beantest.jsp

<html>
<body bgcolor="Orange">
<p> <h3><center>Please enter your user name and
password</center></h3></p>
<form>
<center>Username</center>
<center><input type ='text' name ="name"></center>
<center>Password</center>
<center><input type ='password' name ="pass"></center>
<center><input type ='submit' value="submit" ></center>
</form>

<jsp:useBean class="pack1.mybean" id="mb1" />
<jsp:setProperty name="mb1" property="*"/>
<% if( request.getParameter("name") !=null)
{
%>
<h2>Name is: <jsp:getProperty name="mb1" property="name" /></h2>
<h2>Password is: <jsp:getProperty name="mb1" property="pass" /></h2>
<%
}
%>
</body>
</html>

Note: - The variables names (name, pass) we have declared in the creation of the java bean should be similar with 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.


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend