JAVA网站拜候次数并得到拜候者IP[网站编程]
本文“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.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import 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 HttpSessionActivationListener,
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("+session.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-->ServletContext 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中配置:
<listener>
<listener-class>com.eaie.system.SessionListener</listener-class>
</listener>
需求注意的是LISTENR的位置,必须在<FILTER>的相关配置之后,在<SERVLET>的相关配置之前
以上是“JAVA网站拜候次数并得到拜候者IP[网站编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |