Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

68
Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    213
  • download

    0

Transcript of Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Page 1: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Object-Oriented Enterprise Application Development

Lecture 8

Advanced JavaBeans

Page 2: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Topics

During this class we will examine:

– More on bean properties

– Bean persistence

– Introspection

– Advanced event concepts

Page 3: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Properties

Page 4: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Properties Revisited

Last week we defined properties as being data members of the class.

We could also make a property the result of a method call; the data could be derived and not stored as part of the class.

Page 5: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Indexed Properties

Beans support the concept of an indexed property.

This is a property that can store multiple values.– In this sense the property mirrors an array.

Page 6: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Sample Code - PhraseBean

1. public class PhraseBeanimplements java.io.Serializable {

2. private Vector phrase = new Vector();

3. public PhraseBean() {4. setPhrase("Hello, World!", 0);5. }

6. public synchronized void setPhrase(String phrase, int i) {

7. this.phrase.setElementAt(phrase, i);8. }

Page 7: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Sample Code - PhraseBean

9. public synchronized String getPhrase(int i) {

10. return (String)phrase.elementAt(i);11. }12. }

Page 8: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Bound Properties

Bound properties allow us to notify listeners

Page 9: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Bean Basics

Page 10: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Sample Code - PhraseBean1. public class PhraseBean

implements java.io.Serializable {2. private String phrase = null;3. public PhraseBean() {4. setPhrase("Hello, World!");5. }

6. public void setPhrase(String phrase) {7. this.phrase = phrase;8. }

9. public String getPhrase() {10. return this.phrase;11. }12. }

Page 11: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

PhraseBean

This is a simple, but legitimate bean.

JavaBeans should implement the java.io.Serializable interface to indicate that they can be persisted.

All beans should provide a default constructor.– This is used by the instantiate() method.

Page 12: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

PhraseBean & JavaServer Pages

A logical place to use this kind of bean is in a JavaServer Page.– Since a bean is just a class, it can be used

anywhere that a class can be used.

To use the bean, we first instantiate it.

Once a bean has been created, we can use its properties and methods.

Page 13: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Sample Code – Phrase.jsp

1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

2. <HTML>3. <HEAD>4. <TITLE>Phrase JSP</TITLE>5. </HEAD>6. <BODY>7. <jsp:useBean id="phrase"

class="PhraseBean"/>8. <H2>

<P>Old phrase is 9. <jsp:getProperty name="phrase"

property="phrase"/>10. </P>

Page 14: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Sample Code – Phrase.jsp

11. <jsp:setProperty name="phrase" property="phrase” value = “Hello again, World!"/>

12. <P>New phrase is 13. <jsp:getProperty name="phrase"

property="phrase"/>14. </P>15. </H2>16. </BODY>17. </HTML>

Page 15: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

useBean Tag

The useBean tag creates a new instance of the bean specified by the class attribute and associates it with the name specified by the id attribute.

<jsp:useBean id="phrase"class="se452.PhraseBean"/>

Page 16: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

getProperty Tag

The getProperty tag retrieves the value of the property specified by the property attribute from the bean specified by the name attribute.

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

Page 17: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

getProperty Tag (cont.)

Instead of using the getProperty tag we could have used a JSP expression:

<%= phrase.getPhrase() %>

Page 18: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

setProperty Tag

The setProperty tag changes the value of the property specified by the property attribute of the bean specified by the name attribute to the value specified by the value attribute.

<jsp:setProperty name="phrase"property="phrase"value = "newValue"/>

Page 19: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

setProperty Tag (cont.)

Instead of using the setProperty tag we could have used a JSP scriptlet:

<% phrase.setPhrase("newValue")%>

Page 20: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Properties and Introspection

The various tags are referencing property names which are just the bean's private data. Why doesn't this violate encapsulation?

Behind the scenes, the JSP is converting the property references into appropriate calls to the set<Property>() and get<Property>() methods.

Page 21: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Linking Forms and Beans

Page 22: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Bean Roles

Along with custom tags, JavaBeans should provide the logic required by the JSP so that it need not be embedded within the JSP code itself.

Since this is the bean's role, it makes sense that we might want to link beans and web pages.

Page 23: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Linking Forms and Beans

One approach to making this linkage might be the following:

<jsp:setProperty name = "bean" property = "beanprop" value = "<%= request. getParameter("formparm")%>"/>

Page 24: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Linking Forms and Beans (cont.)

If the form element has the same name as a bean property, we can use the following syntax:

<jsp:setProperty name = "bean" property = "formparm"/>

Page 25: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Linking Forms and Beans (cont.)

Often the form names don't match the bean properties. In these cases we can use a variation of the setProperty tag:

<jsp:setProperty name = "bean" property = "beanprop" param = "formparm"/>

Page 26: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Linking Forms and Beans (cont.)

The last approach scans all of the request parameters and tries to match them to bean properties:

<jsp:setProperty name = "bean" property = "*"/>

Page 27: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Serialization

Page 28: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Serialization

To be a bean, a class must provide ways of serializing and deserializing itself.

This allows a bean's state to be persisted even though the JVM in which the bean is executing shuts down.

The most common way of providing this functionality is to implement the java.io.Serializable interface.

Page 29: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Serial Files

Serialized beans are saved to a special file called a serial file. All of the bean's properties will be saved to this file.

If a given property shouldn't be serialized, it should be marked as transient.

Page 30: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Serialized Beans

Using a serialized bean is similar to using a "regular" bean. There is a slight change to the useBean tag:

<jsp:useBean id = "phrase" beanName = "phrase" type = "PhraseBean"/>

Page 31: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Bean Scope

Page 32: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Default Scope

By default beans only have scope local to the page for which they were created.

This means that, by default, it isn't possible to create a bean on one page, change its properties on another page, and have those new values appear on a third page.

Page 33: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Page Scope

The default scope for a bean is called page scope. A bean can be created explicitly using page scope with a slight modification to the useBean tag:

<jsp:useBean id = "phrase" type = "PhraseBean" scope = "page"/>

Page 34: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Request Scope

We can create our beans so that they exits for the life of a specific request. This is called request scope:

<jsp:useBean id = "phrase" type = "PhraseBean" scope = "request"/>

Page 35: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Application Scope

Sometimes we want a bean to be visible throughout the entire application. This is called application scope:

<jsp:useBean id = "phrase" type = "PhraseBean" scope = "application"/>

Page 36: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Session Scope

We can make the bean a part of the client's HttpSession. This is called session scope:

<jsp:useBean id = "phrase" type = "PhraseBean" scope = "session"/>

Page 37: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Session Scope (cont.)

When using session scope, it's not uncommon to want a bean to perform some action when it's first created and skip that action thereafter.

For page and request scoped beans we always get a new bean so this kind of behavior isn't necessary.

Page 38: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

useBean Tag Reprise

We can configure a JSP to perform certain bean initialization code when the bean is first created and to ignore that code thereafter.

This is accomplished by placing code between the start and end tags of the useBean tag.

Page 39: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Sample Code – Phrase.jsp (rev.)

1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

2. <HTML>3. <HEAD>4. <TITLE>Phrase JSP</TITLE>5. </HEAD>6. <BODY>7. <jsp:useBean id="phrase"

class="PhraseBean" scope="session">8. <jsp:setProperty name="phrase"

property = "phrase" value = "Hello 1st time, World!"/>

9. </jsp:useBean>

Page 40: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Sample Code – Phrase.jsp10. <H2>

<P>Old phrase is 11. <jsp:getProperty name="phrase"

property="phrase"/>12. </P>13. <jsp:setProperty name="phrase"

property="phrase” value = “Hello again, World!"/>

14. <P>New phrase is 15. <jsp:getProperty name="phrase"

property="phrase"/>16. </P>17. </H2>18. </BODY>19. </HTML>

Page 41: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

useBean Tag Reprise (cont.)

Any JSP code enclosed within the useBean tag, will only be executed when the specified bean is first created.

Because this is based on the client's session, each bean will get this first-time behavior no matter how many users have visited the page before.

Page 42: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

JavaBeans Event Model

Page 43: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Introduction

JavaBeans were originally designed to be graphical components.

This lead to the development of an event model where these classes could listen for events in which they were interested.

This is essentially how all GUI applications function.

Page 44: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Concepts

There are three (3) key elements to the event model:– Event objects: encapsulate the

information specific to a given event.

– Event source: the object that triggered, or fired, the event in the first place.

– Event listeners: an object that is notified when a specific type of event has fired.

Page 45: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Interaction

The event model works as follows:

EventSource

EventListener

EventObject

Fire Event

Register Event Listener

Page 46: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Packages

The java.util package contains the basic classes used within the event model:– java.util.EventObject– java.util.EventListener

There isn't a class for an event source.– When an object wishes to fire an event, it

notifies its listeners by using a callback method.

Page 47: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

EventObject Class

All events extend the java.util.EventObject class.

This class contains important data such as a reference to the event source.

By convention all events should end in the word Event.

Page 48: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

PhraseChangeEvent Class

Suppose that I want to be able to notify listeners that a PhraseBean's phrase has been changed.

The first thing to do is create a new Event object to encapsulate this event.

Page 49: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Sample Code - PhraseChangeEvent1. public class PhraseChangeEvent

extends java.util.EventObject {2. public

PhraseChangeEvent(Object source) {3. super( source );4. }5. }

Page 50: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Listeners

A listener is simply an object that's notified whenever one of a set of events occurs in which that listener is interested.

This is the basic principle behind all modern web, application, and database servers.

Page 51: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

PhraseChangeListener Interface

The next step is to define an interface to be implemented by all objects that are interested in listening for PhraseChangeEvent objects.

This is the PhraseChangeListener interface.

Page 52: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Sample Code - PhraseChangeListener1. public interface PhraseChangeListener

extends java.util.EventListener {2. public void

phraseChange(PhraseChangeEvent event);3. }

Page 53: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

EventListener Interface

The java.util.EventListener interface contains no methods; it's simply used to indicate that a given interface is used to listen for events.

It's a good idea to extend it into your own interface rather than implement it directly.

Page 54: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

PhraseListener Class

Now that we have a listener interface, we can implement a concrete listener.

This will be the PhraseListener class.

Page 55: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Sample Code - PhraseListener

1. public class PhraseListenerimplements PhraseChangeListener {

2. public void phraseChange(PhraseChangeEvent event) {

3. System.out.println("phrase.changed");4. }5. }

Page 56: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Event Sources

There aren't any special classes or interfaces for event sources.

The event source provides listeners the ability to register and de-register:

public void add<type>(<type> listener);

public void remove<type>(<type> listener);

Page 57: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

PhraseBean Revisited

For the PhraseBean to act as an event source, we need to implement the following methods:– addPhraseChangeListener

– removePhraseChangeListener

Page 58: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Sample Code – PhraseBean (rev.)

1. public class PhraseBeanimplements Serializable {

2. private String phrase = null;3. private Vector listeners = null;

4. public PhraseBean() {5. phrase = "Hello, World!";6. listeners = new Vector();7. }

8. public void setPhrase(String phrase) {9. this.phrase = phrase;10. notifyListeners();11. }

Page 59: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Sample Code – PhraseBean (rev.)

12. public String getPhrase() {13. return phrase;14. }

15. public synchronized void addPhraseListsner(PhraseListener pl) {

16. if ( !listeners.contains( pl ) ) {17. listeners.addElement( pl );18. }19. }

Page 60: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Sample Code – PhraseBean (rev.)

20. public synchronized void removePhraseListsner(PhraseListener l){

21. if ( listeners.contains( l ) ) {22. listeners.removeElement( l );23. }24. }

Page 61: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Sample Code – PhraseBean (rev.)25. protected synchronized void

notifyPhraseChange() {

26. PhraseChangeEvent event = new PhraseChangeEvent(this);

27. int count = listeners.size();

28. for (int i = 0; i < count; i++) {

29. Object pl = listeners.elementAt(i);

30. PhraseListener client = (PhraseListener)pl;

31. client.phraseChange(event);

32. }

33. }34. }

Page 62: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

PhraseListener Revisited

Now that the PhraseBean class can notify its listeners, we can allow the PhraseListener class to register itself with a PhraseBean.

Page 63: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Sample Code - PhraseListener

1. public class PhraseListenerimplements PhraseChangeListener {

2. private PhraseBean bean = null;

3. public void phraseChange(PhraseChangeEvent event) {

4. System.out.println("phrase.changed");5. }

6. public PhraseListener(PhraseBean pb) {7. this.bean = pb;8. bean.addPhraseListener( this );9. }10. }

Page 64: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Testing

Testing the events consists of creating a bean and then using it as the event source for one or more event listeners.

Page 65: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Sample Code - TestPhrase

1. public class TestPhrase2. public static void

main(String [] args) {3. PhraseBean bean = new PhraseBean();4. PhraseListener listener1 =

new PhraseListener( bean );5. PhraseListener listener2 =

new PhraseListener( bean );6. bean.setPhrase("new.phrase");7. }8. }

Page 66: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Review

During this class we have discussed:

– More on bean properties

– Bean persistence

– Introspection

– Advanced event concepts

Page 67: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Resources

Core Servlets and JavaServer PagesMarty Hall, Prentice-Hall, Inc., 2000.ISBN: 0-13-089340-4

Developing JavaBeansRobert Englander, O'Reilly, 1997.ISBN: 1-56592-289-1

Page 68: Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Coming Attractions

Next week we'll finish our discussion of JavaBeans and how to perform more advanced event modeling.