Struts2 provides statistics on the number of online users and access sources.

Source: Internet
Author: User

A Preliminary Study on listener and filter.

1. Implementation of Online users:

Create a static variable onlinecounter. When a session is received, onlinecounter ++ and onlinecounter -- when the session is destroyed --.

The listener class code is as follows:

package com.dr; import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSessionEvent;import javax.servlet.http.HttpSessionListener; import org.apache.log4j.Logger; public class OnlineCounter extends HttpServlet implements HttpSessionListener     {    private static Logger log = Logger.getLogger(OnlineCounter.class);    private static final long serialVersionUID = 1L;      private static int sessionCounter = 1;      public OnlineCounter(){          log.info("OnlineCounter initialized.");      }      public void sessionCreated(HttpSessionEvent se) {        sessionCounter++;          log.info("session created:" + sessionCounter);      }      public void sessionDestroyed(HttpSessionEvent se) {         sessionCounter--;          log.info("session destroied");      }      public static int getOnlineSession() {          return sessionCounter;      } } 

Add this listener to web. xml and set the session to automatically log out within 10 minutes.

 
        
  
   
Com. dr. OnlineCounter
         
       
     
  
   
10
  
      
  

2. Access source statistics

Initialize a static Map. When the filter receives a request, it obtains the access address. If the request exists in the map, the value is 1. Otherwise, the value is 0.

The method for obtaining the access source is String path = req. getRequestURI (). toString ();

This map is sent to jsp through ServletRequest request.

Filter code:

Package com. dr; import java. io. IOException; import java. util. hashMap; 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. http. cookie; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import org. apache. struts2.interceptor. applicationAware; public class CountTimes implements Filter {// The initialization value from the Filter: private String auther; private Integer time; static Map
 
  
Application = new HashMap (); @ Override public void destroy () {} ServletContext context; @ Override public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, servletException {HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse resp = (HttpServletResponse) response; // HttpServletApplication app = (HttpServletApplication) application; String path = req. getRequestURI (). toString (); time = application. get (path); if (time = null) {time = 0;} time ++; application. put (path, time); request. setAttribute ("application", application); chain. doFilter (req, resp) ;}@ Override public void init (FilterConfig filterConfig) throws ServletException {// get the initialization value auther = filterConfig. getInitParameter ("auther"); context = filterConfig. getServletContext ();}}
 

Register this filter in web. xml

          
  
   
MyFilter
           
  
   
Com. dr. CountTimes
         
           
   
      
         
  
   
MyFilter
                     
           
  
   
/*
        
 

The jsp file is as follows:

<% @ Page language = "java" contentType = "text/html; charset = GB18030" pageEncoding = "GB18030" import = "com. dr. onlineCounter "%> <% @ taglib prefix =" s "uri ="/struts-tags "%> <% @ taglib prefix =" c "uri =" http://java.sun.com/jsp/jstl/core "%>
 Insert title here  Online: <% = OnlineCounter. getOnlineSession () %> person
$ {Entry. key}$ {Entry. value}




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.