1. Trang chủ
  2. » Công Nghệ Thông Tin

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

31 702 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Using JavaBeans in JavaServer Pages - Chương 3
Trường học University of Science and Technology of Hanoi
Chuyên ngành Computer Science
Thể loại lecture notes
Thành phố Hanoi
Định dạng
Số trang 31
Dung lượng 83,5 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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 wa

Trang 1

Using JavaBeans in JavaServer Pages

Chương 3

Trang 2

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

Trang 3

JavaBean or a Bean, is a simple Java class that follows

a set of naming and design conventions,

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

Trang 4

Difference between a JavaBean and a Java class

 The class must be instantiable

 It must have a default constructor

Trang 5

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

Trang 6

An Example of a JavaBean

public class Tower {

private float height;

public float getHeight() {

height = ( float )10.5; }

Property

getPropertyName()

setPropertyName()

Trang 7

 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

Trang 8

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

Trang 9

 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:

Trang 10

class = "className" type = "typeName"

beanName = "beanName" type = "typeName"

type = "typeName"

beanDetails is one of:

Trang 11

 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

Trang 12

 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)

Trang 14

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

Trang 15

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

Trang 16

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

Trang 17

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

Trang 18

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

Trang 19

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() %>

Instantiate the Counter bean

Trang 20

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

Trang 21

 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)

Trang 22

 This is used along with the useBean action

 It sets the values of bean properties in

Trang 23

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"/>

Trang 24

 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" />

Trang 25

Property get Method set Method

Trang 26

An Example – (1)

The JSP that uses the bean

<HTML>

<%@ page language = "java" %>

<%@ page import = "Counter" %>

<jsp:useBean id = "id_counter" scope = "session"

Trang 27

Setting Bean Properties

 Properties can be set through an HTML form

 The users can provide values through the form

 A JSP can be used to pass these values to a bean

through the setProperty tags

 Form values can be passed like this:

<jsp:setproperty name = "id"

property = "propertyName"

value =

"<%= request.getParameter("formParam") %>"

Trang 28

Setting Bean Properties – (1)

<jsp:setProperty name = "id"

property = "formParam" />

<jsp:setProperty name = "id"

property = "propertyName" param = "paramName" />

The form variable and the bean

parameter have the same names

The form variable and the bean

parameter have different names

Trang 29

An Example

public class CalcBean implements

java.io.Serializable {

private int value1;

private int value2;

public void setValue1(int num1) {

value1 = num1; }

public void setValue2(int num2) {

value2 = num2; }

public int getSum() {

The Bean – CalcBean.java

Trang 30

An Example – (1)

<HTML>

Enter any two numbers and click on the

<B>Calculate</B> Button.

<FORM ACTION = "CalcBean.jsp" METHOD = "GET">

<INPUT TYPE = "TEXT" NAME = "value1"> <BR>

<INPUT TYPE = "TEXT" NAME = "value2"> <BR> <BR>

<INPUT TYPE = "SUBMIT" NAME = "Calculate"

VALUE = "Calculate">

</HTML>

The Form – Calculator.html

Trang 31

<B> The sum of the two numbers is </B>

<jsp:getProperty name = "calc"

property = "sum"/>

</HTML>

The JSP – CalcBean.jsp

Ngày đăng: 25/01/2014, 11:20

TỪ KHÓA LIÊN QUAN