Using Beans in JSP: BY Sana Mateen
Using Beans in JSP: BY Sana Mateen
BY
SANA MATEEN
JAVA BEANS
Java Beans are reusable java components that allow us to
separate business logic from presentation.
A JavaBean is nothing more than a class that maintains
some state data (called properties) and follows a certain set
of coding conventions.
Java Bean class meets the following requirement:
Must implement java.io.Serializable or
java.io.Externalizable
provide a no-arguments constructor
private properties must have corresponding get/set
methods
Three action elements are used to work with beans they
are:
useBean
setProperty
getProperty
useBean
Example:
<jsp:useBean id=fact scope=page class=bean.Factorial/>
Is equivalent to
<%bean.Factorial fact=new bean.Factorial()%>
setProperty
<jsp:setProperty> assigns a new value to the specified property of the
specified bean object.
<jsp:setProperty name=obj_name property=prop_name
value=prop_value/>
Is equivalent to
<%obj_name.setProp_name(prop_value)%>
To set a property of our bean object fact
<jsp:setProperty name=fact property=value value=5/>
Is equivalent to
<%fact.setValue(5);%>
getProperty
<jsp:getProperty> action element retrieves the value of specified
property of the specified bean object.
<jsp:getProperty name=obj_name property=prop_name/>
Is equivalent to
<%=obj_name.getProp_name();%>
<jsp:getProperty name=fact property=value/>
Is equivalent to
<%=fact.getValue();%>