快捷搜索:  as  test  1111  test aNd 8=8  test++aNd+8=8  as++aNd+8=8  as aNd 8=8

新蒲京澳门赌场网站:如何统计JAVA网站访问次数并获得访问者IP



简言之,假如不用第三方供给的接口

那最好的办理规划应该是当SESSION树马上sessionCreated(),在造访总数和当前在耳目数上+1

当SESSION销毁时sessionDistroyed(),在耳目数-1

核心措施是使用Listener监听的各类接口

我把代码(主要部分)贴鄙人面

此段代码我亲身应用过,没有问题

但由于承袭了接口,有些未应用的措施也要弥补进去,详细要领比如在MyEclipse中可以靠点差错,ADD UNIMPLEMENTED METHOD实现

package com.eaie.system;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.PrintWriter;

import java.net.MalformedURLException;

import java.net.URL;

import java.util.Enumeration;

import java.util.Set;

import javax.servl新蒲京澳门赌场网站et.RequestDispatcher;

import javax.servlet.Servlet;

import javax.servlet.ServletContext;

import javax.servlet.ServletContextEvent;

i新蒲京澳门赌场网站mport javax.servlet.ServletException;

import javax.servlet.ServletRequestEvent;

import javax.servlet.ServletRequestListener;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpSessionActivationListener;

import javax.servlet.http.HttpSessionEvent;

import javax.servlet.http.HttpSessionListener;

public class SessionListener implements HttpSessionActiv新蒲京澳门赌场网站ationListener,

HttpSessionListener, ServletContext, ServletRequestListener {

ServletContext context;

int onLineUserNum = 0;

int visitNum = 0;

HttpServletRequest request;

public void sessionDidActivate(HttpSessionEvent session) {

log("seesionDidActive("+session.getSession().getId()+")");

}

public void sessionWillPassivate(HttpSessionEvent session) {

log("seesionWillPassivate("+sessio新蒲京澳门赌场网站n.getSession().getId()+")");

}

public void sessionCreated(HttpSessionEvent event) {

onLineUserNum++;

visitNum ++;

String ip = request.getRemoteAddr();

log("sessionCreated('"+event.getSession().getId()+"'),-->IP:"+ip);

context.setAttribute("onLineUserNum", new Integer(onLineUserNum));

context.setAttribute("visitNum", new Integer(visitNum));

}

public void sessionDestroyed(HttpSessionEvent event) {

if (onLineUserNum>0){

onLineUserNum--;

}

String ip = request.getRemoteAddr();

log("sessionDestroyed('"+event.getSession().getId()+"'),-->IP:"+ip);

context.setAttribute("onLineUserNum", new Integer(onLineUserNum));

}

public void contextDestroyed(ServletContextEvent sce){

log("contextDestroyed-->ServletContext is destroyed");

this.context = null;

}

public void contextInitialized(ServletContextEvent sce){

this.context = sce.getServletContext();

log("contextDestroyed-->Ser新蒲京澳门赌场网站vletContext is initialized");

}

public int getUserOnline(){

return this.onLineUserNum;

}

public void log(String message) {

PrintWriter out = null;

try{

out = new PrintWriter(new FileOutputStream("c:\\eaieLogger.txt",true));

out.println(new java.util.Date().toLocaleString()+"::From SessionListener:"+message);

out.println("Totle visit number:"+this.visitNum+" and current online num: "+onLineUserNum);

out.println("=================================");

out.close();

}catch(Exception e){

e.printStackTrace();

out.close();

}

}

public void requestInitialized(ServletRequestEvent event) {

request = (HttpServletRequest)event.getServletRequest();

}

//省略其他措施

}

这个法度榜样必要在web.xml中设置设置设备摆设摆设:

com.eaie.system.SessionListener

必要留意的是LISTENR的位置,必须在的相关设置设置设备摆设摆设之后,在的相关设置设置设备摆设摆设之前

免责声明:以上内容源自网络,版权归原作者所有,如有侵犯您的原创版权请告知,我们将尽快删除相关内容。

您可能还会对下面的文章感兴趣: