过滤器的应用——统计IP访问次数
来源:互联网 发布:linux 查看文件夹数量 编辑:程序博客网 时间:2024/05/16 19:29
public class AListener implements ServletContextListener {/** * 在服务器启动时创建Map,保存到ServletContext */ public void contextInitialized(ServletContextEvent sce) { // 创建Map Map<String,Integer> map = new LinkedHashMap<String,Integer>(); // 得到ServletContext ServletContext application = sce.getServletContext(); // 把map保存到application中 application.setAttribute("map", map); } public void contextDestroyed(ServletContextEvent sce) { }}
public class AFilter implements Filter {private FilterConfig config;public void destroy() {}public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {/* * 1. 得到application中的map * 2. 从request中获取当前客户端的ip地址 * 3. 查看map中是否存在这个ip对应访问次数,如果存在,把次数+1再保存回去 * 4. 如果不存在这个ip,那么说明是第一次访问本站,设置访问次数为1 *//* * 1. 得到appliction */ServletContext app = config.getServletContext();Map<String,Integer> map = (Map<String, Integer>) app.getAttribute("map");/* * 2. 获取客户端的ip地址 */String ip = request.getRemoteAddr();/* * 3. 进行判断 */if(map.containsKey(ip)) {//这个ip在map中存在,说明不是第一次访问int cnt = map.get(ip);map.put(ip, cnt+1);} else {//这个ip在map中不存在,说明是第一次访问map.put(ip, 1);}app.setAttribute("map", map);//把map再放回到app中chain.doFilter(request, response);//肯定放行}
<h1 align="center">显示结果</h1><table align="center" width="60%" border="1"><tr><th>IP</th><th>次数</th></tr><c:forEach items="${applicationScope.map }" var="entry"><tr><td>${entry.key }</td><td>${entry.value }</td></tr></c:forEach></table>
0 0
- 过滤器的应用——统计IP访问次数
- 过滤器应用之分IP统计网站的访问次数
- 监听器和过滤器写的分类统计IP访问次数
- Filter应用之分ip统计网站的访问次数
- 通过map reduce统计应用ip访问次数
- ip统计访问浏览器次数
- 统计 访问量前10的ip 及访问次数
- 统计Apache访问次数前3名的IP
- pig简单案例统计每个ip的访问次数
- 如何统计日志里面访问次数最多的IP
- 分IP统计访问次数(监听器负责创建map,过滤器负责统计,JSP负责显示结果)
- Linux中awk统计ip访问次数
- JavaWeb——允许授权的IP访问站点过滤器
- 统计网站的访问次数
- Jsp之利用过滤器限制时间段内每个ip的访问次数
- 用shell统计访问日志里每个ip访问次数
- 过滤器-页面访问次数
- Java web----Filter之分ip统计网站的访问次数
- 安卓Service组件使用系列3:使用IntentService下载网络图片
- java反射和泛型的一点儿总结
- ios客户端websocket的helloworld
- 异或运算实现两个数的交换
- android 常用动画
- 过滤器的应用——统计IP访问次数
- OracleDBConsole 启动出错 特定服务错误代码 2
- 【leetcode】【165】Compare Version Numbers
- 使用Eclipse进行远程调试
- RabbitMQ基础概念详细介绍
- iOS应用崩溃日志分析
- Missing IOS Distribution signing identity ....
- Eclipse 更新版本,解决android xml加载过慢和容易崩溃的问题
- 正则表达式检查工程中的中文