Download presentation
Presentation is loading. Please wait.
1
Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans
2
Topics During this class we will examine: –More on bean properties –Bean persistence –Introspection –Advanced event concepts
3
Properties
4
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.
5
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.
6
Sample Code - PhraseBean 1. public class PhraseBean implements 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. }
7
Sample Code - PhraseBean 9. public synchronized String getPhrase(int i) { 10. return (String)phrase.elementAt(i); 11. } 12. }
8
Bound Properties Bound properties allow us to notify listeners
9
Bean Basics
10
Sample Code - PhraseBean 1. 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. }
11
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.
12
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.
13
Sample Code – Phrase.jsp 1. 2. 3. 4. Phrase JSP 5. 6. 7. 8. Old phrase is 9. 10.
14
Sample Code – Phrase.jsp 11. 12. New phrase is 13. 14. 15. 16. 17.
15
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.
16
getProperty Tag The getProperty tag retrieves the value of the property specified by the property attribute from the bean specified by the name attribute.
17
getProperty Tag (cont.) Instead of using the getProperty tag we could have used a JSP expression:
18
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.
19
setProperty Tag (cont.) Instead of using the setProperty tag we could have used a JSP scriptlet: <% phrase.setPhrase("newValue") %>
20
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 () and get () methods.
21
Linking Forms and Beans
22
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.
23
Linking Forms and Beans One approach to making this linkage might be the following: <jsp:setProperty name = "bean" property = "beanprop" value = "<%= request. getParameter("formparm")%>" />
24
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" />
25
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" />
26
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 = "*" />
27
Serialization
28
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.
29
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.
30
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" />
31
Bean Scope
32
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.
33
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" />
34
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" />
35
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" />
36
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" />
37
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.
38
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.
39
Sample Code – Phrase.jsp (rev.) 1. 2. 3. 4. Phrase JSP 5. 6. 7. 8. 9.
40
Sample Code – Phrase.jsp 10. Old phrase is 11. 12. 13. 14. New phrase is 15. 16. 17. 18. 19.
41
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.
42
JavaBeans Event Model
43
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.
44
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.
45
Interaction The event model works as follows: Event Source Event Listener Event Object Fire Event Register Event Listener
46
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.
47
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.
48
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.
49
Sample Code - PhraseChangeEvent 1. public class PhraseChangeEvent extends java.util.EventObject { 2. public PhraseChangeEvent(Object source) { 3. super( source ); 4. } 5. }
50
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.
51
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.
52
Sample Code - PhraseChangeListener 1. public interface PhraseChangeListener extends java.util.EventListener { 2. public void phraseChange(PhraseChangeEvent event); 3. }
53
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.
54
PhraseListener Class Now that we have a listener interface, we can implement a concrete listener. This will be the PhraseListener class.
55
Sample Code - PhraseListener 1. public class PhraseListener implements PhraseChangeListener { 2. public void phraseChange(PhraseChangeEvent event) { 3. System.out.println("phrase.changed"); 4. } 5. }
56
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 ( listener); public void remove ( listener);
57
PhraseBean Revisited For the PhraseBean to act as an event source, we need to implement the following methods: –addPhraseChangeListener –removePhraseChangeListener
58
Sample Code – PhraseBean (rev.) 1. public class PhraseBean implements 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. }
59
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. }
60
Sample Code – PhraseBean (rev.) 20. public synchronized void removePhraseListsner(PhraseListener l){ 21. if ( listeners.contains( l ) ) { 22. listeners.removeElement( l ); 23. } 24. }
61
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. }
62
PhraseListener Revisited Now that the PhraseBean class can notify its listeners, we can allow the PhraseListener class to register itself with a PhraseBean.
63
Sample Code - PhraseListener 1. public class PhraseListener implements 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. }
64
Testing Testing the events consists of creating a bean and then using it as the event source for one or more event listeners.
65
Sample Code - TestPhrase 1. public class TestPhrase 2. 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. }
66
Review During this class we have discussed: –More on bean properties –Bean persistence –Introspection –Advanced event concepts
67
Resources Core Servlets and JavaServer Pages Marty Hall, Prentice-Hall, Inc., 2000. ISBN: 0-13-089340-4 Developing JavaBeans Robert Englander, O'Reilly, 1997. ISBN: 1-56592-289-1
68
Coming Attractions Next week we'll finish our discussion of JavaBeans and how to perform more advanced event modeling.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.