murach’s java servlets/jsp (2 nd ed.), c19© 2008, mike murach & associates, inc.slide 1

21
Murach’s Java Servlets/JSP (2 nd Ed.), C19 © 2008, Mike Murach & Associate s, Inc. Slide 1 C hapter19 How to w ork w ith listeners

Post on 19-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Murach’s Java Servlets/JSP (2 nd Ed.), C19© 2008, Mike Murach & Associates, Inc.Slide 1

Murach’s Java Servlets/JSP (2nd Ed.), C19 © 2008, Mike Murach & Associates, Inc. Slide 1

Chapter 19

How to work with listeners

Page 2: Murach’s Java Servlets/JSP (2 nd Ed.), C19© 2008, Mike Murach & Associates, Inc.Slide 1

Murach’s Java Servlets/JSP (2nd Ed.), C19 © 2008, Mike Murach & Associates, Inc. Slide 2

Objectives

Applied

Create, register, and use a listener class.

Knowledge

Describe the use of a listener class.

Page 3: Murach’s Java Servlets/JSP (2 nd Ed.), C19© 2008, Mike Murach & Associates, Inc.Slide 1

Murach’s Java Servlets/JSP (2nd Ed.), C19 © 2008, Mike Murach & Associates, Inc. Slide 3

A listener class that implements the ServletContextListener interface package util; import javax.servlet.*; import java.util.*; import business.*; import data.*; public class CartContextListener implements ServletContextListener { public void contextInitialized(ServletContextEvent event) { ServletContext sc = event.getServletContext(); // initialize the customer service email address String custServEmail = sc.getInitParameter("custServEmail"); sc.setAttribute("custServEmail", custServEmail);

Page 4: Murach’s Java Servlets/JSP (2 nd Ed.), C19© 2008, Mike Murach & Associates, Inc.Slide 1

Murach’s Java Servlets/JSP (2nd Ed.), C19 © 2008, Mike Murach & Associates, Inc. Slide 4

A listener class (cont.) // initialize the current year that's used in the // copyright notice GregorianCalendar currentDate = new GregorianCalendar(); int currentYear = currentDate.get(Calendar.YEAR); sc.setAttribute("currentYear", currentYear); // initialize the path for the products text file String productsPath = sc.getRealPath("WEB-INF/products.txt"); sc.setAttribute("productsPath", productsPath); // initialize the list of products ArrayList<Product> products = new ArrayList<Product>(); products = ProductIO.getProducts(productsPath); sc.setAttribute("products", products); }

Page 5: Murach’s Java Servlets/JSP (2 nd Ed.), C19© 2008, Mike Murach & Associates, Inc.Slide 1

Murach’s Java Servlets/JSP (2nd Ed.), C19 © 2008, Mike Murach & Associates, Inc. Slide 5

A listener class (cont.) public void contextDestroyed(ServletContextEvent event) { // no cleanup necessary } }

Page 6: Murach’s Java Servlets/JSP (2 nd Ed.), C19© 2008, Mike Murach & Associates, Inc.Slide 1

Murach’s Java Servlets/JSP (2nd Ed.), C19 © 2008, Mike Murach & Associates, Inc. Slide 6

How to use a ServletContextListener A listener is a class that listens for various events that can occur in

an application and provides methods that respond to those events when they occur.

To code the class for a listener, you must implement one of the listener interfaces that are stored in the javax.servlet and javax.servlet.http packages.

A class that implements a listener interface must override the methods of that interface.

Page 7: Murach’s Java Servlets/JSP (2 nd Ed.), C19© 2008, Mike Murach & Associates, Inc.Slide 1

Murach’s Java Servlets/JSP (2nd Ed.), C19 © 2008, Mike Murach & Associates, Inc. Slide 7

A web.xml file that includes a listener element <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>CartServlet</servlet-name> <servlet-class>cart.CartServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CartServlet</servlet-name> <url-pattern>/cart</url-pattern> </servlet-mapping> <listener> <listener-class>util.CartContextListener </listener-class> </listener>

Page 8: Murach’s Java Servlets/JSP (2 nd Ed.), C19© 2008, Mike Murach & Associates, Inc.Slide 1

Murach’s Java Servlets/JSP (2nd Ed.), C19 © 2008, Mike Murach & Associates, Inc. Slide 8

A web.xml file that includes a listener element (cont.) <context-param> <param-name>custServEmail</param-name> <param-value>[email protected]</param-value> </context-param> <session-config> <session-timeout>30</session-timeout> </session-config> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>

Page 9: Murach’s Java Servlets/JSP (2 nd Ed.), C19© 2008, Mike Murach & Associates, Inc.Slide 1

Murach’s Java Servlets/JSP (2nd Ed.), C19 © 2008, Mike Murach & Associates, Inc. Slide 9

The listener elements

Element Description

listener Adds a listener to the application.

listener-class Specifies the fully qualified name of a class that implements a listener.

Page 10: Murach’s Java Servlets/JSP (2 nd Ed.), C19© 2008, Mike Murach & Associates, Inc.Slide 1

Murach’s Java Servlets/JSP (2nd Ed.), C19 © 2008, Mike Murach & Associates, Inc. Slide 10

A JSP file that uses attributes set by a listener <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Murach's Java Servlets and JSP</title> </head> <body> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <h1>CD list</h1> <table cellpadding="5" border=1> <tr valign="bottom"> <td align="left"><b>Description</b></td> <td align="left"><b>Price</b></td> <td align="left"></td> </tr>

Page 11: Murach’s Java Servlets/JSP (2 nd Ed.), C19© 2008, Mike Murach & Associates, Inc.Slide 1

Murach’s Java Servlets/JSP (2nd Ed.), C19 © 2008, Mike Murach & Associates, Inc. Slide 11

A JSP file that uses attributes set by a listener (cont.) <c:forEach var="product" items="${products}"> <tr valign="top"> <td>${product.description}</td> <td>${product.priceCurrencyFormat}</td> <td><a href= "<c:url value='/cart?productCode=${product.code}' />"> Add To Cart</a></td> </tr> </c:forEach> </table> <p> For customer service, please send an email to ${custServEmail}. </p>

Page 12: Murach’s Java Servlets/JSP (2 nd Ed.), C19© 2008, Mike Murach & Associates, Inc.Slide 1

Murach’s Java Servlets/JSP (2nd Ed.), C19 © 2008, Mike Murach & Associates, Inc. Slide 12

A JSP file that uses attributes set by a listener (cont.) <p> &copy; Copyright ${currentYear} Mike Murach &amp; Associates, Inc. All rights reserved. </p> </body> </html>

Page 13: Murach’s Java Servlets/JSP (2 nd Ed.), C19© 2008, Mike Murach & Associates, Inc.Slide 1

Murach’s Java Servlets/JSP (2nd Ed.), C19 © 2008, Mike Murach & Associates, Inc. Slide 13

The ServletContext interfaces

Interface Provides methods that are executed when…

ServletContextListener The ServletContext object is initialized and destroyed (that is, when the application is started and stopped).

SerlvetContextAttributeListener Attributes are added to, removed from, or replaced in the ServletContext object.

Page 14: Murach’s Java Servlets/JSP (2 nd Ed.), C19© 2008, Mike Murach & Associates, Inc.Slide 1

Murach’s Java Servlets/JSP (2nd Ed.), C19 © 2008, Mike Murach & Associates, Inc. Slide 14

The HttpSession interfaces

Interface Provides methods that are executed when…

HttpSessionListener The session object is created and destroyed for a user (every time a new user accesses an application or the user session is destroyed).

HttpSessionAttributeListener Attributes are added to, removed from, or replaced in the session object.

HttpSessionBindingListener An object is bound to or unbound from the session.

HttpSessionActivationListener The session is activated or deactivated (that is, when the session is migrating to another JVM).

Page 15: Murach’s Java Servlets/JSP (2 nd Ed.), C19© 2008, Mike Murach & Associates, Inc.Slide 1

Murach’s Java Servlets/JSP (2nd Ed.), C19 © 2008, Mike Murach & Associates, Inc. Slide 15

The ServletRequest interfaces

Interface Provides methods that are executed when…

ServletRequestListener A request object is initialized and destroyed (every time the server receives and processes a request).

ServletRequestAttributeListener Attributes are added to, removed from, or replaced in the request object.

Page 16: Murach’s Java Servlets/JSP (2 nd Ed.), C19© 2008, Mike Murach & Associates, Inc.Slide 1

Murach’s Java Servlets/JSP (2nd Ed.), C19 © 2008, Mike Murach & Associates, Inc. Slide 16

The ServletContext interfaces The ServletContextListener interface contextInitialized(ServletContextEvent e) contextDestroyed(ServletContextEvent e)

The ServletContextAttributeListener interface attributeAdded(ServletContextAttributeEvent e) attributeRemoved(ServletContextAttributeEvent e) attributeReplaced(ServletContextAttributeEvent e)

Page 17: Murach’s Java Servlets/JSP (2 nd Ed.), C19© 2008, Mike Murach & Associates, Inc.Slide 1

Murach’s Java Servlets/JSP (2nd Ed.), C19 © 2008, Mike Murach & Associates, Inc. Slide 17

The HttpSession interfaces The HttpSessionListener interface sessionCreated(HttpSessionEvent e) sessionDestroyed(HttpSessionEvent e)

The HttpSessionAttributeListener interface attributeAdded(HttpSessionBindingEvent e) attributeRemoved(HttpSessionBindingEvent e) attributeReplaced(HttpSessionBindingEvent e)

The HttpSessionBindingListener interface valueBound(HttpSessionBindingEvent e) valueUnbound(HttpSessionBindingEvent e)

The HttpSessionActivationListener interface sessionDidActivate(HttpSessionEvent e) sessionWillPassivate(HttpSessionEvent e)

Page 18: Murach’s Java Servlets/JSP (2 nd Ed.), C19© 2008, Mike Murach & Associates, Inc.Slide 1

Murach’s Java Servlets/JSP (2nd Ed.), C19 © 2008, Mike Murach & Associates, Inc. Slide 18

The ServletRequest interfaces The ServletRequestListener interface requestInitialized(ServletRequestEvent e) requestDestroyed(ServletRequestEvent e)

The ServletRequestAttributeListener interface attributeAdded(ServletRequestAttributeEvent e) attributeRemoved(ServletRequestAttributeEvent e) attributeReplaced(ServletRequestAttributeEvent e)

Page 19: Murach’s Java Servlets/JSP (2 nd Ed.), C19© 2008, Mike Murach & Associates, Inc.Slide 1

Murach’s Java Servlets/JSP (2nd Ed.), C19 © 2008, Mike Murach & Associates, Inc. Slide 19

The ServletContextEvent class

Method Description

getServletContext() Returns the ServletContext object that was initialized or destroyed.

The ServletContextAttributeEvent class

Method Description

getName() Returns a string for the name of the attribute that was added to, removed from, or replaced in the ServletContext object.

getValue() Returns an object for the value of the attribute that was added to, removed from, or replaced in the ServletContext object.

Page 20: Murach’s Java Servlets/JSP (2 nd Ed.), C19© 2008, Mike Murach & Associates, Inc.Slide 1

Murach’s Java Servlets/JSP (2nd Ed.), C19 © 2008, Mike Murach & Associates, Inc. Slide 20

The HttpSessionEvent class

Method Description

getSession() Returns the HttpSession object that was changed.

The HttpSessionBindingEvent class

Method Description

getName() Returns a string for the name of the attribute that was added to, removed from, or replaced in the HttpSession object.

getValue() Returns an object for the value of the attribute that was added to, removed from, or replaced in the HttpSession object.

Page 21: Murach’s Java Servlets/JSP (2 nd Ed.), C19© 2008, Mike Murach & Associates, Inc.Slide 1

Murach’s Java Servlets/JSP (2nd Ed.), C19 © 2008, Mike Murach & Associates, Inc. Slide 21

The ServletRequestEvent class

Method Description

getServletRequest() Returns the ServletRequest object that was initialized or destroyed.

The ServletRequestAttributeEvent class

Method Description

getName() Returns a string for the name of the attribute that was added to, removed from, or replaced in the ServletRequest object.

getValue() Returns an object for the value of the attribute that was added to, removed from, or replaced in the ServletRequest object.