JSP ACTION TAGS
JAVA BEANS
Uses
of JSP Constructs:
• Scripting elements calling servletcode
directly
• Scripting elements calling servletcode
indirectly (by means of utility classes)
• Beans
• Servlet/JSP combo (MVC)
• MVC with JSP expression language
• Custom tags
• MVC with beans, custom tags, and
a framework like Struts or JSF
Creating
dynamic content in JSPs
The commonly used JSP tags for creating
dynamic content within a
JSP are :
jsp:useBean
jsp:getProperty
jsp:setProperty
About
Java beans:
A JavaBean is a Plain Old Java object
(POJO) that isSerializable, has a0-argument i.e.no-argconstructor, and allows
access to its properties usinggetter and setter methods.
Java Beans are Java classes that obey
the following conventions:
–Must have a zero-argument (empty)
constructor
•You can satisfy this requirement either
by explicitly defining such a constructor or by omitting all constructors
–Should have no public instance
variables (fields)
–Persistent values should be accessed
through methods called getXxx
•If class has method getTitle() that
returns a String, class is
said to have a String property named title
•Boolean properties may use isXxxinstead
of getXxx
Note : It is the name of the
method, not instance variable that matters!
Bean
Properties
•Usual rule to turn method name into
property name
–Drop the word “get” or “set” and change
the next letter to
lowercase.
• Method name: getFirstName
• Property name: firstName
• Exception 1: booleanproperties
–If getter returns booleanor
Boolean(wrapper class)
• Property name: prime
• Exception 2: consecutive uppercase
letters
–If two uppercase letters in a row after
“get” or “set”
• Method name: getURL
• Property name: URL (not uRL)
Examples:
Method NamesProperty NameExample JSP Usage
getFirstName firstName <jsp:getProperty…
property="firstName"/>
setFirstName <jsp:setProperty…
property="firstName“ value=“propertyValue”/>
isExecutive executive <jsp:getProperty…
property="executive"/>
setExecutive <jsp:setProperty…
property="executive“ value=“propertyValue”/>
(booleanproperty)
getZIP ZIP <jsp:getProperty…
property="ZIP"/>
setZIP <jsp:setProperty…
property="ZIP” value=“propertyValue”/>
Note: method names are derived from
property names.
Using
Beans : Basic tasks:
jsp:useBean
–In the simplest case, this element
instantiates a java bean, or to locate an existing bean instance, and assign it
to id.
It is used as:
<jsp:useBeanid="beanName"
class="package.Class”/>
jsp:setProperty
–This element modifies a bean property
(i.e., calls a setXXXmethod).
It is used as:
<jsp:setPropertyname="beanName"
property="propertyName"
value="propertyValue"
/>
jsp:getProperty
–This element reads and outputs the
value of a property.
It is used as:
<jsp:getPropertyname="beanName"
property="propertyName"
/>
The <jsp:useBean> action tag:
We can also
specify the life time of the object by giving it a specific scope.
The
<jsp:useBean> action tag ensures that the object is available, with the
specified id,
in the appropriate scope as specified by the tag.
Syntax:
<jsp:useBeanattributes>
Ex.
<jsp:useBeanid="regform"class="RegFormPack.RegForm“scope="session"/>
Once a bean
class is loaded, we can use
jsp:setPropertyandjsp:getPropertyactions
to modify/retrieve bean properties.
Attributes of
<jsp:usebean> tag:
Attributes of
the <jsp:useBean> Tag:
Id:The id attribute of the jsp:useBeanaction tag
represents the variable name assigned to the id attribute of the jsp:useBean.
scope:The scope attribute represents the scope in
which the bean instance has to be located or created. scopes can be page,
request, session or application.
◦page :It means that we can use the Bean within the JSP page.
◦request:We can use the bean from any JSP page processing the
same request.
◦session:We can use the bean from any jsppage in the same
session as the jsppage that created the bean.
◦application:We can use the bean from any page in the same
application as the jsppage that created the bean.
class:It takes the qualified class name (package.classname) to create
a bean instance if the bean instance is not found in the given scope.
Comments
Post a Comment