<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 閱讀(488) 評論(0)  編輯  收藏 所屬分類: javaZZ
    主站蜘蛛池模板: 久久精品国产亚洲一区二区| 亚洲 无码 在线 专区| 亚洲av不卡一区二区三区 | 亚洲国产视频久久| 亚洲三级高清免费| 亚洲人成黄网在线观看| 一二三四影视在线看片免费 | 免费国产作爱视频网站| 亚洲精品国产精品国自产网站| 成人浮力影院免费看| 亚洲av一本岛在线播放| 午夜免费福利在线观看| 国产青草亚洲香蕉精品久久| 日批日出水久久亚洲精品tv| 国产日韩AV免费无码一区二区三区| 亚洲线精品一区二区三区影音先锋| 日本高清不卡aⅴ免费网站| 亚洲成人激情在线| 四虎永久在线精品免费观看视频| 亚洲熟女乱色一区二区三区| 国产精品免费播放| 日本高清不卡aⅴ免费网站| 亚洲日韩在线视频| 国产免费资源高清小视频在线观看| 免费看一级高潮毛片| 亚洲激情视频在线观看| 久久久久国产精品免费免费搜索| 精品韩国亚洲av无码不卡区| 亚洲三区在线观看无套内射| 四虎永久在线观看免费网站网址 | 国产精品国产午夜免费福利看| 精品国产免费人成网站| 久久亚洲精品无码aⅴ大香| 在线免费观看中文字幕| 国产99久久久久久免费看| 久久精品国产亚洲av影院| 日韩a在线观看免费观看| 成全视频高清免费观看电视剧 | 特级毛片全部免费播放a一级| 亚洲av永久无码精品漫画| 免费观看理论片毛片|