<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    jinfeng_wang

    G-G-S,D-D-U!

    BlogJava 首頁 新隨筆 聯系 聚合 管理
      400 Posts :: 0 Stories :: 296 Comments :: 0 Trackbacks
    http://www.stardeveloper.com/articles/display.html?article=2001111901&page=1


    Overview :
    Web application events are new in Servlet 2.3 specification. They give you greater degree of control over your web application. In this article we will study two important application events :
    • Application startup and shutdown
    • Session creation and invalidation

    As their names suggest, application startup event occurs when your web application is first loaded and started by the Servlet container and application shutdown event occurs when the web application is shutdown.

    Session creation event occurs everytime a new session is created on the server and similarly session invalidation event occurs everytime a session is invalidated. To make use of these web application events and to do something useful you'll have to create and make use of special "listener" classes. From here onwards, we'll look at what these listener classes are how you can use them.

    Listener Classes :
    These are simple Java classes which implement one of the two following interfaces :

    • javax.servlet.ServletContextListener
    • javax.servlet.http.HttpSessionListener

    If you want your class to listen for application startup and shutdown events then implement ServletContextListener interface. If you want your class to listen for session creation and invalidation events then implement HttpSessionListener interface.

    Let's see what are the different methods of these interfaces which you'll have to implement.

    ServletContextListener :
    This interface contains two methods :

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

    Once you implement any interface you have to implement all of it's methods. So you if you want to make use of application startup and shutdown events, then create a Java class and implement ServletContextListener interface. An example of such a class is as follows :

    /*
    File : ApplicationWatch.java
    */

    import javax.servlet.ServletContextListener;
    import javax.servlet.ServletContextEvent;

    public class ApplicationWatch implements ServletContextListener {

    public static long applicationInitialized = 0L;

    /* Application Startup Event */
    public void contextInitialized(ServletContextEvent ce) {
    applicationInitialized = System.currentTimeMillis();
    }

    /* Application Shutdown Event */
    public void contextDestroyed(ServletContextEvent ce) {}
    }

    In the code above, a Java class; ApplicationWatch, implements ServletContextListener interface. It implement both of it's methods but it really uses only one of them and the second method's body remains empty. This class notes down the time of application startup in a public static variable which can be called from other application classes to know what was the last time this application was started.

    I'll explain how to tell the application server that we have this listener class and we want to be told of these application events in a moment, but first let's see what are the different methods of HttpSessionListener interface.

    HttpSessionListener :
    This interface also contains just two methods, for session creation and invalidation events respectively :

    • public void sessionCreated(HttpSessionEvent se);
    • public void sessionDestroyed(HttpSessionEvent se);

    Like what we did in the case of ApplicationWatch above, we'll have to create a Java class and implement HttpSessionListener interface. An example of such a class is as follows :

    /*
    File : SessionCounter.java
    */

    import javax.servlet.http.HttpSessionListener;
    import javax.servlet.http.HttpSessionEvent;

    public class SessionCounter implements HttpSessionListener {

    private static int activeSessions = 0;

    /* Session Creation Event */
    public void sessionCreated(HttpSessionEvent se) {
    activeSessions++;
    }

    /* Session Invalidation Event */
    public void sessionDestroyed(HttpSessionEvent se) {
    if(activeSessions > 0)
    activeSessions--;
    }

    public static int getActiveSessions() {
    return activeSessions;
    }
    }

    In the code above, SessionCounter class implements HttpSessionListener to count the number of active sessions. We will learn more about counting active users in a separate article in more detail.

    Ok, we have learned what are web application events, what interfaces are available to us and have also seen examples of classes implementing those interfaces. Let's see how to tell the application server about these listener classes.

    Web.xml :
    We do that by putting classpath of these classes in /WEB-INF/web.xml file under special <listener> tags. An example of such a web.xml file is given below :

    <!-- Web.xml -->
    <?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/j2ee/dtds/web-app_2.3.dtd">

    <web-app>

    <!-- Listeners -->
    <listener>
    <listener-class>
    com.stardeveloper.web.listener.SessionCounter
    </listener-class>
    </listener>
    <listener>
    <listener-class>
    com.stardeveloper.web.listener.ApplicationWatch
    </listener-class>
    </listener>

    </web-app>

    As shown above it is quite easy to declare listener classes in web.xml files. Now every time the server starts or shutdown, a session is created or destroyed, your classes will be told as their specific event methods will be called. It is that simple!

    Summary :
    In this article we learned what are web application events and how we can make use of these events by creating special "listener" classes. We then created two classes which implemented ServletContextListener and HttpSessionListener interfaces respectively. We also learned how to declare these listener classes in web.xml file by using special <listener> and <listener-class> tags.



    posted on 2008-03-18 17:02 jinfeng_wang 閱讀(491) 評論(0)  編輯  收藏 所屬分類: javaZZ
    主站蜘蛛池模板: 久久精品成人免费国产片小草 | 国产亚洲老熟女视频| 国产亚洲精品a在线观看| 国产亚洲综合久久系列| 亚洲国产日韩一区高清在线| 三年片在线观看免费西瓜视频 | 大妹子影视剧在线观看全集免费| 久久久久国产精品免费免费不卡 | 人妻无码久久一区二区三区免费| 成人黄色免费网站| 国产免费啪嗒啪嗒视频看看| 亚洲人成网亚洲欧洲无码久久 | 亚洲国产一区二区a毛片| 亚洲国产精品综合久久20| 国产天堂亚洲国产碰碰| 免费的全黄一级录像带| 精品熟女少妇AV免费观看| 亚洲人成影院在线观看| 337p欧洲亚洲大胆艺术| 国产成人综合久久精品亚洲| 毛片在线播放免费观看| 毛片a级毛片免费观看品善网| 亚洲一区二区三区在线播放| 精品亚洲国产成AV人片传媒| 色窝窝亚洲av网| 99视频在线免费看| 亚洲成av人片一区二区三区| 在线免费观看亚洲| 日本永久免费a∨在线视频| 2021国内精品久久久久精免费| 亚洲国产成人精品女人久久久 | 久久综合亚洲色一区二区三区 | 一级做a爰片久久免费| 91九色视频无限观看免费| 国产四虎免费精品视频| 亚洲国产精品成人AV无码久久综合影院| 久久综合亚洲色一区二区三区| 日本中文字幕免费看| 毛片免费在线观看网站| 精品久久久久久亚洲| 亚洲av第一网站久章草|