Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.

Slides:



Advertisements
Similar presentations
JSP and Servelets.
Advertisements

CS4273: Distributed System Technologies and Programming I Lecture 11: JavaServer Pages (JSP)
Chapter 81 JavaBeans JavaServer Pages By Xue Bai.
 Copyright Wipro Technologies JSP Ver 1.0 Page 1 Talent Transformation Java Server Pages.
Using JavaServer Pages Harry R. Erwin, PhD CIT304/CSE301.
JSP: JavaServer Pages Juan Cruz Kevin Hessels Ian Moon.
Java Beans & Serialization CS-328 Dick Steflik. Java Beans Java based component technology –originally developed to provide java with a component technology.
28/1/2001 Seminar in Databases in the Internet Environment Introduction to J ava S erver P ages technology by Naomi Chen.
JavaBeans introduction Klaus-Peter Heidrich for the University of Karlstad Inst. for Information Technology Dept. of Computer Science.
Object-Oriented Enterprise Application Development Introduction to JavaBeans.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
JSP Architecture  JSP is a simple text file consisting of HTML or XML content along with JSP elements  JSP packages define the interface for the compiled.
Introduction to Java web programming Dr Jim Briggs JWP intro1.
Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.
11. Java-Based Web: JSP, JSF. 2 Motto: Rule 1: Our client is always right Rule 2: If you think our client is wrong, see Rule 1. - Anonymous.
Java Server Pages CS-422. What are JSPs A logical evolution of java servlets –most servlets dynamically create HTML and integrate it with some computational.
CSC 2720 Building Web Applications Using Java Beans, Custom Tags and Tag Libraries in JSP pages.
Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 1 of 37 J2EE Web Components Pre-assessment Questions 1.The _____________ attribute of a JSP page.
JDeveloper 10g and JavaServer Faces: High-Performance UIs on the Web Avrom Roy-Faderman Senior Programmer May, 2006.
What Are Beans? beans are simply Java classes that are written in a standard format. A bean class must have a zero-argument (default) constructor. A bean.
JSP Java Server Pages Softsmith Infotech.
Li Tak Sing COMPS311F. Static attributes in Servlets Since Servlets are also Java classes, you can also use static attributes to store values that can.
Introduction to JavaServer Pages (JSP) Slides from Dr. Mark Llewellyn.
Java Server Pages Lecture July Java Server Pages Java Server Pages (JSPs) provide a way to separate the generation of dynamic content (java)
Jordan Anastasiade. All rights reserved.
SWE 316: Software Design and Architecture Objectives Lecture # 19 Java Beans SWE 316: Software Design and Architecture  Understand  what JavaBeans (“Beans”)
Chapter 8 Script-free pages. Problem with scripting in JSP When you use scripting (declaration, scriplet, expressions) in your JSP, you actually put Java.
Mark Dixon 1 12 – Java Beans. Mark Dixon 2 Session Aims & Objectives Aims –To cover the use of Java Beans Objectives, by end of this week’s sessions,
Chapter 8 Cookies And Security JavaScript, Third Edition.
JAVA SERVER PAGES. 2 SERVLETS The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA James Faeldon CS 119 Enterprise Systems Programming.
Writing Enterprise Applications with J2EE (Fourth lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)
JSP Fundamentals Elements of a JSP Using Beans with JSP Integrating Servlets and JSP.
Copyright © 2002 ProsoftTraining. All rights reserved. JavaServer Pages.
Java Server Pages An introduction to JSP. Containers and Components Several clients – one system.
Introduction to Java Beans CIS 421 Web-based Java Programming.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 27 JavaBeans and.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Slides © Marty Hall, book © Sun Microsystems Press 1 Using JavaBeans with JSP Core Servlets & JSP book:
JAVA BEANS JSP - Standard Tag Library (JSTL) JAVA Enterprise Edition.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 43 JavaServer Page.
Introduction to Software Components: the JavaBeans specs Babak Esfandiari.
1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
Java Beans - Basics CIS 421 Web-based Java Programming.
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
JSP in Action. JSP Standard Actions forward, include, useBean, setProperty, getProperty, text, element, and plugin Additional actions param, params,
Chapter 14 Using JavaBeans Components in JSP Documents.
Bayu Priyambadha, S.Kom. Static content  Web Server delivers contents of a file (html) 1. Browser sends request to Web Server 3. Web Server sends HTML.
JSP Java Server Pages. Hello, !
17 Copyright © 2004, Oracle. All rights reserved. Integrating J2EE Components.
Introduction to Server-Side Web Development Introduction to Server-Side Web Development Introduction to Server-Side Web JavaBeans; basic concepts and syntax.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
JSP Action Elements Lec - 37.
Structure of a web application
Scripted Page Web App Development (Java Server Pages)
Java Beans Sagun Dhakhwa.
Scope and State Handling in JSPs
JavaBeans and JSP CS-422.
Introduction to Java Bean
Introduction to Java Bean
Java Server Pages (JSP)
Java Server Pages B.Ramamurthy.
Pre-assessment Questions
Scripted Page Web Application Development (Java Server Pages)
Presentation transcript:

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

Properties

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.

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.

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. }

Sample Code - PhraseBean 9. public synchronized String getPhrase(int i) { 10. return (String)phrase.elementAt(i); 11. } 12. }

Bound Properties  Bound properties allow us to notify listeners

Bean Basics

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. }

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.

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.

Sample Code – Phrase.jsp Phrase JSP Old phrase is

Sample Code – Phrase.jsp New phrase is

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.

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

getProperty Tag (cont.)  Instead of using the getProperty tag we could have used a JSP expression:

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.

setProperty Tag (cont.)  Instead of using the setProperty tag we could have used a JSP scriptlet: <% phrase.setPhrase("newValue") %>

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.

Linking Forms and Beans

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.

Linking Forms and Beans  One approach to making this linkage might be the following: <jsp:setProperty name = "bean" property = "beanprop" value = "<%= request. getParameter("formparm")%>" />

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

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

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

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.

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.

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

Bean Scope

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

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

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

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

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.

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.

Sample Code – Phrase.jsp (rev.) Phrase JSP

Sample Code – Phrase.jsp 10. Old phrase is New phrase is

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.

JavaBeans Event Model

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.

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.

Interaction  The event model works as follows: Event Source Event Listener Event Object Fire Event Register Event Listener

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.

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.

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.

Sample Code - PhraseChangeEvent 1. public class PhraseChangeEvent extends java.util.EventObject { 2. public PhraseChangeEvent(Object source) { 3. super( source ); 4. } 5. }

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.

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.

Sample Code - PhraseChangeListener 1. public interface PhraseChangeListener extends java.util.EventListener { 2. public void phraseChange(PhraseChangeEvent event); 3. }

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.

PhraseListener Class  Now that we have a listener interface, we can implement a concrete listener.  This will be the PhraseListener class.

Sample Code - PhraseListener 1. public class PhraseListener implements PhraseChangeListener { 2. public void phraseChange(PhraseChangeEvent event) { 3. System.out.println("phrase.changed"); 4. } 5. }

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

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

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. }

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. }

Sample Code – PhraseBean (rev.) 20. public synchronized void removePhraseListsner(PhraseListener l){ 21. if ( listeners.contains( l ) ) { 22. listeners.removeElement( l ); 23. } 24. }

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. }

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

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. }

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

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. }

Review  During this class we have discussed: –More on bean properties –Bean persistence –Introspection –Advanced event concepts

Resources  Core Servlets and JavaServer Pages Marty Hall, Prentice-Hall, Inc., ISBN:  Developing JavaBeans Robert Englander, O'Reilly, ISBN:

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