Sample Program: Writing Listeners for the BookShoppingServlet

Sample Program: Writing Listeners for the BookShoppingServlet

Now you will create an example listener for your BookShoppingServlet. You will be writing listeners for both the events of this servlet: the ServletContext event and the HttpSession event. The methods that you implement in the listeners will not have any functionality; you will simply print messages out on the console through them.

Look first at the source code in Listing 4.4 for the ServletContext listener first.

Listing 4.4 DemonstrateContextListener.java
 /******************************************************************************   * Class Name:DemonstrateContextListener   * Implements:ServletContextListener   * Description:Servlet to demonstrate Servlet Context Listener functionality   *             This is invoked when the container starts   * @author Mandar S. Chitnis, Lakshmi AM.       @version 1.1   * Copyright (c) by Sams Publishing. All Rights Reserved. *******************************************************************************/ package com.sams.learnweblogic7.servlets; //importing the servlet packages import javax.servlet.*; import javax.servlet.http.*; public class DemonstrateContextListener implements ServletContextListener{ //Constructor for this class   public DemonstrateContextListener() {     System.out.println("ServletContextListener class called ");   } //This method is called when the container stops   public void contextDestroyed(ServletContextEvent evt){     System.out.println("The servlet context has been destroyed ");   } //This method is called when the servlet context is initialized   public void contextInitialized(ServletContextEvent evt){     System.out.println("The servlet context "             +(evt.getServletContext()).getServletContextName()             +"has been initialized");   } } 

The code for the listener for ServletContext events is very simple. The Demonstrate ContextListener class implements the ServletContextListener interface and contains the functionality for listening to context-created and context-destroyed events.

In the example, a message is displayed on the WebLogic Server console when a ServletContext is initialized:

 public void contextInitialized(ServletContextEvent evt){      System.out.println("The servlet context "             +(evt.getServletContext()).getServletContextName()             +"has been initialized");   } 

In the example, a message is displayed on the WebLogic Server console when a ServletContext is destroyed:

 public void contextDestroyed(ServletContextEvent evt){      System.out.println("The servlet context has been destroyed ");   } 

You will also write a simple listener for HttpSession events; see Listing 4.5.

Listing 4.5 DemonstrateHttpListener.java
 /******************************************************************************   * Class Name:DemonstrateHttpListener   * Implements:HttpListener   * Description:Listener to demonstrate Http Listener functionality   *             This is invoked when a message is sent on the Http server   * @author Mandar S. Chitnis, Lakshmi AM.       @version 1.1   * Copyright (c) by Sams Publishing. All Rights Reserved. *******************************************************************************/ package com.sams.learnweblogic7.servlets; import javax.servlet.*; import javax.servlet.http.*; public class DemonstrateHttpListener implements HttpSessionListener {     //defining global variables     HttpSession sess = null;     //constructor of the class     public DemonstrateHttpListener(){         System.out.println("Http Listener listener class constructor called");     }     //This method is called to notify when a session is created     public void sessionCreated(HttpSessionEvent evt){         sess = evt.getSession();         System.out.println("\nSession created: Id: " + sess.getId()                 + " CreationTime: " + sess.getCreationTime() );     }     //This method is called to notify when a session is destroyed     public void sessionDestroyed(HttpSessionEvent evt){         sess = evt.getSession();         System.out.println("\nsession destroyed: " + sess.getId() );     } } 

The DemonstrateHttpListener class implements the methods of the HttpSessionListener class. This enables it to listen for the HttpSession life-cycle events: creating an HttpSession and destroying an HttpSession. The specific method that intercepts the session-creating event is given here. In the sessionCreated() method, you display the session ID and the creation time on the WebLogic Server's console:

 public void sessionCreated(HttpSessionEvent evt){      sess = evt.getSession();     System.out.println("\nSession created: Id: " + sess.getId()             + " CreationTime: " + sess.getCreationTime() ); } 

Similarly, when an HttpSession is destroyed, the sessionDestroyed() method is invoked on this listener. The sessionDestroyed() method prints the session ID of the destroyed session on the WebLogic Server's console:

 public void sessionDestroyed(HttpSessionEvent evt){      sess = evt.getSession();     System.out.println("\nsession destroyed: " + sess.getId() ); } 

Compile the Two Listener Programs

Use the compile.bat batch file provided to compile the two listeners at the same time. The batch file compiles the DemonstrateContextListener.java and the DemonstrateHttpListener.java files located in the following directory in your domain:

 applications\ShoppingApp\WEB-INF\classes\com\sams\learnweblogic7\servlets\  

To verify that the compilation was successful, check that the corresponding .class files for DemonstrateContextListener.java and DemonstrateHttpListener.java were created in the directory

 c:\bea\user_domains\mydomain\applications\ShoppingApp\WEB-INF\classes\         com\sams\learnweblogic7\servlets\  

The directory should be similar to the screen shot in Figure 4.6.

Figure 4.6. Directory structure after compiling the listeners.

graphics/04fig06.jpg

Deploy the Program

After a successful compilation, the listeners need to be deployed in the WebLogic Server environment. Since the Web application archive for the servlets is already in place, you will not create a new Web archive file. The deployment activities that need to be carried out are described in the following sections.

Updating the .war Web Archive File

Listeners, like filters, are support applications for servlets. Hence, to deploy Web applications in WebLogic, the listener classes need to be packaged in a .war file. You will be adding your listeners to your existing Web archive file of the book-shopping application. To update the .war file, make sure you are in the root directory of your Web application at a DOS prompt:

 c:\bea\user_domains\mydomain\applications\ShoppingApp\WEB-INF\  

Then type the following command at the DOS prompt:

 jar  uv0f ShoppingApp.war .  
Registering Your Listener in web.xml

Once the .war file containing your listener class is ready, you need to register your listener class with the WebLogic Server. To register your listener, edit the web.xml in your deployment directory, in this case the directory

 c:\bea\user_domains\mydomain\applications\ShoppingApp\WEB-INF\  

Add the following tags to the web.xml file:

 <listener>      <listener-class>              com.sams.learnweblogic7.servlets.DemonstrateContextListener     </listener-class> </listener> <listener>     <listener-class>              com.sams.learnweblogic7.servlets.DemonstrateHttpListener     </listener-class> </listener> 

Notice that this process is similar to the registration of a servlet or a filter in this deployment descriptor file. The <listener></listener> tags encapsulate the registration information for your listener. The actual class filename qualified with the package name should be listed in the <listener-class></listener-class> tags. Your deployment descriptor file web.xml should look similar to the screen shot in Figure 4.7.

Figure 4.7. Registering the listener in the web.xml file.

graphics/04fig07.jpg

Listeners listen for events in a Web application. Hence, they must be defined after the <filter></filter> tags but before the <servlet></servlet> tags.

In the example, when the WebLogic Server invokes the BookShoppingServlet or the CheckOutServlet, your listeners will be in action. From the time the servlets are initialized until they are removed from the Web container of the WebLogic Server, the listeners will monitor these events. Your DemonstrateHttpListener listener will listen for HttpSession events.

Tip

Listeners for a Web application must be defined before the servlet definition in the deployment descriptor file web.xml.




Sams Teach Yourself BEA WebLogic Server 7. 0 in 21 Days
Sams Teach Yourself BEA WebLogic Server 7.0 in 21 Days
ISBN: 0672324334
EAN: 2147483647
Year: 2002
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net