当然可以,你可以用sessionlisterner

解决方案 »

  1.   

    这里有一个例子,有用记得加分。import javax.servlet.*; 
    import javax.servlet.http.*; 
    public class SessionCounter implements HttpSessionListener { private static int activeSessions = 0; 
    public void sessionCreated(HttpSessionEvent se) { 
      activeSessions++; 

    public void sessionDestroyed(HttpSessionEvent se) { 
      if(activeSessions > 0) activeSessions--; 

    public static int getActiveSessions() { 
      return activeSessions; 
    } } 然后需要在你的WEB-INF中建立web.xml 
    文件内容如下: <!-- 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> 
          SessionCount.SessionCounter 
        </listener-class>   
      </listener> 
    </web-app> 
      

  2.   

    我想要一个功能,就是在jsp上显示用户还有多长时间用户的session就会过期,有什么办法可以实现吗?
      

  3.   

    to Joeblackyang(野Heart) :
            我只需要用这个servlet监听就可以了吗?如果我要在session 断开的时候调用一个方法,就是在sessionDestroyed()方法里调用了,对不对?
    等你回复,谢谢
      

  4.   

    有个方法是设置session存活时间的,好好找找了
      

  5.   

    设置session的时间很简单,现在主要是session 超时的时候,我要写日志,不知道怎么做
      

  6.   

    HttpSession session = request.getSession(true);
    if (session.isNew()){}
      

  7.   

    binha:
           不知道你的这个用法什么意思?
      

  8.   

    HttpSession session = request.getSession(false);
    if (session == null){
      写日志
    }
      

  9.   

    我写了一个监听的servlet,但是,当一个session超时的时候没有监听到,不知道为什么?
    package examples.servlets;import javax.servlet.*; 
    import javax.servlet.http.*; 
    import java.util.*;
    import java.io.*;
    public class SessionCounter extends HttpServlet implements HttpSessionListener {
     
    private static int activeSessions = 0; 

    HttpSession sess = null;
       ObjectOutputStream out = null;
       ObjectInputStream in = null;   public static Hashtable h = new Hashtable(100);


    public void sessionCreated(HttpSessionEvent evt) { 
       //activeSessions++;
       sess = evt.getSession();
    log("\nSession created: Id: " + sess.getId() + " CreationTime: " + sess.getCreationTime() );
    synchronized(this) {
      h.put(sess.getId(), sess);
    }
    log("Number of active sessions: " + h.size()); 


    public void sessionDestroyed(HttpSessionEvent evt) { 

       sess = evt.getSession();
        log("\nsession destroyed: " + sess.getId() );
    synchronized(this) {
              h.remove(sess.getId());
        } 
        log("Number of active sessions: " + h.size());
         


    public static int getActiveSessions() { 
       return activeSessions; 


    public void log(String s){
         System.out.println("[SessionListener] " + s);
       }


      

  10.   

    当我网页之间传递的session,通过HttpSessionListener 是否可以监听到?请高人指教
      

  11.   

    session和线程有关,可以记录线程ID
      

  12.   

    Joeblackyang(野Heart) 的例子已经回答了你的问题。
    别忘了配置web.xml
    还有HttpSessionListener接口没必要实现在servlet上。
    ------------------------------------------------------------------
    当我网页之间传递的session,通过HttpSessionListener 是否可以监听到?这个问题你问得不太明白。除了HttpSessionListener外,还有HttpSessionAttributeListener和HttpSessionActivationListener接口
    应该能够满足你需求。
      

  13.   

    to xs21cn:
    我都配置了,在这里我继承servlet是想从客户端取的参数,然后写日志。
    当监听的时候,有没有办法取到客户端的参数?