这几天在做一个在线人数统计的程序,我费了好大劲在网上查了一些资料,经过调试学到了一些东西,在这写两个简单的程序介绍一下:
1.新建一个工程OnlineCount,包结构如下图所示:
demo1:新建一个OnlineCounter.java
代码如下:
package
com.dr.demo2.servlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import org.apache.log4j.Logger;
public class OnlineCounter extends HttpServlet implements HttpSessionListener
{
private static Logger log = Logger.getLogger(OnlineCounter.class);
private static final long serialVersionUID = 1L;
private static int sessionCounter = 0;
public OnlineCounter(){
log.info("OnlineCounter initialized.");
}
public void sessionCreated(HttpSessionEvent se) {
sessionCounter++;
log.info("session created:" + sessionCounter);
}
public void sessionDestroyed(HttpSessionEvent se) {
sessionCounter--;
log.info("session destroied");
}
public static int getOnlineSession() {
return sessionCounter;
}
}
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import org.apache.log4j.Logger;
public class OnlineCounter extends HttpServlet implements HttpSessionListener
{
private static Logger log = Logger.getLogger(OnlineCounter.class);
private static final long serialVersionUID = 1L;
private static int sessionCounter = 0;
public OnlineCounter(){
log.info("OnlineCounter initialized.");
}
public void sessionCreated(HttpSessionEvent se) {
sessionCounter++;
log.info("session created:" + sessionCounter);
}
public void sessionDestroyed(HttpSessionEvent se) {
sessionCounter--;
log.info("session destroied");
}
public static int getOnlineSession() {
return sessionCounter;
}
}
在web.xml中写入以下语句:
<
listener
>
< listener - class >
com.dr.demo2.servlet.OnlineCounter
listener - class >
listener >
< session - config >
< session - timeout > 1 session - timeout >
session - config >
< listener - class >
com.dr.demo2.servlet.OnlineCounter
listener - class >
listener >
< session - config >
< session - timeout > 1 session - timeout >
session - config >
新建一个online.jsp,在body中写入以下语句:
<%@ page import= "com.dr.demo2.servlet.OnlineCounter" %>
在线: <%= OnlineCounter.getOnlineSession() %>人
在线: <%= OnlineCounter.getOnlineSession() %>人
这样就可以启动服务器运行。
1.初始启动服务器运行时:控制台上会下面的日志信息:
2011-01-18 11:11:47,968 [main] [com.dr.demo2.servlet.OnlineCounter] [INFO] - OnlineCounter initialized.
2.在浏览器地址栏中中访问online.jsp:控制台上打出,
2011-01-18 11:37:16,031 [http-8080-1] [com.dr.demo2.servlet.OnlineCounter] [INFO] - session created:1
3.大约一分钟过后,控制台上会打出:
2011-01-18 11:39:10,265 [ContainerBackgroundProcessor[StandardEngine[Catalina]]] [com.dr.demo2.servlet.OnlineCounter] [INFO] - session destroied
这跟在web.xml中的时间有关系
在调试时你会发现,当一个电脑上装有不同的浏览器时,用另一个浏览器再次访问时,会打出:
2011-01-18 11:38:16,031 [http-8080-1] [com.dr.demo2.servlet.OnlineCounter] [INFO] - session created:2
为了避免上述失误产生,在程序中加入IP过滤:
demo2:新建一个SessionCounter.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;
}
}
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;
}
}
将xml中的代码改为如下代码:
<
listener
>
< listener - class > com.dr.demo2.servlet.SessionCounter listener - class >
listener >
< session - config >
< session - timeout > 1 session - timeout >
session - config >
< listener - class > com.dr.demo2.servlet.SessionCounter listener - class >
listener >
< session - config >
< session - timeout > 1 session - timeout >
session - config >
online.jsp代码改为如下代码:
<%@ page import= "com.dr.demo2.servlet.SessionCounter" %>
在线: <%= SessionCounter.getActiveSessions() %>人
在线: <%= SessionCounter.getActiveSessions() %>人
程序运行结果:
1.访问online.jsp时,控制台输出如下:
2.用不同的浏览器访问时,控制台输出如下:
3.用不同的电脑访问时,控制台输出如下:
4.大概一分钟过后,控制台上输出如下:
希望各位大虾批评指导!