Friday, April 6, 2012

comment_icon 0 working with Java Beans

In this tutorial i am going to talk about the java beans.
i am going to cover the following  things


  1. what is  Java Bean ?
  2. how we use java beans in JSP
  3. scope of java Beans
what is  Java Bean ?
   If i say in general wordings java beans is the component that can be reused again and again .it just follow write once and use whenever required.

for more details and basics follow the link java Beans

how we use java beans in JSP

JSP provides a usebean tag for using beans in java

 Syntax :
   <jsp:useBean 
id="beanInstanceName" 
  scope="page|request|session|application"
 { class="package.class" | 
  type="package.class" | 
  class="package.class" type="package.class" |
  beanName="{ package.class | <%= expression %> }" type="package.class"  
 }
/>
Example:

Examples

<jsp:useBean id="db" scope="session" class="database.DataConnect" />
How it works (from  http://java.sun.com ) ? 
The <jsp:useBean> tag attempts to locates a Bean, or if the Bean does not exist, instantiates it from a class or serialized template. To locate or instantiate the Bean, <jsp:useBean> takes the following steps, in this order:
  1. Attempts to locate a Bean with the scope and name you specify.
  2. Defines an object reference variable with the name you specify.
  3. If it finds the Bean, stores a reference to it in the variable. If you specified type, gives the Bean that type.
  4. If it does not find the Bean, instantiates it from the class you specify, storing a reference to it in the new variable. If the class name represents a serialized template, the Bean is instantiated byjava.beans.Beans.instantiate.
  5. If it has instantiated (rather than located) the Bean, and if it has body tags (between <jsp:useBean> and </jsp:useBean>), executes the body tags.
Attributes and their Usage 
  • id="beanInstanceName"Names a variable that identifies the Bean in the scope you specify. You can use the variable name in expressions or scriptlets in the same JSP file. The name is case sensitive and must conform to the naming conventions of the page scripting language.
  • scope="page|request|session|application"Defines a scope in which the Bean exists and the variable named in id is available. The default value is page. latter we see explanation about other scopes
  • class="package.class"Instantiates a Bean from a class, using the new keyword and the class constructor. The class must not be abstract and must have a public, no-argument constructor. The package and class name are case sensitive.
  • class="package.class" type="package.class"Instantiates a Bean from a class, using the new keyword and the class constructor, and gives the Bean the type you specify in type. The class you specify in class must not be abstract and must have a public, no-argument constructor. The package and class names you use with both class and type are case sensitive. The value of type can be the same as class, a superclass of class, or an interface implemented by class.
  • type="package.class"If the Bean already exists in the specified scope, gives the Bean the type you specify. If you use type without class or beanName, no Bean is instantiated. The package and class name are case sensitive
 Bean Scopes page Scope:in this scope bean is available for that particular page, as soon as page finishes the the processing the bean will disappear. request scope :if a bean is set to request scope it will be available until that request get processed , if the request results in the page redirection the bean will be also available on the redirected page. session Scope: when a bean is requested as session scope the bean instance will be available for the whole session Application scope: here the bean will be available for the entire application it will exist until application is running on server

Working demo of java Bean : using Bean For db2 connectivity

Download files used in demo  Demo.zip