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

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>

你可能感兴趣的:(html,Web,dos)