Listener and Filter Write classification statistics IP access times

Source: Internet
Author: User
Tags ip number

Servletcontextlistenertest class

Set Listener Listening

Package Com.zhiyou.ipadr.demo;

Import Java.util.HashMap;
Import Java.util.Map;

Import Javax.servlet.ServletContext;
Import javax.servlet.ServletContextEvent;
Import Javax.servlet.ServletContextListener;
Import Javax.servlet.annotation.WebListener;

@WebListener public
class Servletcontextlistenertest implements Servletcontextlistener {

    @Override
    public void contextinitialized (Servletcontextevent sce) {
        //create Map map< in Servletcontextlistener init method
        String,integer> map=new hashmap<> ();
        Get ServletContext
        ServletContext context = Sce.getservletcontext ();
        Context.setattribute ("Mapip", map);

    @Override public
    void contextdestroyed (Servletcontextevent sce) {


    }

}
Ipcountfilter class
Package Com.zhiyou.ipadr.demo;
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;

Import Javax.servlet.annotation.WebFilter; @WebFilter (urlpatterns= "*") public class Ipcountfilter implements Filter {@Override public void init (filterconf IG Filterconfig) throws servletexception {} @Override public void Dofilter (ServletRequest request, SERVLETR Esponse response, Filterchain chain) throws IOException, servletexception {//classification statistics IP access times SERVL
        Etcontext context = Request.getservletcontext ();

        map<string,integer> map = (map<string,integer>) context.getattribute ("Mapip");
        Gets the requested IP address String IP = request.getremoteaddr (); Determine if the map has access to the IP number if (map.cOntainskey (IP)) {map.put (IP, map.get (IP) +1);
        }else{map.put (IP, 1);

        //re-put ServletContext context.setattribute ("Mapip", map);

        SYSTEM.OUT.PRINTLN (Map.get (IP));
    Release Chain.dofilter (request, response);
 @Override public void Destroy () {}}
index.jsp

http://localhost:8080/week09_day04_3/index.jsp
Refresh more than once, several times is the site traffic, because the listener annotation for @webfilter (urlpatterns= "*"), that is, listening to all the action. The number of times to refresh is the amount of Web site visits

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
    pageencoding=" UTF-8 "%>
<%
    String path = Request.getcontextpath ();
    String basepath = request.getscheme () + "://" + request.getservername () + ":" + request.getserverport () + path + "/";
%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
show.jsp

http://localhost:8080/week09_day04_3/show.jsp
Table shows IP and display times

<%@ page language= "java" contenttype= "text/html; 
    Charset=utf-8 "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" "Http://www.w3.org/TR/html4/loose.dtd" > 

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.