- 爱易网页
-
Java教程
- JSP在线人员列表兑现
日期:2014-05-17 浏览次数:20808 次
JSP在线人员列表实现
使用HttpSessionBindingListener接口
要求显示登陆人的IP,登陆时间,所在页面等信息
具体实现如下:
Struts-config.xml
<action-mappings>
<!-- huangyaocan permission change -->
<action attribute="loginForm2" path="/loginAction2" input="/index.jsp" parameter="method" type="com.gsta.umcc.console.login.action.LoginAction" name="loginForm2" scope="request" validate="false">
<forward name="success" path="/login.jsp"/>
<forward name="failure" path="/index.jsp"/>
<forward name="logout" path="/index.jsp"/>
<forward name="logoutClose" path="/logout.jsp"/>
</action>
<action-mappings>
web.xml
<listener-class>com.gsta.umcc.console.login.OnlineUserSessionListener
</listener-class>
OnlineUserSessionListener.java
public void valueBound(HttpSessionBindingEvent event) {
// TODO Auto-generated method stub
HttpSession session = event.getSession ();
ServletContext ctx = session.getServletContext ();
Map map = (Map)ctx.getAttribute ("userName");
if (map == null) { // 如果是服务器启动后的第一个登录用户,新建一个容器
map = new HashMap();
ctx.setAttribute ("userName", map);
}
map.put(userName, this); // 把自己加入容器中
}
/*
* (non-Javadoc)
*
* @see javax.servlet.http.HttpSessionListener#sessionDestroyed(javax.servlet.http.HttpSessionEvent)
*/
public void valueUnbound(HttpSessionBindingEvent event) {
// TODO Auto-generated method stub
System.out.println("valueUnbound:"+this.userSessionID);
AuthorizationMap.removeUserAuthorizationException(this.userSessionID);
HttpSession session = event.getSession ();
ServletContext ctx = session.getServletContext ();
Map map = (Map)ctx.getAttribute ("userName");
map.remove (this); // 把自己从容器中删除
}
public class OnlineUser {
private ScmLogin scmLogin = null ;
private static List list=new ArrayList(); //用来存放在线用户列表
/**
* Returns the scmLogin.
* @return ScmLogin
*/
public ScmLogin getScmLogin() {
return scmLogin;
}
/**
* Sets the scmLogin.
* @param scmLogin The scmLogin to set
*/
public void setScmLogin(ScmLogin scmLogin) {
this.scmLogin = scmLogin;
}
public void updateOnline(){
list.add(this.scmLogin);
}
public void updateUnline(){
list.remove(this.scmLogin);
}
public static List getList(){
return list;
}
}
将在线用户信息放在一个static的list中。
此处还用到一个用户的基类ScmLogin,
public class ScmLogin {
private String UserID;
private String RealName;
private Integer OrgID;
private String OrgName;
private String OrgCode; //add by starrain.tian 2003.12.17
private java.sql.Timestamp loginTime; //add by starrain.tian 2004.11.10
private String ip;
.....
setXX,getXX.....
}
求JSP和action的写法,如果能Q我教我就最好了..谢谢先
------解决方案--------------------