计数器

计数器
用Java程序   
     现在许多网站都必须统计浏览量,在此为了记录当前在线人数,我设计了一个计数器。其功能是:计数器就将自动加一,离开时就自动减一。。
这里就做了个很小的 demo:
Java 代码如下:


package com.dr.demo2.servlet;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.apache.log4j.Logger;

public class SessionCounter implements HttpSessionListener,
  ServletRequestListener {
 private static Logger log = Logger.getLogger(SessionCounter.class);
 private static final String CONTENT_TYPE = "text/html; charset=GBK";
 private static int activeSessions = 0;// 当前活动的人数
 private HttpServletRequest request;
 private static ArrayList list = new ArrayList();// 用来存放不同ip的地址

 public void init() throws ServletException {
  log.info("SessionCounter init!");

 }

 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  log.info("SessionCounter doGet!");
  response.setContentType(CONTENT_TYPE);
  HttpSession session = request.getSession();
 }

 public void destroy() {
  log.info("SessionCounter destroy!");
 }

 public void requestDestroyed(ServletRequestEvent event) {
  // To change body of implemented methods use File | Settings | File
  // Templates.
  log.info("SessionCounter requestDestroyed!");
 }

 public void requestInitialized(ServletRequestEvent sre) {
  request = (HttpServletRequest) sre.getServletRequest();
  log.info("SessionCounter requestInitialized!");
 }

 public void sessionCreated(HttpSessionEvent httpSessionEvent) {
  log.info("SessionCounter sessionCreater!");
  String sessionId = httpSessionEvent.getSession().getId();
  Timestamp createTime = new Timestamp(System.currentTimeMillis());
  String loginIp = request.getRemoteAddr();
  boolean rs = true;
  if (list.size() > 0) {
   for (int i = 0; i < list.size(); i++) {
    if (loginIp.equals(list.get(i))) {
     rs = false;
    }
   }
  }
  if (rs) { // 如果队列中存在相同的IP 则SESSION不增加
   list.add(loginIp);
   log.info("ipList队列新增ip: " + loginIp);
   activeSessions++;
   log.info("新增SESSION,sessionId = " + sessionId + "; createTime = "
     + createTime + "; loginIp = " + loginIp + "; 当前总SESSION值为 "
     + activeSessions);
  }
 }

 public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
  log.info("SessionCounter sessionDestroyed!");
  String sessionId = httpSessionEvent.getSession().getId();
  Timestamp overTime = new Timestamp(System.currentTimeMillis());
  String loginIp = request.getRemoteAddr();
  if (activeSessions > 0) {
   if (list.size() > 0) {
    for (int i = 0; i < list.size(); i++) {
     if (loginIp.equals(list.get(i))) {
      list.remove(i);
      log.info("ipList队列移除ip: " + loginIp);
     }
    }
   }
   activeSessions--; // 在用户销毁的时候,从队列中踢出这个IP
   log.info("销毁SESSION,sessionId = " + sessionId + "; overTime = "
     + overTime + "; loginIp = " + loginIp + "; 当前总SESSION值为 "
     + activeSessions);
  }
 }

 public static int getActiveSessions() {
  log.info("SessionCounter getActiveSessions!");
  return activeSessions;
 }

 public void setActiveSessions(int i) {
  log.info("SessionCounter setActiveSessions!");
  activeSessions = i;
 }

}

jsp  部分代码


<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'online.jsp' starting page</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

  </head>
 
  <body>
    <%@   page   import= "com.dr.demo2.servlet.SessionCounter"   %>  
 在线: <%=  SessionCounter.getActiveSessions()   %>人 
  </body>
</html>

启动tomcat ,在浏览器中输入:http://127.0.0.1:8080/OnlineCount/online.jsp

执行效果如下:

 

你可能感兴趣的:(计数器)