Tải bản đầy đủ (.ppt) (31 trang)

Tài liệu Using JavaBeans in JavaServer Pages - Chương 3 pptx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (387.83 KB, 31 trang )


Using JavaBeans in
JavaServer Pages
Chương 3
/ 2 of 36
Session Objectives

Describe the various features of a JavaBean

Differentiate between a JavaBean and a Java class

Describe the basic structure of a JavaBean

Describe the various ways of creating beans

Describe the advantages of using beans

Explain how to access beans through JSP scriptlets

Describe the use of various JSP bean tags

Define the scope of beans
/ 3 of 36
JavaBean or a Bean, is a simple Java class that follows
a set of naming and design conventions,
outlined by the JavaBeans specifications
These components can be combined into applets,
applications, or composite components
With the help of the JavaBeans API, you can create
reusable and platform-independent components
What is a JavaBean?


JavaBeans brings component technology
to the Java platform
/ 4 of 36
Difference between a
JavaBean and a Java class

The class must be instantiable

It must have a default constructor

It must be serializable

It must follow the JavaBeans design
patterns
A bean is a Java class with the following additional
characteristics:
/ 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
/ 15 of 36
Session Scope – (1)
<HTML> . . .

<%@ page language = "java" %>
<%@ page import = "Counter" %>

<jsp:useBean id = "id_counter" scope = "session"
class = "Counter" />
<H1> A Session bean: The First Example </H1>
<B>The current count for the counter bean is: </B>
<%= id_counter.getCount() %>
. . . </HTML>
An Example - The first JSP
Instantiate the
Counter bean
/ 16 of 36
Session Scope – (2)
<HTML> . . .
<%@ page language = "java" %>
<%@ page import = "Counter" %>

<jsp:useBean id = "id_counter" scope = "session"
class = "Counter" />
<H1> A Session bean: The Second Example </H1>
<B>The current count for the counter bean is: </B>
<%= id_counter.getCount() %>

. . . </HTML>
An Example - The second JSP
Instantiate the
Counter bean
/ 17 of 36
Session Scope – (3)


When you browse the first JSP for the first time, the
“current count of the counter bean” will be equal to
1.

When you browse the second JSP with the same
instance of the browser, the counter increases from
the last value shown in the first JSP.

If a new instance of the browser is opened, the
current count attribute will be reset. Each instance
of a client creates its own instance of the
HttpSession, which is where the Counter bean is
stored.
/ 18 of 36
Application Scope

Beans with the application scope are accessible
within pages that are in the same application

References to the object will be released only when
the runtime environment reclaims the
ServletContext object.

Beans with the application scope are best used
when there is a need for sharing the information
among JSPs and servlets for the life of an
application.

All clients access the same object

/ 19 of 36
Application Scope – (1)
An Example - The first JSP
<HTML>. . .
<%@ page language = "java" %>
<%@ page import = "Counter" %>

<jsp:useBean id = "id_counter" scope = "application"
class = "Counter" />

<H1> An Application bean: The First Example </H1>
<B>The current count for the counter bean is: </B>
<%= id_counter.getCount() %>
. . . </HTML>
Instantiate the
Counter bean
/ 20 of 36
An Example - The second JSP
Application Scope – (2)
<HTML> . . .
<%@ page language = "java" %>
<%@ page import = "Counter" %>
<jsp:useBean id = "id_counter" scope = "application"
class = "Counter" />

<H1> An Application bean: The Second Example </H1>
<B>The current count for the counter bean is: </B>
<%= id_counter.getCount() %>
. . . </HTML>
Instantiate the

Counter bean
/ 21 of 36

Both these pages share the same instance of
the Counter bean

Each page increments the other page’s
instance of the bean

These pages will share this same instance,
until the JSP engine is shut down
Application Scope – (3)
/ 22 of 36
jsp:setProperty

This is used along with the useBean action

It sets the values of bean properties in
various ways:

At request time, using the parameters in the
request object

At request time, using the result of an evaluated
expression

From a specified string
/ 23 of 36
jsp:setProperty – (1)


The bean introspection method is used to discover
what properties are present, their names, whether
these are simple or indexed, their types and the
accessor methods

The syntax is as follows:
<jsp:setProperty name = "id" property = "*" />
<jsp:setProperty name = "id" property = "propertyName"
param = "parameterName"/>
<jsp:setProperty name = "id" property = "propertyName"
value = "propertyValue"/>
/ 24 of 36
jsp:getProperty

This action is complementary to the jsp:setProperty
action

It is used to access the properties of a bean

It converts the retrieved value to a String

The syntax is as follows:
<jsp:getProperty name = "id"
property = "propertyName" />
/ 25 of 36
An Example
The Bean – Counter.java
public class Counter {
int count;
public Counter() {

count = 0; }
public int getCount() {
count++;
return count; }
public void setCount(int c) {
count = c; }
}
Property
get Method
set Method

×