JavaBeans Classes and JSP
JSP markup and the EL language make it relatively easy to
perform simple, presentation-oriented programming tasks within a JSP document.
For more sophisticated computations we may still want to use Java directly – by
calling the Java methods from within a JSP document. JSP provides such a
mechanism through its support for JavaBeans classes.
1.
Create a
class that can be recognized as a JavaBeans class by JSP
2.
Allow JSP
and JavaBeans objects to interact.
JavaBeans Technology Basics
For JSP purposes – A Java class is said to conform with
the JavaBeans specification – if the class –
§ Is public and not abstract.
§ Has one or more public methods that follow the
property design patterns of the JavaBeans specification.
§ has a public no-argument constructor.
§ is contained in a named package (package jb in following example), rather than in the default
package.
If a Java class conforms to the above JavaBeans
Specification, then the methods of this class can be called from a JSP document.
For
example, consider the following class jb.fnCube:
package jb;
public class fnCube
{
public int cube(int n)
{
return n*n*n;
}
}
Compile
the class and place it in the WEB-INF/classes/jb
directory of a web application named WT. Consider a JSP document associated
with this web application with the following markup:
<jsp:useBean
id="obj" class="jb.fnCube"
/>
<head>
<title> JavaBeans and JSP </title>
</head>
<body>
<h2> <c:out value="${obj.cube(5)}" /> </h2>
</body>
The useBean standard action
successfully instantiates an object belonging to the fnCube
class and associates the object with an EL variable name (obj
in this example).
JavaBeans simple property design patterns
There
are two types of JavaBeans simple property design patterns
1.
getter simple property design pattern – requires that
a.
the
method name must begin with get
b.
the method
i. must be public,
ii. must return a value, and
iii. take no arguments
2.
setter simple property design pattern – requires that
a.
the
method name must begin with set
b.
the
method
i. must be public,
ii. must not return a value (that is, it must be
void), and
iii. must take a single argument
If a method of a JavaBeans class conforms to the simple
property design patterns specifications – then the method can be accessed by EL
expressions as if it were a property of an object rather than a method.
Example:
package jb;
public class fnGetSetMtds
{
public String strN="";
public String getstrN() {
return strN;
}
public void setstrN(String s) {
this.strN = s;
}
}
In this example, the getstrN() and setstrN(String s) methods conform with the getter and
setter simple property design pattern. This has the effect of associating a bean
property named strN with each instance of fnGetSetMtds. The getstrN() method will be called automatically when a JSP document
attempts to retrieve the value of the strN bean
property of a fnGetSetMtds object.
Thus, the
markup
<jsp:useBean
id="obj" class="jb.fnGetSetMtds"
scope="application" >
<jsp:setProperty name="obj"
property="strN" value="IT" />
<jsp:getProperty name="obj" property="strN"
/>
</jsp:useBean>
1.
setstrN(String s) is called automatically when a JSP document
attempts to assign a value to the corresponding strN property
of a JavaBeans object.
2.
getstrN() is called automatically to retrieve the value of strN bean property.
Instantiating Bean Objects
The useBean standard JSP action
can be used to instantiate a bean object within a JSP document. If an
appropriate JavaBeans instance exists when useBean is
invoked, then it will generally do nothing. For instance, the markup
<jsp:useBean id="obj" class="jb.fnCube"
scope="session" />
will create the variable obj only if
it does not already exist. If the obj variable
already exists in the specified scope and refers to an instance of my.fnCube, then this action does nothing. Note:
1.
String
literals (not EL expressions) must be used to provide the value of all of the useBean attributes
2.
Also,
the id value specified in a useBean action must be
unique with respect to all other useBean actions
within the same translation unit
.
Using JavaBeans Objects
1.
While on
the surface it might appear that the JavaBeans interface can only be used for
simple data access and storage in Java objects, it can in fact be used for much
more complex applications.
2.
JavaBeans
objects are also often used to provide indirect access to persistent data stored
either on a file system or in a database.
Getters/Setters on Nonbean
Objects
Many Java Servlet API classes have methods that follow
the getter-setter syntax. Example, the HttpServletRequest
class has a getPathInfo() method that takes no arguments and returns a String. Such
methods can be accessed from JSP documents using the bean property syntax,
whether or not the class defining the methods has a no-argument constructor.
So, for example, if the EL expression
${pageContext.request.pathInfo}
appears outside other markup, then it is equivalent to the Java scriptlet
<jsp:scriptlet>
out.write(request.getPathInfo()}:
</jsp:scriptlet>
No comments:
Post a Comment
Don't be a silent reader...
Leave your comments...
Anu