PLC解密网-PLC培训学习-工控自动化人才技术交流

超级管理员

453

帖子

1378

回复

3110

积分

楼主
发表于 2020-02-16 15:01:07 | 查看: 1966 | 回复: 0

(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>



您需要登录后才可以回帖 登录 | 立即注册

技术支持 KZYPLC V2.1 © 2020-2027

欢迎光临昆山中宇工控PLC论坛!您是第 6436403 位访问者, 日访问量: 20964 总访问量: 15898961,当前 2024-04-18 20:13:02 在线人数:57

ICP备案证书号: 苏ICP备14003016-2号