Java Web----Filter number of visits to the IP statistics site

Source: Internet
Author: User

Statistical work needs to be performed before all resources are available, so it can be placed in the filter.

We are not going to intercept this filter! Because we are only used to do statistics.

Use something to load the statistic data. Map<string,integer>

The entire site only needs a map!

When the map was created (using Servletcontextlistener, it was created when the server was started, and only in ServletContext), where is the map saved to! (Map saved to ServletContext!!!) )

    • The map needs to be used to save the data in the filter
    • Map needs to be used on the page to print the data in the map

1 analysis

Because a site may have multiple pages, regardless of which page is accessed, the number of visits is counted, so it is most convenient to use filters.

Because of the need for IP statistics, you can create a map in the filter, using the IP key, the number of accesses is value. When there is a user access, obtain the requested IP, if the IP exists in the map, indicating the previous access, then add 1 to the number of visits, the IP in the map does not exist, then set the number of 1.

Store this map in ServletContext!

2 Code

<?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 "><filter><filter-name>myfilter</filter-name ><filter-class>com.cug.filter02.MyFilter</filter-class></filter><filter-mapping> <filter-name>myfilter</filter-name><url-pattern>/*</url-pattern></filter-mapping ><listener><listener-class>com.cug.filter02.MyListener</listener-class></listener> </web-app>

Package Com.cug.filter02;import Java.util.linkedhashmap;import Java.util.map;import javax.servlet.ServletContext; Import Javax.servlet.servletcontextevent;import Javax.servlet.servletcontextlistener;public class MyListener Implements servletcontextlistener{@Overridepublic void contextdestroyed (Servletcontextevent arg0) {} @Overridepublic void contextinitialized (Servletcontextevent arg0) {ServletContext context = Arg0.getservletcontext (); map<string, integer> ipMap = new linkedhashmap<string, integer> (); Context.setattribute ("IpMap", IpMap);}}

Package Com.cug.filter02;import Java.io.ioexception;import Java.util.map;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 filterconfig filterconfig; @Overridepublic void Destroy () {} @Overridepublic void DoFilter (ServletRequest request, Servletresponse Response,filterchain chain) throws IOException, Servletexception {ServletContext context = Filterconfig.getservletcontext (); map<string, integer> IpMap = (map<string, integer>) context.getattribute ("IpMap"); String IP = request.getremoteaddr (), if (Ipmap.containskey (IP)) {Integer count = ipmap.get (IP); ipmap.put (ip,count+1);} Else{ipmap.put (ip,1);} Context.setattribute ("IpMap", IpMap); Chain.dofilter (request, response);} @Overridepublic void init (Filterconfig filterconfig) throws servletexception {thiS.filterconfig = Filterconfig;}} 

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><% @taglib prefix= "C" uri= "/HTTP/ Java.sun.com/jsp/jstl/core "%><%string path = Request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

Java Web----Filter number of visits to the IP statistics site

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.