Java web--filter Filter IP statistics access times

Source: Internet
Author: User
Tags set set

The number of IP statistics visits is the site statistics of each the number of times the IP address visited this website.

  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 toIpstatistics, you can create one in the filterMap, usingIPto beKey, the number of visits isvalue. When a user accesses, gets the requestedIP, ifIPin theMapHas been previously accessed, then the number of visits is added1, you can;IPin theMapis not present, the number of settings is1.

  So here's the problem!

Question one: Why use map storage?

A map is a data structure consisting of key-value pairs, in which all keys form a set set, and all values form a list collection. and each key in the collection is unique, keeping the consistency of the lookup results.

Question two: What domain do you place the map in?

Because access-related data is recorded in the map, it is necessary to create a map to record the data when the server is turned on. To store this map in ServletContext!

1. Store data using: Map, where the IP is the key and the number of accesses is the value.

ip           Times
192.168.51.2      
192.168.51.4       3

2. Place the map in the ServletContext

. Use filters to complete statistics!
  * Get map in ServletContext
   > Map map = (map) sc.getattribute ("map");
   > if (map = = null) {map = new HashMap (); Sc.setattribute ("Map", map);}
  * Get the IP address of the current request: Request.getremoteaddr ()
  * Use IP as key to see if the key exists in the map
  * If the IP is present in the map, the description is not the first access, Then get the number of accesses, plus 1, then save back to map
  * If the IP does not exist in the map, it is the first time to access, add a key value to the map, the key is IP, and the value is 1.
4. Create a index.jsp, this page will get the map in ServletContext and then loop through it.

Code:

Ipfilter.java

Importjava.io.IOException;ImportJava.util.LinkedHashMap;ImportJava.util.Map;ImportJavax.servlet.Filter;ImportJavax.servlet.FilterChain;ImportJavax.servlet.FilterConfig;ImportJavax.servlet.ServletContext;Importjavax.servlet.ServletException;Importjavax.servlet.ServletRequest;ImportJavax.servlet.ServletResponse;/*** Number of IP statistics visits*/ Public classIpFilterImplementsFilter {Privatefilterconfig config;  Public voidInit (filterconfig config)throwsservletexception { This. config=config; }    /** 1. Get map * 2. GET request IP * 3. See if IP exists in map * 4. If present, put the number of visits +1, and then save back * 5. If not, add a key value, the key is IP, and the value is 1 * 6.     Release! */     Public voidDoFilter (ServletRequest request, servletresponse response, Filterchain chain)throwsIOException, servletexception {//Get ServletConfigServletContext sc =Config.getservletcontext (); //get a map in ServletContextMap<string,integer> map= (map<string, integer>) sc.getattribute ("Map"); //If the map does not exist, this is the first time it is accessed        if(map==NULL){            //Create a mapmap=NewLinkedhashmap<string,integer>(); }        //GET request IPString IP =request.getremoteaddr (); //determine if the IP exists in the map        if(Map.containskey (IP)) {//If the IP exists, this IP has already visited the site//Get access timesInteger count =map.get (IP); //the number of visits +1count++; //Save the new number of visits backmap.put (IP, count); }Else{            //because this IP is the first time access, the value is 1Map.put (IP, 1); }        //put the map into the ServletContextSc.setattribute ("Map", map); //ReleaseChain.dofilter (request, response); }     Public voiddestroy () {}}

index.jsp

<%@ 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 Filter IP statistics access times

Related Article

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.