Listener监听器(记录在线用户信息)

    xiaoxiao2021-12-10  12

    package com.xxx.listener; import java.sql.ResultSet; import javax.servlet.ServletRequestEvent; import javax.servlet.ServletRequestListener; import javax.servlet.annotation.WebListener; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import com.xxx.domain.Db; /** * 监听在线用户的访问请求 * 用户存在更新信息不存在就插入新在线用户 * @author Administrator * */ @WebListener public class OnlineRequestListener implements ServletRequestListener { //当用户请求到达,初始化时触发该方法 @Override public void requestInitialized(ServletRequestEvent sre) { HttpServletRequest request=(HttpServletRequest) sre.getServletRequest(); HttpSession session=request.getSession(); String sessionId=session.getId(); // 访问的ip String ip=request.getRemoteAddr(); //访问的页面 String page=request.getRequestURI(); //当前登录用户的session String user=(String) session.getAttribute("user"); //为登录用户当游客 user=(user==null)?"游客":user; try{ Db db=new Db("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/jdbc", "root", ""); ResultSet rs=db.query("select * from tb_online where session_id=?", sessionId); //如果用户存在 修改记录 if(rs.next()){ rs.updateString(5, page); rs.updateLong(5, System.currentTimeMillis()); rs.updateRow(); rs.close(); }else{ //插入用户在线信息 db.insert("insert into tb_online values(?,?,?,?,?)", sessionId,user,ip,page,System.currentTimeMillis()); } }catch(Exception e){ e.printStackTrace(); } } @Override public void requestDestroyed(ServletRequestEvent sre) { } }

    package com.xxx.listener; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.ResultSet; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; import javax.swing.Timer; import com.xxx.domain.Db; /** * 后台执行 *定期检查在线记录 */ @WebListener public class OnlineServletContextListener implements ServletContextListener { //超过10分钟没有访问本站即认为用户已经离线 public final int MAX_MILITIS=10*60*1000; @Override public void contextInitialized(ServletContextEvent arg0) { //每5秒检查一次 new Timer(1000*5,new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try{ Db db=new Db("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/jdbc", "root", ""); ResultSet rs=db.query("select * from tb_online"); StringBuffer beRemove=new StringBuffer("("); while(rs.next()){ if( (System.currentTimeMillis()-rs.getLong(5))>MAX_MILITIS ){ //将被删除的session id添加进来 beRemove.append("'"); beRemove.append(rs.getString(1)); beRemove.append("' ,"); } //有需要删除的记录 if(beRemove.length()>3){ beRemove.setLength(beRemove.length()-3); beRemove.append(")"); //删除所有超过指定时间没有重新请求的记录 db.update("delete from tb_onlone where session_id in"+beRemove.toString()); db.closeConn(); } } }catch(Exception e1){ e1.printStackTrace(); } } }); } @Override public void contextDestroyed(ServletContextEvent arg0) { } }

    <% Db db=new Db("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/jdbc", "root", ""); ResultSet rs=db.query("select * from tb_online"); while(rs.next()){ %> <tr> <td><%=rs.getString(1) %></td> <td><%=rs.getString(2) %></td> <td><%=rs.getString(3) %></td> <td><%=rs.getString(4) %></td> </tr> <%} %> </body> </html>

    转载请注明原文地址: https://ju.6miu.com/read-700136.html

    最新回复(0)