package com.filter;
im
port java.io.IOException;
im
port java.sql.Timestamp;
im
port java.text.DateFormat;
im
port java.text.ParseException;
im
port java.text.SimpleDateFormat;
im
port java.util.Date;
im
port java.util.UUID;
im
port javax.servlet.Filter;
im
port javax.servlet.FilterChain;
im
port javax.servlet.FilterConfig;
im
port javax.servlet.ServletContext;
im
port javax.servlet.ServletException;
im
port javax.servlet.ServletRequest;
im
port javax.servlet.ServletResponse;
im
port javax.servlet.http.HttpServletRequest;
im
port javax.servlet.http.HttpServletResponse;
im
port javax.servlet.http.HttpSession;
im
port org.apache.log4j.Logger;
im
port org.springf
ramework.beans.factory.annotation.Autowired;
im
port org.springf
ramework.context.ApplicationContext;
im
port org.springf
ramework.web.context.support.WebApplicationContextUtils;
im
port com.bean.LogBean;
im
port com.service.LogService;
public class LogFilter implements Filter {
Logger logger = Logger.getLogger(LogFilter.class);
@Autowired
private LogService logService;
public void doFilter(ServletRequest servletRequest,
ServletRespo
nse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
if (!(servletRequest instanceof HttpServletRequest)
|| !(servletRespo
nse instanceof HttpServletResponse)) {
throw new ServletException(
"o
ncePerRequestFilter just supports HTTP requests");
}
HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
HttpSession session = httpRequest.getSession();
String ip = httpRequest.getRemoteAddr();
String page = httpRequest.getRequestURI();
String co
ntextPath = httpRequest.getCo
ntextPath();
String servletPath = httpRequest.getServletPath();
logger.info("doFilter sessio
nId="+session.getId()+",ip="+ip+",page="+page+",co
ntextPath="+contextPath+",servletPath="+servletPath);
LogBean logBean = new LogBean();
logBean.setId(UUID.randomUUID().toString());
logBean.setSessio
nId(session.getId());
logBean.setIp(ip);
logBean.setPage(page);
logBean.setAccessTime(new Timestamp(new Date().getTime()));
logBean.setStayTime(0);
//通过session id 和 ip,查出最近的一条访问记录
LogBean bean = null;
try {
bean = logService.getLatestLog(session.getId(), ip);
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage());
}
//更改最近访问记录的停留时间,这里把两次访问记录的间隔时间算成上一次页面访问的停留时间
if(bean != null){
long stayTime = (System.currentTimeMillis() - bean.getAccessTime().getTime())/1000;
try {
logService.updateLog(bean.getId(), stayTime);
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage());
}
}
//保存当前访问记录
try {
logService.saveLog(logBean);
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage());
}
//统计网站的PV(页面浏览量),UV(独立访客数)
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String format = df.format(new Date());
Date parse = null;
try {
parse = df.parse(format);
} catch (ParseException e1) {
e1.printStackTrace();
}
Timestamp startTime = new Timestamp(parse.getTime());
Timestamp endTime = new Timestamp(parse.getTime() + 24*3600*1000);
try {
int pv = logService.getPV(startTime, endTime);
int uv = logService.getUV(startTime, endTime);
logger.info("pv="+pv);
logger.info("uv="+uv);
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage());
}
filterChain.doFilter(servletRequest, servletResponse);
return;
}
public void init(FilterCo
nfig filterConfig) throws ServletException {
ServletCo
ntext co
ntext = filterConfig.getServletCo
ntext();
Applicatio
nContext ctx = WebApplicationContextUtils.getWebApplicatio
nContext(context);
logService = (LogService) ctx.getBean("logService");
logger.info("init");
}
public void destroy() {
}