JavaServer Pages (JSP)

Slides:



Advertisements
Similar presentations
SIP Servlets. SIP Summit SIP Servlets Problem Statement Want to enable construction of a wide variety of IP telephony.
Advertisements

Dispatching, monitoring, filtering
Using JavaServer Pages Harry R. Erwin, PhD CIT304/CSE301.
MC365 Application Servers: Servlets. Today We Will Cover: What a servlet is The HTTPServlet and some of its more important methods How to configure the.
Object-Oriented Enterprise Application Development Tomcat 3.2 Configuration Last Updated: 03/30/2001.
1 Listeners A listener is an event handler that the server invokes when certain events occur (e.g. web application initialization/shutdown, session created/timed.
Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.
/ department of mathematics and computer sciencedepartment of mathematics and computer science / architecture of information systems.
Session-02.
Java Servlets and JSP.
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.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 37 - JavaServer Pages (JSP): Bonus for Java Developers Outline 37.1 Introduction 37.2 JavaServer.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 8 Servlets (Based on Møller and Schwartzbach,
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.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 9 JavaServer Pages (JSP) (Based on Møller.
CSC 2720 Building Web Applications Using Java Beans, Custom Tags and Tag Libraries in JSP pages.
Java Servlets CS-422. Application Mapping Your servlet application will be mapped to a directory structure: –“myapp” maps to some directory C:/docs/apps/myapp.
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.
Introduction to Java Server Pages (JSPs) Robert Thornton.
Web Application Basic Scopes Application Session Request Page 1.
1 JSP with Custom Tags Blake Adams Introduction Advanced Java Server Pages – Custom Tags Keyterms: - Tag Library Descriptor(TLD) - Tag Libraries.
® IBM Software Group © 2007 IBM Corporation Servlet Listeners
Fall 2007cs4201 Advanced Java Programming Umar Kalim Dept. of Communication Systems Engineering
Java Servlets Lec 27. Creating a Simple Web Application in Tomcat.
JSP Filters 23-Oct-15. JSP - FILTERS A filter is an object that can transform a request or modify a response. Filters are not servlets; they don't actually.
CSC 2720 Building Web Applications JavaServer Pages (JSP) The Basics.
JSP Tag Libraries Lec Last Lecture Example We incorporated JavaBeans in “Course Outline” Example But still have to write java code inside java.jsp.
@2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING.
JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can servlets do? ► Servlets Vs JSP ► Syntax ► Samples ► JavaBean ►
Chính phủ điện tử TS. Phạm Văn Tính Khoa CNTT, ĐH Nông Lâm TP.HCM
CSC 2720 Building Web Applications JavaServer Pages (JSP) JSP Directives and Action Elements.
Servlet Filters JAVA Enterprise Edition. Servlet Filters Servlet Filters are Java classes that can be used in Servlet Programming for the following purposes:
Java Enterprise Edition Programming Page 1 of 9Configuring Servlets Web Application Context Name  In multiple web applications, a “context name” is used.
©SoftMoore ConsultingSlide 1 Filters. Filters can be used in a web application to intercept, examine, and possibly transform requests or responses associated.
CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, written in Java code, that.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
Modern Programming Language. Web Container & Web Applications Web applications are server side applications The most essential requirement.
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.
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
Chapter 4 Request and Response. Servlets are controlled by the container.
1 Lecture 8 George Koutsogiannakis/Summer 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES.
17 Copyright © 2004, Oracle. All rights reserved. Integrating J2EE Components.
CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles.
Struts 2 Development. Topics  Roles in Struts Development  Control Flow  Actions  Struts 2 Views and Target  Struts 2 Custom Tags  Validation 
Amsterdam What is new in Servlet 3.0 ApacheCon 2009.
CS-4220 Dr. Mark L. Hornick1 Single class doing too many things =Bad Code Smell.
CS3220 Web and Internet Programming Introduction to Java Servlets
Servlet Sessions and Cookies
Java Servlets By: Tejashri Udavant..
Course Outcomes of Advanced Java Programming AJP (17625, C603)
Java Servlets.
Sri Vatsav Konreddy CIS 764 FALL 2007
Pre-assessment Questions
Servlet.
Knowledge Byte In this section, you will learn about:
Servlet API and Lifecycle
30 Java Applets.
MSIS 655 Advanced Business Applications Programming
Server Side Programming: Java Servlets
ESIS Consulting LLC (C) ESIS Consulting LLC. All rights reserved
Objectives In this lesson you will learn about: Need for servlets
Java Servlets and JSP.
Knowledge Byte In this section, you will learn about:
CS3220 Web and Internet Programming Introduction to Java Servlets
J2EE Lecture 1:Servlet and JSP
Introduction to Java Servlets
Directories and DDs 25-Apr-19.
Directories and DDs 21-Jul-19.
Directories and DDs 14-Sep-19.
Presentation transcript:

JavaServer Pages (JSP) Lifecycle Listeners Primary Reference: Marty Hall, Larry Brown, and Yaakov Chaikin, Core Servlets and JavaServer Pages, Volume 2: Advanced Technologies (Second Edition), Prentice Hall, 2008. ©SoftMoore Consulting

JavaServer Pages (JSP) Motivation Respond to events in the overall application lifecycle. notice when certain attributes are removed from the servlet context Perform tasks more general than any one servlet or JSP page has responsibility for. insert application-wide data into the servlet context before any servlets or JSP pages are accessed keep track of certain session attributes, regardless of which servlet or JSP page added the attributes Lifecycle listeners respond to web application lifecycle events ©SoftMoore Consulting

Lifecycle Listeners A listener is a class that “listens” for and can respond to various events that can occur during the lifecycle of a web application. A listener must extend one of the “listener” interfaces in package javax.servlet or package javax.servlet.http. Listeners must be configured in the deployment descriptor for the web application. ©SoftMoore Consulting

Example Listener Interfaces ServletContextListener void contextInitialized(ServletContextEvent sce) void contextDestroyed(ServletContextEvent sce) ServletRequestListener void requestInitialized(ServletRequestEvent sre) void requestDestroyed(ServletRequestEvent sre) HttpSessionListener void sessionCreated(HttpSessionEvent se) void sessionDestroyed(HttpSessionEvent se) ©SoftMoore Consulting

Kinds of Lifecycle Listeners Servlet context listeners – notified when the servlet context (i.e., the web application) is initialized and destroyed. Servlet context attribute listeners – notified when attributes are added to, removed from, or replaced in the servlet context Session listeners – notified when session objects are created, invalidated, or timed out Session attribute listeners – notified when attributes are added to, removed from, or replaced in any session ©SoftMoore Consulting

Kinds of Lifecycle Listeners (continued) Session migration listeners – notified when session objects are serialized and deserialized by the container Session object binding listeners – notified when the implementing object is added to or removed from the session object Request listeners – notified when request objects are initialized or destroyed Request attribute listeners – notified when attributes are added to, removed from, or replaced in any request ©SoftMoore Consulting

Implementing a Lifecycle Listener Implement the appropriate interface – ServletContextListener – ServletContextAttributeListener – HttpSessionListener – HttpSessionAttributeListener – HttpSessionBindingListener − HttpSessionActivationListener – ServletRequestLIstener – ServletRequestAttributeListener Implement the methods needed to respond to the events of interest Provide empty bodies for the other methods in the interface. Obtain access to the important Web application objects – name of the servlet context attribute that changed – value of the servlet context attribute that changed – request /session object – name of the session attribute that changed ©SoftMoore Consulting

Implementing a Lifecycle Listener (continued) Use the objects. For example, use the servlet context to read initialization parameters, method getInitParameter() store data for later access, method setAttribute() read previously stored data, method getAttribute() Declare the listener. Add listener and listener-class elements to the application deployment descriptor (web.xml) or of a tag library descriptor file. Provide any needed initialization parameters. Add context-param elements to the application deployment descriptor (web.xml) to provide initialization parameters for servlet context listeners ©SoftMoore Consulting

Example: ContextListener package com.softmoore.servlet; import com.softmoore.process.DatabaseUpdateThread; import com.softmoore.util.Log; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class ContextListener implements ServletContextListener { private DatabaseUpdateThread updateThread; public ContextListener() super(); updateThread = new DatabaseUpdateThread(); } ©SoftMoore Consulting

Example: ContextListener (continued) @Override public void contextInitialized(ServletContextEvent event) { updateThread.start(); Log log = Log.getInstance(); log.logMessage("DatabaseUpdateThread started"); } public void contextDestroyed(ServletContextEvent event) updateThread.interrupt(); log.logMessage("DatabaseUpdateThread terminated"); ©SoftMoore Consulting

Declaring a Listener in the Deployment Descriptor <?xml version="1.0" encoding="ISO-8859-1"?> <web-app 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/..." version="2.5"> ... <listener> <listener-class> edu.citadel.cis.servlet.ContextListener </listener-class> </listener> </web-app> ©SoftMoore Consulting

Using Annotations to Declare Listeners Introduced in servlet API version 3.0, the annotation @WebListener annotation can be used to register a class as a listener of a web application. syntax: @WebListener(optional description) Example @WebListener public class ContextListener implements ServletContextListener { ... } Note that the class must still implement one of the listener interfaces. ©SoftMoore

Using Annotations to Declare/Map Filters (continued) Annotation @WebListener replaces the equivalent <listener> configuration element in the deployment descriptor file (web.xml) Servlet containers process the annotated classes when the application is deployed. Note: Several additional annotations were also introduced in servlet API 3.0; e.g., @WebServlet and @WebFilter. ©SoftMoore