注册的在线人数/统计在线人数/网站总访问量

32 篇文章 0 订阅
8 篇文章 0 订阅
java LoginAction:
request.getSession().setAttribute(Constant.USER_KEY, user);

java UserSessionListener:

public class UserSessionListener implements HttpSessionListener,HttpSessionAttributeListener {

	 private static int activeSessions = 0;//当前在线人数
	 private static Map online=new HashMap();//当前注册的在线用户
	 private static int accessCount = 0; //网站总访问量
	 public static Map getOnline() { 
		 return online; 
	 }
	 public static int getOnlineSize() {//当前注册的在线人数
		 return online.size(); 
	 } 

	public void sessionCreated(HttpSessionEvent event) {
		// TODO Auto-generated method stub
//		System.out.println("[监听到]用户"+event.getSession().getId()+"上线!");
		activeSessions++;
		//统计总访问量
		System.out.println(event.getSession().getServletContext().getRealPath("/count/"));
		 File file = new File(event.getSession().getServletContext().getRealPath("/count/"), "count.txt");
		 FileInputStream in = null;
		 FileOutputStream out = null;
		 try {
			 if(file.exists()){
				 	in=new FileInputStream(file);
				    DataInputStream dis=new DataInputStream(in);
				    accessCount=dis.readInt();
				    dis.close();
				    in.close();
				    accessCount++;
				 	out = new FileOutputStream(file);
				    DataOutputStream dos = new DataOutputStream(out);
				    dos.writeInt(accessCount);
				    dos.flush();
				    dos.close();
				    out.close();
				    System.out.println("accessCount1="+accessCount);
			 }else{
					file.createNewFile();
					out = new FileOutputStream(file);
				     DataOutputStream dos = new DataOutputStream(out);
				     dos.writeInt(0);
				     dos.flush();
				     out.close();
				     dos.close();
				     System.out.println("accessCount0="+accessCount);
			 }
		 } catch (IOException e) {
			 e.printStackTrace();
		 }
	}
	public static int getAccessCount(){//网站总访问量
		return accessCount;
	}


	public void sessionDestroyed(HttpSessionEvent event) {
//		System.out.println("[监听到]用户"+event.getSession().getId()+"下线!");
		if(activeSessions>0){
			activeSessions--;
		}
	}
	public static int getActiveSessions() {//当前在线人数
		return activeSessions; 
	}

	public void attributeAdded(HttpSessionBindingEvent event) {
//		System.out.println("[监听到]Added:event.getName()="+event.getName());
//		System.out.println("[监听到]用户"+event.getSession().getId()+"上线!");
		if (event.getName().equals(Constant.USER_KEY)) { 
//			System.out.println("User logged in."); 
			User users = (User) event.getSession().getAttribute(Constant.USER_KEY);
			online.put(event.getSession().getId(),users); 
		} 
	}

	public void attributeRemoved(HttpSessionBindingEvent event) {
//		System.out.println("[监听到]Removed:event.getName()="+event.getName());
//		System.out.println("[监听到]用户"+event.getSession().getId()+"下线!");
		if (event.getName().equals(Constant.USER_KEY)) { 
//			System.out.println("User logged out."); 
			online.remove(event.getSession().getId()); 
		} 

	}

	public void attributeReplaced(HttpSessionBindingEvent event) {
	}
}



html:

<%@ page import="com.worthtech.eshop.web.listener.UserSessionListener" %> 


<tr>
				<td>会员/在线人数:</td>	
				<td><%= UserSessionListener.getOnlineSize() %> / <%= UserSessionListener.getActiveSessions() %></td>
				<td>本月新增会员:</td>
				<td><%= (String)request.getSession().getAttribute("loginedMonth") %></td>
			</tr>
			<tr>
				<td>网站流量:</td>
				<td><%= UserSessionListener.getAccessCount() %> </td>
				<td>注册人数:</td>	
				<td><%= (String)request.getSession().getAttribute("loginedUser") %></td>
			</tr>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值