过滤器Filter和监听器Listener

过滤器:用来过滤网站的数据
  • 处理中文乱码
  • 登录验证
  • etc…
开发步骤:
  1. 导包,创建实现类,实现Filter接口(java.servlet包下的filter)

  2. 编写过滤器,重写三个方法,实现业务逻辑

    package com.ola.servlet;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    /**
     * ClassName:ShowServlet Package:com.ola.servlet
     *
     * @author morningj
     * @date 2021/1/9 11:57
     */
    public class ShowServlet extends HttpServlet {
        private static final long serialVersionUID = -3255769486004657840L;
    
        @Override
      protected void doGet(HttpServletRequest req, HttpServletResponse resp)
          throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        resp.getWriter().write("hello!你好世界!");
      }
    
      @Override
      protected void doPost(HttpServletRequest req, HttpServletResponse resp)
          throws ServletException, IOException {
        doGet(req, resp);
      }
    }
    
    
    package com.ola.filter;
    
    import javax.servlet.*;
    import java.io.IOException;
    
    /**
     * ClassName:CharacterEncodingFilter Package:com.ola.filter
     *
     * @author morningj
     * @date 2021/1/9 11:52
     */
    public class CharacterEncodingFilter implements Filter {
     @Override
     public void init(FilterConfig filterConfig) throws ServletException {
       System.out.println("已执行初始化操作!");
     }
    
     @Override
     public void destroy() {
       System.out.println("已执行销毁操作!");
     }
    
     @Override
     public void doFilter(
         ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
         throws IOException, ServletException {
       servletRequest.setCharacterEncoding("utf-8");
       servletResponse.setCharacterEncoding("utf-8");
       servletResponse.setContentType("text/html;charset=UTF-8");
    
       System.out.println("CharacterEncodingFilter执行前......");
       filterChain.doFilter(servletRequest, servletResponse);
       System.out.println("CharacterEncodingFilter执行后......");
     }
    }
    
    
    
  3. web.xml中配置Filter

注意:
  1. doFilter中的所有代码,在过滤特定请求的时候都会执行

  2. 必须要让过滤器继续通行,使用:

    filterChain.doFilter(servletRequest, servletResponse);
    
监听器The listener
package com.ola.listener;

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

/**
 * ClassName:OnlineCountListener Package:com.ola.listener
 * description:
 * 实现监听在线用户人数,通过session会话的数量判断;
 * @author morningj
 * @date 2021/1/9 15:50
 */
public class OnlineCountListener implements HttpSessionListener {
  @Override
  public void sessionCreated(HttpSessionEvent se) {
    ServletContext servletContext = se.getSession().getServletContext();
    Integer onlineCount = (Integer) servletContext.getAttribute("OnlineCount");
    if (onlineCount == null) {
      onlineCount = new Integer(1);
    } else {
      int count = onlineCount.intValue();
      onlineCount = new Integer(count + 1);
    }
    servletContext.setAttribute("OnlineCount", onlineCount);
  }

  @Override
  public void sessionDestroyed(HttpSessionEvent se) {
    ServletContext servletContext = se.getSession().getServletContext();
    Integer onlineCount = (Integer) servletContext.getAttribute("OnlineCount");
    if (onlineCount == null) {
      onlineCount = new Integer(1);
    } else {
      int count = onlineCount.intValue();
      onlineCount = new Integer(count - 1);
    }
    servletContext.setAttribute("OnlineCount", onlineCount);
  }
}
<listener>
        <listener-class>com.ola.listener.OnlineCountListener</listener-class>
</listener>
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值