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}