/ 5 of 36
JavaBeans Design Patterns
Naming conventions for bean methods and
data members
Design patterns are important for
introspection
Introspection: mechanism using components
makes its internal structure known to the outside
world?
Methods are exposed through reflection
/ 6 of 36
An Example of a JavaBean
public class Tower {
private float height;
public float getHeight() {
return height; }
public void setHeight(float h) {
height = h; }
public boolean isGreaterHeight(int initialHeight,
int finalHeight) {
if((finalHeight - initialHeight) > 0) {
return true; }
else {
return false;}
}
public Tower() {
height = (float)10.5; }
}
Property
get
PropertyName()
set
PropertyName()
/ 7 of 36
JSP provides three basic bean tags:
To find and use the bean – jsp:useBean
To set one or more properties – jsp:setProperty
To get a property – jsp:getProperty
These are also known as actions and are
specific tags that affect the following:
the runtime behavior of the JSP
the responses that are sent back to the client
Using JSP Bean Tags
/ 8 of 36
Using JSP Bean Tags – (1)
The Black box approach
getProperty_Name()
setProperty_Name(
value
)
Bean
The JSP need not know the
inner structure of the bean
/ 9 of 36
jsp:useBean
This is used to associate a JavaBean in JSP.
It ensures that the object can be referenced
from JSP, using an ID and scope
The syntax is as follows:
<jsp:useBean id = "bean_name"
class = "bean_class“ />
The bean class
must be available
to the JSP engine
/ 10 of 36
jsp:useBean – (1)
<jsp:useBean id = "id_name"
class = "bean_class"
scope = "page|request|session|application"
beanDetails / >
class = "className"
class = "className" type = "typeName"
beanName = "beanName" type = "typeName"
type = "typeName"
beanDetails is one of:
/ 11 of 36
Scope of Beans
page
The bean will disappear as soon as the current page
finishes generating
References to this object will only be released after the
response is sent back to the client
request
The bean instance will be available as long as the
request object is available
References to this object will be released only after the
request is processed completely
/ 12 of 36
session
The instance will be available across a particular session
between the browser of a particular machine and the
Web server
References to this object will be released only after the
associated sessions end
application
The instance will be available to all users and pages of
the application
It is released only when the run-time environment
reclaims the ServletContext
Scope of Beans – (1)
/ 13 of 36
An Example
.
.
<jsp:useBean id = "mparam" scope = "session"
class = "java.lang.String">
This is used to instantiate the bean
</jsp:useBean>
<HTML>
<BODY>
<H1> Hello World! </H1>
</BODY>
</HTML>
The scope is set
to the session
/ 14 of 36
Session Scope
Beans with session scope are accessible only within
pages processing requests that are in the same
session as the one in which the bean was created
Beans cannot be defined in a page whose page
directive has an attribute session=false
References to the session scope are stored in the
session object
The bean is distinct for every client and is valuable
as long as the client’s session is valid
Không có nhận xét nào:
Đăng nhận xét