1.客户关闭浏览器一段时间后
2.客串户长时间不点页面中的链接
上述的两种情况都会导致和此用户关联的session失效,你可以用一个session监听就可以完成所说的功能了。比如下面的代码:
public class SessionListener implements HttpSessionListener {
static Logger log=Logger.getRootLogger();
public void sessionCreated(HttpSessionEvent event) {
//做一些事件
} public void sessionDestroyed(HttpSessionEvent event) {
SysUser user=(SysUser) event.getSession().getAttribute("user");
if(user!=null) {
CtxUtil.getService().getUserMsgManager().delSessionUser(user);
}
}
}相应的web.xml
<listener>
<listener-class>com.filter.CtxUtilListen</listener-class>
</listener>
<listener>
<listener-class>
com.service.listener.SessionListener
</listener-class>
</listener>
2.客串户长时间不点页面中的链接
上述的两种情况都会导致和此用户关联的session失效,你可以用一个session监听就可以完成所说的功能了。比如下面的代码:
public class SessionListener implements HttpSessionListener {
static Logger log=Logger.getRootLogger();
public void sessionCreated(HttpSessionEvent event) {
//做一些事件
} public void sessionDestroyed(HttpSessionEvent event) {
SysUser user=(SysUser) event.getSession().getAttribute("user");
if(user!=null) {
CtxUtil.getService().getUserMsgManager().delSessionUser(user);
}
}
}相应的web.xml
<listener>
<listener-class>com.filter.CtxUtilListen</listener-class>
</listener>
<listener>
<listener-class>
com.service.listener.SessionListener
</listener-class>
</listener>
解决方案 »
- 这段代码中的super()做什么用的?
- 两道华为JavaEE面试题,昨天面试了答不出来,求解。
- 一般 K线图 X轴显示的都是时间日期 怎么让X轴的每个坐标显示让我自定义
- Mina服务器端研发群,以积极讨论问题为主.
- 请问哪里能下到一些开源软件的源码
- 请各位高手帮忙,EJB 3.0有关EntityManager的merge方法
- ORACLE 如何向ORACLE数据库插入DATE型数据
- struts 问题
- 请大虾帮忙,有关jbuilder脚本的出错问题
- eclipse問題
- 用Spring JdbcTemplate 查询出的Map,是如何产生大小写忽略的Key的?
- 【POI相关】如何在Excel中写入百分数?
HttpSessionAttributeListener这个监听器能得到。不知道HttpSessionListener 是不是得到?能的话。请告诉我一下。谢谢。我想精确的知道在线用户的名字。所以我建一个List 来维护。
当用户进来的时候到是加入了。但用户关闭浏览器进的时候。List确不知道把它remove啊。
请问怎么样才能解决?
HttpSessionListener, ServletContextListener { ServletContext application = null;
List list= null;
public void attributeAdded(HttpSessionBindingEvent sbe) {
if("uname".equals(sbe.getName())){
String value = (String) sbe.getValue();
list = (List)application.getAttribute("allUser");
list.add(value);
} } public void attributeRemoved(HttpSessionBindingEvent sbe) {
if("uname".equals(sbe.getName())){
String value = (String) sbe.getValue();
list = (List)application.getAttribute("allUser");
list.remove(value);
} }
public void sessionDestroyed(HttpSessionEvent arg0) {
System.out.println("Session销毁。"); } public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub } public void contextInitialized(ServletContextEvent sce) {
application = sce.getServletContext();
application.setAttribute("allUser", new ArrayList()); }}以上是Listener.java代码你可以采用以下方式注销用户
第一:在JSP页面提供一个按钮“注销”<%session.removeAttribute("uname"); %>
就可以直接可以移除List里面的的名字。
第二:利用注销session方法 : session.invalidate();注销用户;
第三: 设置session时间:web.xml里配置:
<session-config>
<session-timeout>1</session-timeout>
</session-config>
//1分钟后注销这是我知道的方法,我也学J2EE2个月,一定有不足之处 , 在这里献丑了