Java
  Home arrow Java arrow Page 8 - Overview of Java Web Technologies, Part 1
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Overview of Java Web Technologies, Part 1
By: McGraw-Hill/Osborne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 24
    2004-03-01


    Table of Contents:
  • Overview of Java Web Technologies, Part 1
  • Methods
  • Creating a Servlet Directory Structure
  • Reviewing Deployment Descriptors
  • Servlet Mapping
  • Defining Context Parameters
  • Retrieving Context Parameters
  • Listening to Application Events
  • Packaging and Deploying a Web Application

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Overview of Java Web Technologies, Part 1 - Listening to Application Events
    ( Page 8 of 9 )

    The Servlet 2.3 and 2.4 specifications allow you to write listener classes for servlet contexts, session objects, and request objects (only in Servlet 2.4). In this section, we’re interested in the listener classes for a servlet context.

    The javax.servlet package provides two listener interfaces that support event notifications for state changes in the ServletContext object: the ServletContext Listener interface and the ServletContextAttributesListener interface. We’ll examine the ServletContextListener interface here, because it’s directly
    related to writing JSF applications.

    You use the ServletContextListener interface to listen to the ServletContext lifecycle events. ServletContextListener provides two methods:

    • The contextInitialized method is called when the Web application is ready to service requests. The method is called automatically by the servlet container when its own initialization process is finished. You can write code that needs to be executed when the application initializes, such as loading a JDBC driver, creating a database Connection object, or assigning initialization values to global variables.

    • The contextDestroyed method is invoked when the servlet context is about to be shut down. You can use this method to write code that needs to run when the application shuts down, such as closing a database connection or writing to the log.

    The signatures of contextInitialized and contextDestroyed are as follows:

    public void contextInitialized(ServletContextEvent sce)
    public void contextDestroyed(ServletContextEvent sce)

    As an example, the code in Listing 4 is a listener class called ApplicationListener. It listens to the lifecycle events of the ServletContext. It simply prints the string “Application initialized” when the ServletContext is initialized and “Application destroyed” when the ServletContext is destroyed.

    Listing 4 The ApplicationListener Class

    import javax.servlet.ServletContextListener;
    import javax.servlet.ServletContextEvent;
    public class ApplicationListener implements ServletContextListener {
      public void contextInitialized(ServletContextEvent cse) {
        System.out.println("Application initialized");
      }
      public void contextDestroyed(ServletContextEvent cse) {
        System.out.println("Application shut down");
      }
    }

    For the ApplicationListener class to work, you must register it in the deployment descriptor, such as the one in Listing 5.

    Listing 5 The Deployment Descriptor for the ApplicationListener Class

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">
     <web-app>
      <listener>
       <listener-class>ApplicationListener</listener-class>
      </listener>
    </web-app>

    Now, every time your application starts (the first time a servlet or JSP page is invoked), you will see the string “Application initialized” at the console. And before your application is taken out of service, it will display “Application shut down.”

    Buy this book now!Remember: This is part one of the first chapter of JavaServer Faces Programming, by Budi Kurniawan (McGraw-Hill/Osborne, ISBN 0-07-222983). Stay tuned for part 2 of "Overviews of Java Web Technologies," where we learn about JSP, JavaBeans, and Model 2. 
    Buy this book!



     
     
    >>> More Java Articles          >>> More By McGraw-Hill/Osborne
     

       

    JAVA ARTICLES

    - Exception Handling Techniques in Java
    - More About Multithreading in Java
    - The Basics of Multiple Threads in Java
    - Data Access Using Spring Framework JDBC
    - New Object Initialization in Java
    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT