公司最近在做官网,需要做流量统计,发现网上关于这一块的东西比较杂乱。整理了一下,以后备用
--------------------------------------------------------------------------------------------------------------------------------
public class StatFilter
implements Filter {
public StatFilter() {
}
public void init(FilterConfig filtercfg) throws javax.servlet.
ServletException {
}
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain filterchain) throws java.io.IOException,
javax.servlet.ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpSession session = httpRequest.getSession(true);
if (session.getAttribute("isRecorded") == null) {
//如果本次用户访问还没有记录,就记录本次用户信息,并保存到数据库中
session.setAttribute("isRecorded", Boolean.TRUE);
//在session对象中保存一个变量"isRecorded",并赋值
String ip = getIp(httpRequest);//用户ip
String browser = getBrowser(httpRequest);//用户使用的浏览器
String os = getSysInfo(httpRequest);//用户使用系统
String source = httpRequest.getHeader("Referer");//访问来源
String url = httpRequest.getRequestURI();//用户当前访问路径
if (browser == null) {
browser = "未知";
//如果读取的浏览器名为空,则赋值为“未知”
}
if (os == null) {
os = "未知";
//如果读取的操作系统名为空,则赋值为“未知”
}
try {
System.out.println("ip:"+ip);
System.out.println("os:"+os.trim());
System.out.println("browser:"+browser.trim());
System.out.println("source:"+source);
System.out.println("url:"+url);
// StatHandle.insert(ip, os.trim(), browser.trim());
//调用业务逻辑,将用户数据插入到数据库中
}
catch (Exception es) {
es.printStackTrace();
//在输入日志中打印异常信息
}
}
filterchain.doFilter(request, response);
//调用下一个过滤器或者直接调用JSP
}
// Mozilla/5.0 (Windows; U; Windows NT 5.0; zh-CN; rv:1.7.12) Gecko/20050919 Firefox/1.0.7
// Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)
public void destroy() {
}
public String getIp(HttpServletRequest httpRequest){
String ipAddress = httpRequest.getHeader("x-forwarded-for");
if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = httpRequest.getHeader("Proxy-Client-IP");
}
if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = httpRequest.getHeader("WL-Proxy-Client-IP");
}
if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = httpRequest.getRemoteAddr();
if(ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")){
//根据网卡取本机配置的IP
InetAddress inet=null;
try {
inet = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
ipAddress= inet.getHostAddress();
}
}
//对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
if(ipAddress!=null && ipAddress.length()>15){ //"***.***.***.***".length() = 15
if(ipAddress.indexOf(",")>0){
ipAddress = ipAddress.substring(0,ipAddress.indexOf(","));
}
}
return ipAddress;
}
public String getBrowser(HttpServletRequest httpRequest){
String agent = httpRequest.getHeader("User-Agent");
String browserVersion = "";
//从请求头中读取User-Agent值
if(agent.indexOf("MSIE")>0){
browserVersion = "IE";
}else if(agent.indexOf("Firefox")>0){
browserVersion = "Firefox";
}else if(agent.indexOf("Chrome")>0){
browserVersion = "Chrome";
}else if(agent.indexOf("Safari")>0){
browserVersion = "Safari";
}else if(agent.indexOf("Camino")>0){
browserVersion = "Camino";
}else if(agent.indexOf("Konqueror")>0){
browserVersion = "Konqueror";
}else if(agent.indexOf("QQBrowser")>0){
browserVersion = "QQBrowser";
}
if("".equals(browserVersion)){
StringTokenizer st = new StringTokenizer(agent, "(;)");
while(st.hasMoreElements()){
browserVersion=st.nextToken();
}
}
return browserVersion;
}
public String getSysInfo(HttpServletRequest httpRequest){
String systenInfo="";
String agent = httpRequest.getHeader("User-Agent");
if (agent.indexOf("NT 10.0") > 0){
systenInfo = "Windows Vista/Server 10";
}else if (agent.indexOf("NT 8.0") > 0){
systenInfo = "Windows Vista/Server 8";
}else if (agent.indexOf("NT 6.0") > 0){
systenInfo = "Windows Vista/Server 2008";
} else if (agent.indexOf("NT 5.2") > 0){
systenInfo = "Windows Server 2003";
} else if (agent.indexOf("NT 5.1") > 0){
systenInfo = "Windows XP";
} else if (agent.indexOf("NT 6.0") > 0){
systenInfo = "Windows Vista";
} else if (agent.indexOf("NT 6.1") > 0){
systenInfo = "Windows 7";
} else if (agent.indexOf("NT 6.2") > 0){
systenInfo = "Windows Slate";
} else if (agent.indexOf("NT 6.3") > 0){
systenInfo = "Windows 8";
} else if (agent.indexOf("NT 5") > 0){
systenInfo = "Windows 2000";
} else if (agent.indexOf("NT 4") > 0){
systenInfo = "Windows NT4";
} else if (agent.indexOf("Me") > 0){
systenInfo = "Windows Me";
} else if (agent.indexOf("98") > 0){
systenInfo = "Windows 98";
} else if (agent.indexOf("95") > 0){
systenInfo = "Windows 95";
} else if (agent.indexOf("Mac") > 0){
systenInfo = "Mac";
} else if (agent.indexOf("Unix") > 0){
systenInfo = "UNIX";
} else if (agent.indexOf("Linux") > 0){
systenInfo = "Linux";
} else if (agent.indexOf("SunOS") > 0){
systenInfo = "SunOS";
}
if("".equals(systenInfo)){
StringTokenizer st = new StringTokenizer(agent, "(;)");
int i=0;
while(st.hasMoreElements()&&i<=1){
i++;
systenInfo=st.nextToken();
}
}
return systenInfo;
}
}
今天在实现的过程中,找出来一点问题,继续贴在末尾吧。上面是将代码写在了filter里,有一点不合适,因为在filter中的代码,只有在创建servlet容器的时候才会执行一次filter,此后则不再执行,我们要记录多个页面的访问情况,最好将代码嵌在jsp页面的初始化。这样则不受影响了