JAVABEANS IN JSPsite.iugaza.edu.ps/hmasry/files/Lab9-JavaBeans.pdf · Beans are regular Java...

17
In the name of Allah Islamic University of Gaza Faculty of Engineering Computer Engineering Department ECOM 5049 Advanced Internet Technology Lab Lab # 9 JAVABEANS IN JSP Eng. Haneen El-masry 2013

Transcript of JAVABEANS IN JSPsite.iugaza.edu.ps/hmasry/files/Lab9-JavaBeans.pdf · Beans are regular Java...

Page 1: JAVABEANS IN JSPsite.iugaza.edu.ps/hmasry/files/Lab9-JavaBeans.pdf · Beans are regular Java classes that follow some simple conventions defined by the JavaBeans specification: A

In the name of Allah

Islamic University of Gaza

Faculty of Engineering

Computer Engineering Department

ECOM 5049

Advanced Internet Technology Lab

Lab # 9

JAVABEANS IN JSP

Eng. Haneen El-masry

2013

Page 2: JAVABEANS IN JSPsite.iugaza.edu.ps/hmasry/files/Lab9-JavaBeans.pdf · Beans are regular Java classes that follow some simple conventions defined by the JavaBeans specification: A

Advanced Internet Technology Lab

Eng. Haneen 2

Objectives

Understanding JavaBeans.

Understanding the benefits of beans.

Creating beans.

Accessing bean properties.

Explicitly setting bean properties.

Automatically setting bean properties from request parameters.

Sharing beans among multiple servlets and JSP pages.

What are JavaBeans?

Beans are regular Java classes that follow some simple conventions defined by the JavaBeans

specification:

A bean class must have a zero-argument (default) constructor.

A bean class should have no public instance variables (fields).

Persistent values should be accessed through methods called getXxx and setXxx.

Why You Should Use Accessors, Not Public Fields ?

You can put constraints on values.

You can change your internal representation without changing the interface.

You can perform arbitrary side effects.

Using Beans in JSP

jsp:useBean: It can either build a new bean or access a preexisting one.

<jsp:useBean id="beanName" class="package.Class" />

It can be thought of as equivalent to the scriptlet:

<% package.Class beanName = new package.Class(); %>

But jsp:useBean has two additional advantages:

- It is easier to derive object values from request parameters.

- It is easier to share objects among pages or servlets.

Page 3: JAVABEANS IN JSPsite.iugaza.edu.ps/hmasry/files/Lab9-JavaBeans.pdf · Beans are regular Java classes that follow some simple conventions defined by the JavaBeans specification: A

Advanced Internet Technology Lab

Eng. Haneen 3

jsp:getProperty: Allow access to bean properties.

<jsp:getProperty name="beanName" property="property" />

It is equivalent to the JSP expression:

<%= beanName.getProperty() %>

jsp:setProperty: Allow setting of bean properties.

<jsp:setProperty name="beanName" property="property" value="value" />

It is equivalent to the scriptlet:

<% beanName.setProperty("value"); %>

Example:

- JavaBean

Page 4: JAVABEANS IN JSPsite.iugaza.edu.ps/hmasry/files/Lab9-JavaBeans.pdf · Beans are regular Java classes that follow some simple conventions defined by the JavaBeans specification: A

Advanced Internet Technology Lab

Eng. Haneen 4

- JSP Page

- Output

Beans with Forms

To assign bean properties with request parameters, there are three cases:

1- Explicit Conversion & Assignment.

2- Associating Individual Properties with Input Parameters.

3- Associating All Properties with Input Parameters.

Page 5: JAVABEANS IN JSPsite.iugaza.edu.ps/hmasry/files/Lab9-JavaBeans.pdf · Beans are regular Java classes that follow some simple conventions defined by the JavaBeans specification: A

Advanced Internet Technology Lab

Eng. Haneen 5

Example:

- JSP Form:

- Java Bean:

Page 6: JAVABEANS IN JSPsite.iugaza.edu.ps/hmasry/files/Lab9-JavaBeans.pdf · Beans are regular Java classes that follow some simple conventions defined by the JavaBeans specification: A

Advanced Internet Technology Lab

Eng. Haneen 6

- SaleEntry1.jsp: Explicit Conversion

Page 7: JAVABEANS IN JSPsite.iugaza.edu.ps/hmasry/files/Lab9-JavaBeans.pdf · Beans are regular Java classes that follow some simple conventions defined by the JavaBeans specification: A

Advanced Internet Technology Lab

Eng. Haneen 7

- The Output:

Page 8: JAVABEANS IN JSPsite.iugaza.edu.ps/hmasry/files/Lab9-JavaBeans.pdf · Beans are regular Java classes that follow some simple conventions defined by the JavaBeans specification: A

Advanced Internet Technology Lab

Eng. Haneen 8

- SaleEntry2.jsp: Individual Parameters Associating:

- The Output

Page 9: JAVABEANS IN JSPsite.iugaza.edu.ps/hmasry/files/Lab9-JavaBeans.pdf · Beans are regular Java classes that follow some simple conventions defined by the JavaBeans specification: A

Advanced Internet Technology Lab

Eng. Haneen 9

- SaleEntry3.jsp: All Properties Association

- The Output

Page 10: JAVABEANS IN JSPsite.iugaza.edu.ps/hmasry/files/Lab9-JavaBeans.pdf · Beans are regular Java classes that follow some simple conventions defined by the JavaBeans specification: A

Advanced Internet Technology Lab

Eng. Haneen 10

JavaBeans Sharing

You can specify a scope attribute that associates the bean with more than just the current page.

If beans can be shared, it is useful to obtain references to existing beans, rather than always

building a new object. So, the jsp:useBean action specifies that a new object is instantiated only

if there is no existing one with the same id and scope.

<jsp:useBean id="…" class="…" scope="…" />

Values of the scope attribute:

1- page (<jsp:useBean … scope="page"/> or <jsp:useBean…>)

It is a default value. Bean object should be placed in the PageContext object for the duration of

the current request.

2- request (<jsp:useBean … scope="request"/>)

Bean object should be placed in the HttpServletRequest object for the duration of the current

request.

3- session (<jsp:useBean … scope="session"/>)

Bean will be stored in the HttpSession object associated with the current request.

4- application (<jsp:useBean … scope="application"/>)

Bean will be stored in ServletContext that is shared by all servlets and JSP pages in the Web

application.

Conditional Bean Operations

To execute some statements only if a new bean is created, not if an existing bean is found, use

this form of the action:

<jsp:useBean ...>statements</jsp:useBean>

Instead of the form:

<jsp:useBean .../>

Page 11: JAVABEANS IN JSPsite.iugaza.edu.ps/hmasry/files/Lab9-JavaBeans.pdf · Beans are regular Java classes that follow some simple conventions defined by the JavaBeans specification: A

Advanced Internet Technology Lab

Eng. Haneen 11

Examples:

- JSP Form

- JavaBean

Page 12: JAVABEANS IN JSPsite.iugaza.edu.ps/hmasry/files/Lab9-JavaBeans.pdf · Beans are regular Java classes that follow some simple conventions defined by the JavaBeans specification: A

Advanced Internet Technology Lab

Eng. Haneen 12

- Page Scope

The Output

1st Request

Page 13: JAVABEANS IN JSPsite.iugaza.edu.ps/hmasry/files/Lab9-JavaBeans.pdf · Beans are regular Java classes that follow some simple conventions defined by the JavaBeans specification: A

Advanced Internet Technology Lab

Eng. Haneen 13

2nd request

- Request Scope

BakedBeanDisplay.jsp

Page 14: JAVABEANS IN JSPsite.iugaza.edu.ps/hmasry/files/Lab9-JavaBeans.pdf · Beans are regular Java classes that follow some simple conventions defined by the JavaBeans specification: A

Advanced Internet Technology Lab

Eng. Haneen 14

BakedBeanDisplay2.jsp

The Output

Page 15: JAVABEANS IN JSPsite.iugaza.edu.ps/hmasry/files/Lab9-JavaBeans.pdf · Beans are regular Java classes that follow some simple conventions defined by the JavaBeans specification: A

Advanced Internet Technology Lab

Eng. Haneen 15

- Session Scope

The Output

1st Request.

2nd request that is in the same session.

Page 16: JAVABEANS IN JSPsite.iugaza.edu.ps/hmasry/files/Lab9-JavaBeans.pdf · Beans are regular Java classes that follow some simple conventions defined by the JavaBeans specification: A

Advanced Internet Technology Lab

Eng. Haneen 16

3rd request from another session.

- Application Scope

Page 17: JAVABEANS IN JSPsite.iugaza.edu.ps/hmasry/files/Lab9-JavaBeans.pdf · Beans are regular Java classes that follow some simple conventions defined by the JavaBeans specification: A

Advanced Internet Technology Lab

Eng. Haneen 17

The Output

1st Request

2nd Request in the same session

3rd request from different session.