<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 首頁 新隨筆 聯(lián)系 聚合 管理
      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
    主站蜘蛛池模板: 特级毛片全部免费播放a一级| 亚洲综合激情六月婷婷在线观看| 伊人久久亚洲综合影院首页| 最近中文字幕mv免费高清在线| 精品亚洲永久免费精品| 国产日韩久久免费影院 | 蜜桃传媒一区二区亚洲AV| 最近2019中文免费字幕| 国产精品亚洲精品观看不卡| 啦啦啦中文在线观看电视剧免费版| 国产成人精品日本亚洲直接| 国产精品深夜福利免费观看| 美景之屋4在线未删减免费| 亚洲国产成人爱av在线播放| 国产V片在线播放免费无码| 亚洲成AV人在线观看天堂无码| 性色午夜视频免费男人的天堂| 亚洲精品国产专区91在线| 在线观看免费大黄网站| 国产AV日韩A∨亚洲AV电影| 亚洲日韩VA无码中文字幕| 日韩电影免费在线观看网站| 亚洲美女一区二区三区| 最近最新的免费中文字幕| 国产午夜亚洲精品不卡| 亚洲午夜久久久久妓女影院 | 一区视频免费观看| 亚洲成AV人在线播放无码| 在线看片无码永久免费视频| WWW亚洲色大成网络.COM| 最新精品亚洲成a人在线观看| 无码A级毛片免费视频内谢| 456亚洲人成影院在线观| 亚洲av无码天堂一区二区三区| AAA日本高清在线播放免费观看| 亚洲国产亚洲片在线观看播放| 国产一区二区免费在线| 国产一区二区三区免费| 国产午夜亚洲精品| 亚洲精品字幕在线观看| 毛片免费视频观看|