Thursday, 26 May 2016

A Java class that can be easily reused and composed  together in an application. Any java class that follows certain design  composed together in an application. Any java class that folllows certain  design conventions can be a Java Bean.

Java Bean Design Convention: 
These conventions are :

1. A bean class must  have a zero  argument constructor.
2. A bean class should not have any public instance / variable (attribute or field).
3. Private values should be accessed through setter / getter.
      For boolean data types, use boolean isXXX() & setXXX(boolean).
4. A Java bean class must be serializable.

JSP Action Element: 
   JSP Action elements allow us to work with Java Bean to include pages at request time and to forward request to other resources etc.

Format :
   Expressed using XML syntax:
     - Opening tag <jsp:actionElement attribute = "value" .....> 
     -  Body
     -   Closing tag </jsp:actionElement>

Some JSP Action Element: 
 1. To work with JavaBeans
       a. <jsp: useBean />
       b. <jsp:setProperty />
       c. <jsp:getProperty />
2. To include resources at request time.
          <jsp: include />
3. To forward request to another JSP or servlet
            <jsp: forward />
4. To work with applets.
           <jsp: plugin />

Working with JavaBeans using JSP Action Element
The three action elements are used to work with JavaBeans.

1. JSP useBean Action Element: 

         It is used to obtain a reference to an existing Java Beans object by specifying id(name of object) and scope in which bean is stored. If a reference is not found, the bean is instantiated.
The format of this action element is
<jsp:useBean id ="name" scope="page | request | session | application" class="pakage.class" />


jsp:useBean is being equivalent to building an object in scriplet. For example, to build an object of MyBean using scriplet is :
    < % MyBean m = new MyBean(); %>
Achieving above functionality using jsp:useBean action element will look like this :
    <jsp: useBean id ="m" scope ="page" class="javatutorial.MyBean" />

2. JSP setProperty Action Element: 
 
     To set or change the property value of specified bean. The format of this action  is
     <jsp:setProperty name="beanName or id" property="name" value="value" />
 jsp:setProperty is being equivalent to the following code of script. For example to change the name property of m(instance of MyBean) using scriplet is:
        <%  m.setProperty("ali") %>
Achieving above functionality using jsp:setProperty action element will look like this:
  <jsp:setProperty name="m" property ="name" value= "ali" />

3. JSP getProperty ActionElemnet: 
      Use to retrieve the value of property, convert it to string and writes it to output stream. The format of this action element is :
<jsp:getProperty name="beanName or id"  property = "name" />

jsp:getProperty is being equivalent to the following code of scriptlet. For example to retrieve the name property of m(instance of MyBean) followed by writing it to ouput stream, scriplet code will look like
     <%  
                  String name = m.getName(); 
                  out.println(name); 
   %> 

Achieving above functionality using jsp.getProperty action element will look like this:
      <jsp:getProperty name="m" property = "name' />


4 comments: