2. Agenda
• Understanding the benefits of beans
• Creating beans
• Installing beans classes on server
• Accessing bean properties
3. What is JavaBean
• JavaBean is reusable software components
• Rules for creating Java Bean
- Public class
- Private/protected instance variable
- Zero-arg constructor
- Public getter and setter methods(accessor and mutator )
• Any java class which satisfies the above rule is a Java Bean
4. Why use a JavaBeans Component
• A JSP page can create and use any type of Java programming language object within a declaration or
scriptlet. The following scriptlet creates the bookstore shopping cart and stores it as a session attribute:
- <% ShoppingCart cart = (ShoppingCart)session. getAttribute("cart"); // If the user has no
cart, create a new one if (cart == null) { cart = new ShoppingCart();
session.setAttribute("cart", cart); } %>
• If the shopping cart object conforms to JavaBeans conventions, JSP pages can use JSP elements to
create and access the object. For example, the Duke's Bookstore pages bookdetails.jsp, catalog.jsp, and
showcart.jsp replace the scriptlet with the much more concise JSP useBean element:
<jsp:useBean id="beanName" class="fully_qualified_classname" scope="scope"/>
Or
<jsp:useBean id="beanName" class="fully_qualified_classname“
scope="scope"> <jsp:setProperty /> </jsp:useBean>
6. How to access javabean in JSP
• <jsp:useBean> action
- Enables JSP to manipulate Java object
• Creates Java object or locates an existing object for use in JSP
• Location can be
- Page Scope
- Request Scope
- Session Scope
- Application Scope
7. <jsp:useBean> attributes
Attribute Description
id The name used to manipulate the Java object with actions
<jsp:setProperty> and <jsp:getProperty>. A variable of
this name is also declared for use in JSP scripting elements. The name
specified here is case sensitive.
scope The scope in which the Java object is accessible—page, request,
session or application. The default scope is page.
class The fully qualified class name of the Java object.
beanName The name of a bean that can be used with method instantiate of
class java.beans.Beans to load a JavaBean into memory.
type The type of the JavaBean. This can be the same type as the class
attribute, a superclass of that type or an interface implemented by that
type. The default value is the same as for attribute class. A
ClassCastException occurs if the Java object is not of the type
specified with attribute type.
8. <jsp:useBean> attributes( cont)
Attribute Description
name The ID of the JavaBean for which a property (or properties) will be
set.
property The name of the property to set. Specifying "*"for this attribute
causes the JSP to match the request parameters to the properties of the
bean.For each request parameter that matches (i.e., the name of the
request parameter is identical to the bean’s property name), the
corresponding property in the bean is set to the value of the parameter.
If the value of the request parameter is"", the property value in the
bean remains unchanged.
param If request parameter names do not match bean property names, this
attribute can be used to specify which request parameter should be
used to obtain the value for a specific bean property. This attribute is
optional. If this attribute is omitted, the request parameter names must
match bean property names.
value The value to assign to a bean property. The value typically is the result
of a JSP expression. This attribute is particularly useful for setting
bean properties that cannot be set using request parameters. This
attribute is optional. If this attribute is omitted, the JavaBean property
must be of a type that can be set using request parameters.
Fig. 25.16 Attributes of the <jsp:setProperty> action.
9. JavaBean Example
package mypack;
public class Student {
private String name;
private int rollno;
public Student(){}
public Student(String name, int rollno) {
this.name = name;
this.rollno = rollno; }
public String getName() { return name; }
public void setName(String name) {this.name = name; }
public int getRollno() { return rollno; }
public void setRollno(int rollno) { this.rollno = rollno; }
}
10. Setting the properties
<jsp:useBean id=“student" class=“mypack.Student" scope="session">
<jsp:setProperty name=“student" property=“name“ value=“king” />
<jsp:setProperty name=“student” property=“rollno” value=“21” />
</jsp:useBean>
1. beanName must be the same as that specified for the id
attribute in a useBean element.
2. There must be a setPropName method in the JavaBeans
component.
3. paramName must be a request parameter name.
11. Setting the properties
Value Source Element Syntax
String constant
<jsp:setProperty name="beanName"
property="propName" value="string constant"/>
Request parameter
<jsp:setProperty name="beanName"
property="propName" param="paramName"/>
Request parameter name matches
bean property
<jsp:setProperty name="beanName"
property="propName"/>
<jsp:setProperty name="beanName"
property="*"/>
Expression
<jsp:setProperty name="beanName"
property="propName"
value="<%= expression %>"/>
13. Advantages of JavaBean
• “Write once run anywhere”
• Reusable Components
• Seperation of Business logic from Presentation and Navigation
• Loosely Coupled
• EJB can also be used for business logic
• No need to implement any API for creating a JavaBean
• Using reflection can access properties, method easily dynamically
14. Summary
• JavaBean is a specification
• Simple convention for creating JavaBean
• Best Practises
• Standard action to create and access the JavaBean
• Standard action to read their content
• EL can also be used to access the properties