1.在web.xml文件加入如下段代码,其中AccessCountListener.java是自己定义的监听器
<listener>
<listener-class>com.cfets.imix.util.AccessCountListener</listener-class>
</listener>
<listener>
<listener-class>com.cfets.imix.util.AccessCountListener</listener-class>
</listener>
3.在applicationContext-ioc.xml文件中添加如下段代码
<bean id="accessCountListener" class="com.cfets.imix.util.AccessCountListener">
<property name="statisticsDataService" ref="statisticsDataService"></property>
</bean>
<property name="statisticsDataService" ref="statisticsDataService"></property>
</bean>
2.每新建一次会话,访问量加1 【注:要实现HttpSessionListener接口】
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.cfets.imix.pojo.StatisticsData;
import com.cfets.imix.service.StatisticsDataService;
public class AccessCountListener implements HttpSessionListener{
final Logger log = LoggerFactory.getLogger("AccessCountListener");
private static int count;
private StatisticsDataService statisticsDataService;
private ApplicationContext appContext = null;
public AccessCountListener(){
count = 0;
}
//每新建一次会话,就会调用一次此方法
public void sessionCreated(HttpSessionEvent se) {
count = count + 1;
import com.cfets.imix.service.StatisticsDataService;
public class AccessCountListener implements HttpSessionListener{
final Logger log = LoggerFactory.getLogger("AccessCountListener");
private static int count;
private StatisticsDataService statisticsDataService;
private ApplicationContext appContext = null;
public AccessCountListener(){
count = 0;
}
//每新建一次会话,就会调用一次此方法
public void sessionCreated(HttpSessionEvent se) {
count = count + 1;
//如果配置文件没有加载,则重新获取
if(appContext == null){
HttpSession session = se.getSession();
ServletContext servletCxt = session.getServletContext();
appContext = WebApplicationContextUtils.getWebApplicationContext(servletCxt);
}
accessCount(appContext);
}
if(appContext == null){
HttpSession session = se.getSession();
ServletContext servletCxt = session.getServletContext();
appContext = WebApplicationContextUtils.getWebApplicationContext(servletCxt);
}
accessCount(appContext);
}
//session被销毁时调用,在此不做任何操作
public void sessionDestroyed(HttpSessionEvent arg0) {
}
public void sessionDestroyed(HttpSessionEvent arg0) {
}
public void accessCount(ApplicationContext appContext){
try{
//获取配置文件中id为accessCountListener的bean
AccessCountListener acl =
AccessCountListener acl =
(AccessCountListener) appContext.getBean("accessCountListener");
StatisticsDataService statisticsDataService = acl.getStatisticsDataService();
Date date = new Date();
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
String todayDate = sdf1.format(date);
int queryResult = statisticsDataService.queryByDateAndName(todayDate, "访问量");
if(queryResult == 0){
StatisticsData sd = new StatisticsData();
sd.setDate(todayDate);
sd.setName("访问量");
sd.setCount(1);
statisticsDataService.saveStatisticsData(sd);
}else if(queryResult == 1){
int countByCon = statisticsDataService.queryCountByDateAndName(todayDate, "访问量");
statisticsDataService.updateStatisticsData(todayDate, "访问量", countByCon+1);
statisticsDataService.queryCountByDateAndName(todayDate, "访问量");
}
StatisticsDataService statisticsDataService = acl.getStatisticsDataService();
Date date = new Date();
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
String todayDate = sdf1.format(date);
int queryResult = statisticsDataService.queryByDateAndName(todayDate, "访问量");
if(queryResult == 0){
StatisticsData sd = new StatisticsData();
sd.setDate(todayDate);
sd.setName("访问量");
sd.setCount(1);
statisticsDataService.saveStatisticsData(sd);
}else if(queryResult == 1){
int countByCon = statisticsDataService.queryCountByDateAndName(todayDate, "访问量");
statisticsDataService.updateStatisticsData(todayDate, "访问量", countByCon+1);
statisticsDataService.queryCountByDateAndName(todayDate, "访问量");
}
}catch(Exception e){
log.error("统计访问量数据出错!", e);
}
log.error("统计访问量数据出错!", e);
}
}
public StatisticsDataService getStatisticsDataService() {
return statisticsDataService;
}
public void setStatisticsDataService(StatisticsDataService statisticsDataService) {
this.statisticsDataService = statisticsDataService;
}
public static int getCount() {
return count;
}
public static void setCount(int count) {
AccessCountListener.count = count;
}
}
public StatisticsDataService getStatisticsDataService() {
return statisticsDataService;
}
public void setStatisticsDataService(StatisticsDataService statisticsDataService) {
this.statisticsDataService = statisticsDataService;
}
public static int getCount() {
return count;
}
public static void setCount(int count) {
AccessCountListener.count = count;
}
}