Chapter 81 JavaBeans JavaServer Pages By Xue Bai.

30
Chapter 8 1 JavaBeans JavaServer Pages By Xue Bai

Transcript of Chapter 81 JavaBeans JavaServer Pages By Xue Bai.

Page 1: Chapter 81 JavaBeans JavaServer Pages By Xue Bai.

Chapter 8 1

JavaBeans

JavaServer Pages

By Xue Bai

Page 2: Chapter 81 JavaBeans JavaServer Pages By Xue Bai.

Chapter 8 2

Objectives

In this chapter, you will:

• Survey the basics of the Java programming language

• Write JavaBeans

• Compile and install bean classes

• Use beans with action tags

• Get and set bean’s properties

• Connect beans with forms

• Use beans in scriptlets

• Write a file bean to read from and write to files

Page 3: Chapter 81 JavaBeans JavaServer Pages By Xue Bai.

Chapter 8 3

Java Primer

• JavaBeans are written in Java

• JSP uses the same syntax as Java

programming language

• JSP is built on top of servlet, which in turn

is built on Java

Page 4: Chapter 81 JavaBeans JavaServer Pages By Xue Bai.

Chapter 8 4

Objects and Classes

• A class is a template or blueprint for objects

• A class consists of three parts:

– Data members

– Methods

– Constructors

• Objects are instances of classes

Page 5: Chapter 81 JavaBeans JavaServer Pages By Xue Bai.

Chapter 8 5

Class Example

public class Circle{

private float radius;

private float area;

public Circle(){

radius=0.0f;

area = 0.0f;

}

public Circle(float radius){

this.radius = radius;

area = 0.0f;

}

public void setRadius(float radius){

this.radius = radius; } public void calculateArea(){ area = radius*Math.PI; } public float getArea(){ return

area;}}

Page 6: Chapter 81 JavaBeans JavaServer Pages By Xue Bai.

Chapter 8 6

Vector

• One of the most frequently used classes in Java

• Create Vector object:

– Vector v1 = new Vector();

– Vector v2 = new Vector(Collection c);

– Vector v3 = new Vector(int initialCapacity);

– Vector v4 = new Vector(int initialCapacity, int increments);

Page 7: Chapter 81 JavaBeans JavaServer Pages By Xue Bai.

Chapter 8 7

Method Usage

addElement(Object o)or add(Object o)

Appends the specified object in the argument to the end of the vector.

elementAt(int index) Returns the component at the specified index. The index of the first element is zero, the second one is 1, and so on.

Contains(Object o) Tests whether the specified object is a component in this vector. It returns a Boolean value

indexOf(object o) Searches for the first occurrence of the given argument, testing for equality using the equals method. It returns the index of the element in the vector if found, -1 otherwise.

InsertElementAt(Object o, int index)

Inserts the specified object as a component in this vector at the specified index

size() Returns the number of components in this vector as an int

Page 8: Chapter 81 JavaBeans JavaServer Pages By Xue Bai.

Chapter 8 8

Using Vector

• Storing objects

Vector v = new Vector();

v.addElement(“Lynne”);

• Retrieving object:

Object obj = v.elementAt(0);

• Cast object to its original data type:

String s = (String)obj;

Page 9: Chapter 81 JavaBeans JavaServer Pages By Xue Bai.

Chapter 8 9

Writing JavaBean

• Reusable components in JSP

• Written in Java

• Regular Java classes with certain rules:

– Rules for GET and SET methods:

– If a data member is called foo, then its GET and SET methods as follow:

• getFoo();

• setFoo(“arguments”);

Page 10: Chapter 81 JavaBeans JavaServer Pages By Xue Bai.

Chapter 8 10

Class Example

package com.jspbook.chapter08;

public class SimpleBean{

private String message;

public SimpleBean(){

message = "Hi, there.";

}

public String getMessage(){ return message;}

public void setMessage(String aMessage){

message = aMessage;}

}

Page 11: Chapter 81 JavaBeans JavaServer Pages By Xue Bai.

Chapter 8 11

Installing Bean Classes

• A directory the JSP engine looks for in the

classpath

• One choice in Tomcat is:

– TomatRoot\WEB-INF\classes

Page 12: Chapter 81 JavaBeans JavaServer Pages By Xue Bai.

Chapter 8 12

Using Beans

• With action tags, you can use these beans

• No Java programming language is required in order to use beans

• With simple action tags, you can utilize all Java powerful features in JSP:

– Perform I/O

– Use well-fined collection classes, etc.

Page 13: Chapter 81 JavaBeans JavaServer Pages By Xue Bai.

Chapter 8 13

Instantiate a Bean Object

<jsp:useBean id="bean_name" class="class_name"

/>

• Create and load a JavaBean

• “bean_name” is the name that refers to the bean

• It must be unique everywhere it is to be used

• No two beans can have the same name in the same page

• The bean name serves as a local variable reference to the bean object

Page 14: Chapter 81 JavaBeans JavaServer Pages By Xue Bai.

Chapter 8 14

Accessing Bean Properties

<jsp:getProperty name=”bean_name” property=”property_name” />

• This tag retrieves the value of a bean property, converts it to a string, and inserts it into the output

• The two required attributes are name and property

• The “bean name” is the same name specified in the ID attribute when the bean is created, and the “property name” is the name of the property to get

Page 15: Chapter 81 JavaBeans JavaServer Pages By Xue Bai.

Chapter 8 15

Set Bean Properties

<jsp:setProeprty name=”bean_name” property=”property_name”

value=”a new property value” />

• This tag assigns a new value to the specified property

• In this tag, the “value” attribute specifies the new value to be assigned to the bean property

Page 16: Chapter 81 JavaBeans JavaServer Pages By Xue Bai.

Chapter 8 16

Setting a Bean Property Form

Page 17: Chapter 81 JavaBeans JavaServer Pages By Xue Bai.

Chapter 8 17

Setting a Bean Property

Page 18: Chapter 81 JavaBeans JavaServer Pages By Xue Bai.

Chapter 8 18

Beans and Forms

• Beans interact with forms to generate

dynamic Web pages

• Use form control elements to interact

with bean properties

Page 19: Chapter 81 JavaBeans JavaServer Pages By Xue Bai.

Chapter 8 19

Setting Properties with form Input Fields

<jsp:setParamter name=”bean_name” property=”foo”/>

• The value assigned to the property is presumed to come from the form

• In order for this technique to work, the bean must have a public setter method called setFoo, and this method takes a String object as an argument

• This tag requires the match between the property name and the form’s input parameter name

Page 20: Chapter 81 JavaBeans JavaServer Pages By Xue Bai.

Chapter 8 20

Setting Properties with form Input Fields

<jsp:setProperty name=”bean_name”

property=”propertyName” param=”inputFieldName”/>

• Used when the name of the form parameter

and the name of the property do not match

• This tag uses the form input field called

“inputFieldName” to set the bean’s property

called “propertyName”

Page 21: Chapter 81 JavaBeans JavaServer Pages By Xue Bai.

Chapter 8 21

Setting Properties with Form Input Fields

<jsp:setProperty name=”bean_name” property=”*”/>

• Looks through all the input fields provided

by the form and all the methods provided

by the bean, and links them together

automatically

Page 22: Chapter 81 JavaBeans JavaServer Pages By Xue Bai.

Chapter 8 22

Working with Arrays

• Multiple values are associated with one

input field

• You can also use a bean to get all these

values associated with the same input

field on a form

Page 23: Chapter 81 JavaBeans JavaServer Pages By Xue Bai.

Chapter 8 23

A SETTER Method Taking Array Argument

public void setValues(String[] values){

this.values = values;

}

Page 24: Chapter 81 JavaBeans JavaServer Pages By Xue Bai.

Chapter 8 24

Beans and Scriptlet

• <jsp:useBean id=”bean_name” class=”class_Path”/> locates or instantiates a bean object from the class and binds the variable specified as “bean_name” to the bean object

• After the variable “bean_name” has been bound to the bean object, you can use this variable as a reference to the bean object in your JSP script

• Since the variable is a reference to the bean object, you can call all methods provided by the bean in your JSP scriptlets

Page 25: Chapter 81 JavaBeans JavaServer Pages By Xue Bai.

Chapter 8 25

Beans and Scriptlets Example

<jsp:useBean id="calcBean" class="com.jspbook.chapter08.CalcBean1"/>

<%

calcBean.setValue1(request.getParameter("value1"));

calcBean.setValue2(request.getParameter("value2"));

%>

<%= calcBean.getSum() %>

Page 26: Chapter 81 JavaBeans JavaServer Pages By Xue Bai.

Chapter 8 26

A Bean performs I/O

• Write JavaBean class to

perform I/O

• Use Bean in JSP

Page 27: Chapter 81 JavaBeans JavaServer Pages By Xue Bai.

Chapter 8 27

Bulletin Board

Page 28: Chapter 81 JavaBeans JavaServer Pages By Xue Bai.

Chapter 8 28

Posting a Bulletin

Page 29: Chapter 81 JavaBeans JavaServer Pages By Xue Bai.

Chapter 8 29

Saving the Bulletin

Page 30: Chapter 81 JavaBeans JavaServer Pages By Xue Bai.

Chapter 8 30

Viewing the Bulletin