采用了jass的方法以后(代码如下),现在我如果打开首页,会显示a user coming!
可是当我关闭首页以后,并没有destroy Session啊!这是为什么呢?请高手解答!如何才能比较准确的一点把关闭IE或者通过其他方式已经离开网站的用户从列表种清除呢??
/**监听器,监听用户的在线信息*/
public class UserSessionListener
implements HttpSessionListener {
public UserSessionListener() {
}
public static int onlineUserCount = 0;
public static ArrayList onlineUsersList = new ArrayList();
/* (non-Javadoc)
* @see javax.servlet.http.HttpSessionListener#sessionCreated(javax.servlet.http.HttpSessionEvent)
*/
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
onlineUserCount++;
System.out.println(onlineUserCount + " A users coming!");
}
/* (non-Javadoc)
* @see javax.servlet.http.HttpSessionListener#sessionDestroyed(javax.servlet.http.HttpSessionEvent)
*/
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
// TODO Auto-generated method stub
onlineUserCount--;
SysUser sysUser = (SysUser) httpSessionEvent.getSession().getAttribute(
"com.hhsoft.beans.elearning.model.user.SysUser");
if (sysUser != null){
int i = onlineUsersList.indexOf(sysUser.getUserId());
onlineUsersList.remove(i);
System.out.println("Session Destroyed!Session ID:" + sysUser.getUserId());
}
System.out.println("Session Destroyed!A user left!");
}
}
好像各个Element的顺序是有要求的,
你最好是按照icon?,display-name?,description?,distributable?,context-param*,filter*,filter-
mapping*,listener*,servlet*,servlet-mapping*,session-config?,mime-mapping*,welco
me-file-list?,error-page*,taglib*,resource-env-ref*,resource-ref*,security-const
raint*,login-config?,security-role*,env-entry*,ejb-ref*,ejb-local-ref*
的顺序,listener好像就必须放在filter-mapping的后面,servlet的前面,
你把session-config放到servlet-mapping后面试试。
下面是我的一个config,就没有问题:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<filter>
<filter-name>servlet2</filter-name>
<filter-class>servletexam.Servlet2</filter-class>
</filter>
<filter>
<filter-name>servlet3</filter-name>
<filter-class>servletexam.Servlet3</filter-class>
</filter>
<filter-mapping>
<filter-name>servlet2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>servlet3</filter-name>
<servlet-name>servlet1</servlet-name>
</filter-mapping>
<listener>
<listener-class>servletexam.MySessionListener</listener-class>
</listener>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>servlet1</servlet-name>
<servlet-class>servletexam.Servlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>servlet1</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>1</session-timeout>
</session-config>
</web-app>