它在javax.servlet.http包内,只要能运行servlet的就可以啊

解决方案 »

  1.   

    我知道,但是我试过了,好像不行,没法监控session的断开,能否给出一个实例?
      

  2.   

    <listener>
        <listener-class>
         com.listeners.MyContextListener
        </listener-class>
      </listener>
     <listener>
        <listener-class>
          com.listeners.ServletContextAttribListener
        </listener-class>
      </listener>
     <listener>
        <listener-class>
          com.listeners.MySessionListener
        </listener-class>
      </listener>
    package com.listeners;import javax.servlet.ServletContext;
    import javax.servlet.ServletContextAttributeEvent;
    //import javax.servlet.ServletContextAttributesListener;
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;public final class MyContextListener
        implements ServletContextListener {  private ServletContext context = null;  public MyContextListener() {}  //This method is invoked when the Web Application
      //has been removed and is no longer able to accept
      //requests  public void contextDestroyed(ServletContextEvent event)
      {    //Output a simple message to the server's console
        System.out.println("The Simple Web App. Has Been Removed");
        this.context = null;  }
      //This method is invoked when the Web Application
      //is ready to service requests  public void contextInitialized(ServletContextEvent event)
      {
        this.context = event.getServletContext();    //Output a simple message to the server's console
        System.out.println("The Simple Web App. Is Ready");  }
    }
    package com.listeners;import javax.servlet.ServletContext;
    import javax.servlet.http.HttpSessionAttributeListener;
    import javax.servlet.http.HttpSessionBindingEvent;
    import javax.servlet.http.HttpSessionEvent;
    import javax.servlet.http.HttpSessionListener;public final class MySessionListener
      implements HttpSessionAttributeListener, HttpSessionListener {
      public void sessionCreated(HttpSessionEvent event) {    System.out.println("HttpSession object has been created");  }
      public void sessionDestroyed(HttpSessionEvent event) {    System.out.println("HttpSession object has been removed");  }  public void attributeAdded(HttpSessionBindingEvent event) {    System.out.println("An attribute has been added " +
          "to an HttpSession object");  }  public void attributeRemoved(HttpSessionBindingEvent event) {    System.out.println("An attribute has been removed " +
          "to an HttpSession object");  }
      public void attributeReplaced(HttpSessionBindingEvent event) {    System.out.println("An attribute has been replaced " +
          "to an HttpSession object");  }
    }package com.listeners;
    import javax.servlet.ServletContext;
    import javax.servlet.ServletContextAttributeEvent;
    import javax.servlet.ServletContextAttributeListener;
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    public class ServletContextAttribListener
        implements ServletContextAttributeListener {
      //This method is invoked when an attribute
      //is added to the ServletContext object
      public void attributeAdded (ServletContextAttributeEvent scab)
      {
        System.out.println("An attribute was added to the " +
          "ServletContext object");
      }  //This method is invoked when an attribute
      //is removed from the ServletContext object
      public void attributeRemoved (ServletContextAttributeEvent scab)
      {
        System.out.println("An attribute was removed from " +
          "the ServletContext object");
      }  //This method is invoked when an attribute
      //is replaced in the ServletContext object
      public void attributeReplaced (ServletContextAttributeEvent scab)
      {
        System.out.println("An attribute was replaced in the " +
          "ServletContext object");
      }}
      

  3.   

    只监听session,现在代码我都有,就是在session断开时删除数据库中的记录,但是不行,不知道是否是weblogic不支持,我再试一下。
      

  4.   

    看看这个
    http://www.csdn.net/expert/topic/934/934675.xml?temp=.3332636
      

  5.   

    session吹毁不是立即的
    强制一下看看session.invalidate()
      

  6.   

    我现在关键想了解weblogic 6.x 是否不支持Httpsessionlistener,如果是,我就要安装wls 7了。
      

  7.   

    现在有新问题了,我安装了wls7,但是创建的Listener Class还是无法生效。
      

  8.   

    怎么样算生效呢?
    浏览器关闭是不会导致session被destroy的,需要timeout或invalidate
      

  9.   

    timeoout时间已经过了,但是class中的操作并没有执行。
      

  10.   

    奇怪,你怎么使用那么多listener
    我有一个统计在线人数的程序,也是使用HttpSessionListener不知道是否可以作为参考(这个程序我的网站一直在用,没有问题):
    package com.demo;
    import javax.servlet.http.HttpSessionEvent;
    import javax.servlet.http.HttpSessionListener;public class SessionCounter
        implements HttpSessionListener
    {    private static int activeSessions = 0;    public SessionCounter()
        {
        }    public void sessionCreated(HttpSessionEvent se)
        {
            activeSessions++;
        }    public void sessionDestroyed(HttpSessionEvent se)
        {
            if(activeSessions > 0)
                activeSessions--;
        }    public static int getActiveSessions()
        {
            return activeSessions;
        }}
    在web.xml加入:
      <listener> 
        <listener-class>
         com.demo.SessionCounter
        </listener-class>
     </listener>
      

  11.   

    to gdsean(摇滚java)
    没有啊,我只用一个Listener,我没贴代码出来,你怎么知道我用了多少?
      

  12.   

    weblogic 6.1 有class SessionTimeoutNotifier implements HttpSessionBindingListener
    {
      SessionTimeoutNotifier(){
      }  public void valueBound(HttpSessionBindingEvent event){
        com.pagic.sas.common.ConfigObj.TOTAL++;
      }  public void valueUnbound(HttpSessionBindingEvent event){
        try{
          HttpSession session=event.getSession();
          String str_userid = "chenxh";
          str_userid=(String)(event.getName()).substring(0,(event.getName()).indexOf("::"));
          com.pagic.sas.common.ConfigObj.UserList.remove(str_userid);
          com.pagic.sas.common.ConfigObj.TOTAL--;
          System.out.println("\t"+str_userid+" Leave System!");
        }catch(Exception e){
        }
      }
    }