Count website visits by ip address, and count visits by ip Address

Source: Internet
Author: User

Count website visits by ip address, and count visits by ip Address


Package web. listener; import javax. servlet. servletContext; import javax. servlet. servletContextEvent; import javax. servlet. servletContextListener; import javax. servlet. annotation. webListener; import javax. servlet. http. httpSessionAttributeListener; import javax. servlet. http. httpSessionListener; import java. util. linkedHashMap; import java. util. map; @ WebListener () public class AListener implements ServletContextListener, HttpSessionListener, listener {/** create a Map at Service Startup and save it to ServletContext **/public void contextInitialized (ServletContextEvent sce) {// create Map <String, Integer> Map = new LinkedHashMap <String, Integer> (); // obtain ServletContext application = sce. getServletContext (); application. setAttribute ("map", map);} public void contextDestroyed (ServletContextEvent sce ){}}
1 package web. filter; 2 3 import javax. servlet. *; 4 import javax. servlet. annotation. webFilter; 5 import java. io. IOException; 6 import java. util. map; 7 8/* 9 * Get Map10 from application * Get IP11 of current client from request * for statistics, save the result to Map 12 **/13 @ WebFilter (filterName = "AFilter", urlPatterns = "/*") 14 public class AFilter implements Filter {15 private FilterConfig config; 16 public void destroy () {17} 18 19 public void doFilter (ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {20/* 21*1. Obtain map22 * 2 in the application, obtain the IP address of the current client from the request 23*3. Check whether the corresponding access times of this IP address exist in the map, if yes, save the number of times + 1 back to 24*4. If this IP address does not exist, it indicates that it was the first time you visited this site, set the number of visits to 125 **/26/* 27*1. Obtain application28 **/29 ServletContext app = config. getServletContext (); 30 Map <String, Integer> map = (Map <String, Integer>) app. getAttribute ("map"); 31/* 32*2. Obtain the Client ip address 33 **/34 String ip = req. getRemoteAddr (); 35/* 36*3. Judge 37 */38 if (map. containsKey (ip) {39 int cnt = map. get (ip); 40 map. put (ip, cnt + 1); 41} else {42 map. put (ip, 1); 43} 44 app. setAttribute ("map", map); 45 46 chain. doFilter (req, resp); // certainly allow 47} 48/* 49 * This method will be executed when the server is started, in addition, this method only runs 50 **/51 public void init (FilterConfig config) throws ServletException {52 this. config = config; 53} 54}
1 <% @ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %> 2 <% -- 3 Created by IntelliJ IDEA. 4 User: Mac 5 Date: 13/09/2017 6 Time: 12:37 PM 7 To change this template use File | Settings | File Templates. 8 -- %> 9 <% @ page contentType = "text/html; charset = UTF-8 "language =" java "%> 10 

 

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.