实现网站在线访问计数器功能,网站的初始值设置为1000

wKiom1LWNNjhBuykAADDuwQ0B7A473.jpg

(1)创建CountFilter的类,实现javax。servlet.Filter接口,是一个过滤器对象,通过过滤器实现统计网站人数功能:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.lixiyu;
import java.io.IOException;
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.HttpServletRequest;
/**
 * 统计过滤器
 * @author lixiyu
 */
public class CountFilter implements Filter {
    // 来访数量
    private int count;
                                          
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // 获取初始化参数
        String param = filterConfig.getInitParameter("count");
        // 将字符串转换为int
        count = Integer.valueOf(param);
    }
    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        // 访问数量自增
        count ++;
        // 将ServletRequest转换成HttpServletRequest
        HttpServletRequest req =(HttpServletRequest)request;
        // 获取ServletContext
        ServletContext context =req.getSession().getServletContext();
        // 将来访数量值放入到ServletContext中
        context.setAttribute("count", count);
        // 向下传递过滤器
        chain.doFilter(request, response);
    }
    @Override
    public void destroy() {
    }
}

(2)配置已创建的CountFilter对象,设置初始值为1000,配置web.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
  <filter>
    <filter-name>CountFilter</filter-name>
    <filter-class>com.lixiyu.CountFilter</filter-class>
    <init-param>
      <param-name>count</param-name>
      <param-value>1000</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>CountFilter</filter-name>
    <url-pattern>/index.jsp</url-pattern>
  </filter-mapping>
</web-app>

(3)创建程序首页index.jsp,在该页面通过JSP内置对象Application获取计数器的值:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>人数统计</title>
</head>
<body>
    <h2>
    欢迎光临,<br>
    您是本站的第【
    <%=application.getAttribute("count") %>
     】位访客!
     </h2>
</body>
</html>

本文转自lixiyu 51CTO博客,原文链接:http://blog.51cto.com/lixiyu/1351934,如需转载请自行联系原作者