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


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?

/ 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


<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

.
<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> . . .


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

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)


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


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status