通过监听器实现统计
来源:互联网 发布:淘宝童装店铺名称大全 编辑:程序博客网 时间:2024/05/17 01:57
1 设计一个监听类 com.yilong.listener.ApplicationListener
2设计一个过滤器com.yilong.filter.MyFilter拦截所有的用户请求
3在web.xml中注册过滤器和监听器
package com.yilong.listener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class ApplicationListener implements ServletContextListener{
public void contextDestroyed(ServletContextEvent sce) {
ServletContext context=sce.getServletContext();
PrintWriter pw=null;
try {
pw = new PrintWriter(context.getRealPath("/WEB-INF/count.txt"));
pw.write(String.valueOf(context.getAttribute("count")));
pw.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally{
if(pw!=null){
pw.close();
}
}
}
public void contextInitialized(ServletContextEvent sce) {
ServletContext context=sce.getServletContext();
File file=new File(context.getRealPath("/WEB-INF/count.txt"));
System.out.println(context.getRealPath("/WEB-INF/count.txt"));
FileReader fr=null;
BufferedReader br=null;
try {
fr=new FileReader(file);
br=new BufferedReader(fr);
String countStr=br.readLine();
if(countStr==null){
countStr="0";
}
Integer count=Integer.parseInt(countStr);
System.out.println(count);
context.setAttribute("count", count);
} catch (Exception e) {
try {if(fr!=null){
fr.close();
} if(br!=null){
br.close();
}}
catch (Exception e1) {
e1.printStackTrace();
}
}
}
}
package com.yilong.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class MyFilter implements Filter{
private ServletContext context;
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
synchronized (this) {//同步块,这里解决并发问题,但影响访问速度,
Integer count=(Integer) context.getAttribute("count");
count=count+1;
System.out.println(count);
context.setAttribute("count", count);
chain.doFilter(request, response);
}
}
public void init(FilterConfig config) throws ServletException {
context=config.getServletContext();
}
}
3 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<filter>
<filter-name>myfilter</filter-name>
<filter-class>com.yilong.filter.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>myfilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>com.yilong.listener.ApplicationListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
4.index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>统计</title>
</head>
<body>
${applicationScope.count }
</body>
</html>
- 通过监听器实现统计
- 通过监听器实现在线人数统计
- 通过监听器实现单点登录
- 使用java 监听器实现在线人数统计
- 监听器(实现在线统计人数,踢人)
- 监听器--ServletRequestListener接口实现统计在线人数
- Servlet监听器实现网站在线人数统计
- 利用监听器实现网站在线人数统计
- jsp监听器实现在线人数统计
- HttpSessionListener监听器,实现在线用户统计
- spring-boot通过HttpSessionListener监听器统计在线人数
- Servlet监听器简介与实现在线人数统计
- 使用监听器实现一个统计网站在线人数的示例
- 用Servlet监听器实现对在线人数的统计
- java使用监听器实现对在线用户的统计
- 使用监听器实现一个统计网站在线人数的示例
- Servlet监听器的简单使用实现统计在线人数
- 如何使用Servlet监听器实现网站在线人数统计
- Ubuntu下qt+opencv小试牛刀1:打开图片
- 为什么需要bootloader
- 改变矩阵元素的位置
- [Unity3d] 在Unity中使用c++ dll
- CentOS下通过yum安装svn及配置
- 通过监听器实现统计
- python 学习笔记4
- ScrollView内嵌套 WebView WebView 拦截滚动事件问题
- C语言函数学习
- andriod APP
- leetcode--Unique Paths && Unique Paths ii
- 【BZOJ】3339 - Ⅲ - 变式Ⅱ+变式Ⅲ
- 最短寻道时间优先算法(SSTF)
- struts2简单好用的自定义日期转换