前言:

本文内容:过滤器Filter、监听器

推荐免费JavaWeb入门到实战视频:【狂神说Java】JavaWeb入门到实战_哔哩哔哩_bilibili

过滤器Filter

Filter:过滤器,用来过滤网站的数据

  • 中文乱码
  • 登录验证

119

Filter基本使用

  • 导包

  • 编写过滤器(注意Filter包为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
    package com.jokerdig.filter;

    import javax.servlet.*;
    import java.io.IOException;

    /**
    * @author Joker大雄
    * @data 2022/4/9 - 10:03
    **/
    public class CharacterEncodingFilter implements Filter {


    // 初始化 在web服务器启动的时候就已经初始化
    public void init(FilterConfig filterConfig) throws ServletException {
    System.out.println("编码过滤器初始化");
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    // 解决乱码
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
    response.setContentType("text/html;charset=UTF-8");
    System.out.println("执行前");
    // chain 链
    // 用来放行
    chain.doFilter(request,response);
    System.out.println("执行后");
    }
    // 销毁 在web服务器关闭时执行销毁
    public void destroy() {
    System.out.println("编码过滤器已销毁");
    }
    }

    用来测试乱码的Servlet

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    package com.jokerdig.servlet;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;

    /**
    * @author Joker大雄
    * @data 2022/4/9 - 10:07
    **/
    public class ResponseServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.getWriter().write("测试过滤器解决中文乱码");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    doGet(req,resp);
    }
    }
  • 配置web.xml

    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
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    version="4.0">

    <!-- 配置-->
    <servlet>
    <servlet-name>response</servlet-name>
    <servlet-class>com.jokerdig.servlet.ResponseServlet</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>response</servlet-name>
    <url-pattern>/servlet/resp</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
    <servlet-name>response</servlet-name>
    <url-pattern>/resp</url-pattern>
    </servlet-mapping>
    <!-- 配置过滤器-->
    <filter>
    <filter-name>filterEncoding</filter-name>
    <filter-class>com.jokerdig.filter.CharacterEncodingFilter</filter-class>
    </filter>
    <filter-mapping>
    <filter-name>filterEncoding</filter-name>
    <!-- 过滤/servlet下的所有请求-->
    <url-pattern>/servlet/*</url-pattern>
    </filter-mapping>
    </web-app>

监听器

实现一个监听器的接口:

  • 编写一个监听器,实现监器接口,重写方法(这里使用session监听器测试网站在线人数)

    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
    44
    45
    46
    47
    48
    49
    package com.jokerdig.listener;

    import javax.servlet.ServletContext;
    import javax.servlet.http.HttpSessionEvent;
    import javax.servlet.http.HttpSessionListener;

    /**
    * @author Joker大雄
    * @data 2022/4/9 - 10:32
    **/

    // 统计网站在线人数
    public class OnlyCountListener implements HttpSessionListener {

    // 创建session监听 session创建就触发
    public void sessionCreated(HttpSessionEvent se) {
    ServletContext context = se.getSession().getServletContext();
    Integer onlyCount =(Integer) context.getAttribute("onlyCount");

    // 判断是否有人在线
    if(onlyCount==null){
    onlyCount= new Integer(1);
    } else {
    int count = onlyCount.intValue();
    onlyCount= new Integer(count+1);
    }
    context.setAttribute("onlyCount",onlyCount);

    }
    //销毁session监听 session销毁就触发
    public void sessionDestroyed(HttpSessionEvent se) {
    ServletContext context = se.getSession().getServletContext();
    Integer onlyCount =(Integer) context.getAttribute("onlyCount");
    // 判断是否有人在线
    if(onlyCount==null){
    onlyCount= new Integer(0);
    } else {
    int count = onlyCount.intValue();
    onlyCount= new Integer(count-1);
    }
    context.setAttribute("onlyCount",onlyCount);

    /*
    Session销毁:
    手动销毁 se.getSession().invalidate();
    自动销毁 在web.xml配置session-timeout
    */
    }
    }
  • 使用并不多,仅作了解