要求使用javax.servlet.filter实现统计某个用户访问了多少次tomcat服务器的功能
发布网友
发布时间:2022-05-02 17:04
我来回答
共2个回答
热心网友
时间:2023-10-09 00:59
// Import required java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class SiteHitCounter implements Filter{
private int hitCount;
public void init(FilterConfig config)
throws ServletException{
// Reset hit counter.
hitCount = 0;
}
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws java.io.IOException, ServletException {
// increase counter by one
hitCount++;
// Print the counter.
System.out.println("Site visits count :"+ hitCount );
// Pass request back down the filter chain
chain.doFilter(request,response);
}
public void destroy()
{
// This is optional step but if you like you
// can write hitCount value in your database.
}
}
....
<filter>
<filter-name>SiteHitCounter</filter-name>
<filter-class>SiteHitCounter</filter-class>
</filter>
<filter-mapping>
<filter-name>SiteHitCounter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
....
热心网友
时间:2023-10-09 00:59
每来一个用户+1