Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · ASP.NET Newsletter Application · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Newsletter
Enter your email address to receive full length articles at Stardeveloper:


Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (41)
  ADO (16)
  ADO.NET (11)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)
Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article
Stardeveloper RSS Feed
Hosted by Securewebs.com
 
Home : J2EE : JSP : What are JavaBeans ?
 
RSS - Read full length articles at Stardeveloper using Stardeveloper RSS Feed RSS

What are JavaBeans ?

by Faisal Khan. Follow Faisal Khan on Twitter Follow Faisal Khan on Facebook

Overview
JavaBeans are usual Java classes which adhere to certain coding conventions. Following are the coding conventions I am talking about :

  • Implements java.io.Serializable interface
  • Provides no argument constructor
  • Provides getter and setter methods for accessing it's properties

Let's now create a simple JavaBean class.

A simple JavaBean class
Create a new SimpleBean.java file and place it in the /WEB-INF/classes/com/stardeveloper/bean/test/ folder so that the complete path is :

/WEB-INF/classes/com/stardeveloper/bean/test/SimpleBean.java

Now copy the following code and paste it into the SimpleBean.java file we created above :

package com.stardeveloper.bean.test;

public class SimpleBean implements java.io.Serializable {

	/* Properties */
	private String name = null;
	private int age = 0;

	/* Empty Constructor */
	public SimpleBean() {}

	/* Getter and Setter Methods */
	public String getName() {
		return name;
	}

	public void setName(String s) {
		name = s;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int i) {
		age = i;
	}
}

Explanation
First line is the package statement :

package com.stardeveloper.bean.test;

Next we define our class and make it implement java.io.Serializable interface. Notice that Serializable interface doesn't contain any method. Implementing it just flags to the compiler that we might be serializing this class's objects.

public class SimpleBean implements java.io.Serializable {

Then we declare two variables which hold name and age of a person. These variables inside a JavaBean are called as properties. These properties are private and are thus not directly accessible by other classes. To make them accessible we provide getter and setter methods to get and set their values.

private String name = null;
private int age = 0;

Next we create an empty argument constructor. Keep in mind that the only requirement to a JavaBean is an empty "argument" constructor, not that you shouldn't use constructor at all.

public SimpleBean() {}

Explanation
The convention for writing getter and setter methods for JavaBean's properties is really simple. All you have to do is to take the property name e.g. name. Make it's first character uppercase e.g. Name. Now append 'get' for getter method and 'set' for setter method so that it becomes :

public String getName() {
	return name;
}

public void setName(String s) {
	name = s;
}

See! how easy it is. Since name variable is of type String, we set the return type of getName() to String. Same is the case with setName() method which takes a parameter of type String because name is of type String.

Next we added four getter and setter methods for private variables ( properties ) name and age.

public String getName() {
	return name;
}

public void setName(String s) {
	name = s;
}

public int getAge() {
	return age;
}

public void setAge(int i) {
	age = i;
}

Now close the class.

}

Compiling JavaBean
You will compile JavaBean like you will compile any other Java class file. After compilation, a SimpleBean.class file will be created.

We are now done with SimpleBean.

Summary
We learned that JavaBeans are Java classes which adhere to an extremely simple coding convention. All you have to do is to implement java.io.Serializable interface, use a public empty argument constructor and provide public getter and setter methods to get and set the values of private variables ( properties ).

Now move over to the next article about calling this JavaBean from within a JSP page. That article will also explain the use of JSP <jsp:useBean>, <jsp:setProperty> and <jsp:getProperty> tags which are provided to you by the JSP specification to make use of JavaBeans.


 ( No Further Pages )

Related Articles
  1. Calling JavaBeans from a JSP Page
  2. Creating a Browser Detection JavaBean

Comments/Questions ( Threads: 6, Comments: 8 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. java beans exicution process
  2. Unable to keep Servlets and JSPS anywhere else other than ROOT of Tomacat Directory ( 1 Reply )
  3. Regarding the directory structure
  4. I use Jbean with JSP for the first time : the example above is not clear ( 1 Reply )
  5. Good One
  6. i have an error : db connect within a javabean

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed.

 
© 1999 - 2010 Stardeveloper.com, All Rights Reserved.