zoukankan      html  css  js  c++  java
  • 过滤器和监听器

    Filter和Listener

    1. Filter概述

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

    graph LR id1[Web浏览器] --> id2[Web服务器] id2 --> id1 id2 --> id3[<strong>过滤器</strong> <br>web服务有一些垃圾请求,后台不应该处理,或者应该报错<br>处理乱码问题] id3 --> id2 id3 --> id4[Servlet<br>JSP<br>HTML<br>静态资源<br>......] id4 --> id3

    2. Filter开发步骤

    1. 导包不要错:javax.servlet包
    2. 编写过滤器:实现Filter接口,重写对应方法
    package com.wang.filter;
    
    import javax.servlet.*;
    import java.io.IOException;
    
    public class CharacterEncodingFilter implements Filter {
    
        //初始化:web服务器启动,就已经初始化了,随时等待过滤对象出现!
        public void init(FilterConfig filterConfig) throws ServletException {
            System.out.println("CharacterEncodingFilter初始化");
        }
    
        //1.过滤器中的所有代码在过滤特定请求的时候都会执行
        //2.必须要让过滤器继续通行:    filterChain.doFilter(servletRequest,servletResponse);
        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执行后...");
        }
    
        //销毁:web服务器关闭的时候,过滤器会销毁
        public void destroy() {
            System.out.println("CharacterEncodingFilter销毁");
        }
    }
    
    1. 在web.xml中配置Filter
    <?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>ShowServlet</servlet-name>
            <servlet-class>com.wang.Servlet.ShowServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>ShowServlet</servlet-name>
            <url-pattern>/Servlet/show</url-pattern>
        </servlet-mapping>
        
        <filter>
            <filter-name>CharacterEncodingFilter</filter-name>
            <filter-class>com.wang.filter.CharacterEncodingFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>CharacterEncodingFilter</filter-name>
            <!--只要是/Servlet的任何请求,都会经过这个过滤器-->
            <url-pattern>/Servlet/*</url-pattern>
        </filter-mapping>
        
    </web-app>
    

    3. 监听器

    监听器是实现一个监听器的接口

    1. 编写一个监听器:实现监听器的接口,重写里面的方法
    package com.wang.listener;
    
    import javax.servlet.ServletContext;
    import javax.servlet.http.HttpSessionEvent;
    import javax.servlet.http.HttpSessionListener;
    import java.awt.*;
    
    //统计网站在线人数:统计session
    public class OnlineCountListener implements HttpSessionListener {
    
        //创建session监听
        //一旦创建一个session,就会触发一次这个事件!
        @Override
        public void sessionCreated(HttpSessionEvent httpSessionEvent) {
            //每次来一个session,取出上下文
            ServletContext ctx = httpSessionEvent.getSession().getServletContext();
            //将上下文中的OnlineCount属性取出!
            Integer onlineCount = (Integer) ctx.getAttribute("OnlineCount");
    
            //如果一开始没有人,第一个session来的时候将onlineCount设定为1
            if (onlineCount == null) {
                onlineCount = new Integer(1);
            }else {
                //如果之前有人,来一个session就将onlineCount加1
                int count = onlineCount;
                onlineCount = new Integer(count + 1);
            }
    
            //将计算得到的onlineCount值放入OnlineCount属性中!
            ctx.setAttribute("OnlineCount", onlineCount);
    
        }
    
        //销毁session监听
        //一旦session销毁,就会触发这个事件!
        @Override
        public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
            //每次来一个session,取出上下文
            ServletContext ctx = httpSessionEvent.getSession().getServletContext();
            //将上下文中的OnlineCount属性取出!
            Integer onlineCount = (Integer) ctx.getAttribute("OnlineCount");
    
            if (onlineCount == null) {
                onlineCount = new Integer(0);
            }else {
                //如果之前有人,来一个session就将onlineCount减1
                int count = onlineCount;
                onlineCount = new Integer(count - 1);
            }
    
            //将计算得到的onlineCount值放入OnlineCount属性中!
            ctx.setAttribute("OnlineCount", onlineCount);
        }
    
        /*
        session销毁:
        1. 手动销毁     httpSessionEvent.getSession().invalidate();
        2. 自动销毁     在web.xml下配置:设定1分钟后自动销毁
                        <session-config>
                            <session-timeout>1</session-timeout>
                        </session-config>
         */
    }
    
    1. 在web.xml中注册监听器
    <?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>ShowServlet</servlet-name>
            <servlet-class>com.wang.Servlet.ShowServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>ShowServlet</servlet-name>
            <url-pattern>/Servlet/show</url-pattern>
        </servlet-mapping>
        
        <filter>
            <filter-name>CharacterEncodingFilter</filter-name>
            <filter-class>com.wang.filter.CharacterEncodingFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>CharacterEncodingFilter</filter-name>
            <!--只要是/Servlet的任何请求,都会经过这个过滤器-->
            <url-pattern>/Servlet/*</url-pattern>
        </filter-mapping>
        
        <!--注册监听器-->
        <listener>
            <listener-class>com.wang.listener.OnlineCountListener</listener-class>
        </listener>
    
    </web-app>
    
    1. 看情况是否使用

    此处在前台展示session数目

    <%--
      Created by IntelliJ IDEA.
      User: Wang
      Date: 2020/8/20
      Time: 16:58
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
      <head>
        <title>$Title$</title>
      </head>
      <body>
      <%--
        JSP中,this关键字代表JSP中的page对象
        通过getServletConfig获取Servlet的初始化参数
        通过getServletContext获取Servlet的上下文
        通过getAttribute获取上下文中对应名称的属性值
      --%>
      <h1>当前有 <span style="color: aqua"><%=this.getServletConfig().getServletContext().getAttribute("OnlineCount")%></span> 人在线</h1>
      </body>
    </html>
    

    首次启动时有可能不为1,这是由于浏览器使用了多个session,只有最后一个才成功!

    4. 过滤器,监听器的常见应用

    1. 在GUI中使用监听器关闭窗口

    package com.wang.listener;
    
    import java.awt.*;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;
    
    public class PagePanel {
        public static void main(String[] args) {
            Frame frame = new Frame("这是一个窗口");       //新建一个窗体
            Panel panel = new Panel(null);              //面板
            frame.setLayout(null);                             //设置窗体的布局
    
            frame.setBounds(300, 300, 500, 500);
            frame.setBackground(Color.BLUE);                 //设置背景颜色
            panel.setBounds(50, 50, 300, 300);  //设置背景颜色
            panel.setBackground(Color.GREEN);
    
            frame.add(panel);
    
            frame.setVisible(true);     //设置窗体可见性
    
            //监听事件,监听关闭时间
            frame.addWindowListener(new WindowListener() {
                @Override
                public void windowOpened(WindowEvent e) {
                    System.out.println("打开");
                }
    
                @Override
                public void windowClosing(WindowEvent e) {
                    System.out.println("正在关闭");
                    //0:程序正常关闭
                    System.exit(0);
                }
    
                @Override
                public void windowClosed(WindowEvent e) {
                    System.out.println("已关闭");
                }
    
                @Override
                public void windowIconified(WindowEvent e) {
    
                }
    
                @Override
                public void windowDeiconified(WindowEvent e) {
    
                }
    
                @Override
                public void windowActivated(WindowEvent e) {
                    System.out.println("激活");
                }
    
                @Override
                public void windowDeactivated(WindowEvent e) {
                    System.out.println("未激活");
                }
            });
    
            //监听窗口的另一种写法,避免对所有方法重写
            //new 一个 WindowAdapter(),选择要重写的方法
            frame.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    super.windowClosing(e);
                }
            });
        }
    }
    

    2. Filter实现权限拦截

    需求:用户登录之后才能进入主页,用户注销之后就不能进入了!

    1. 用户登录之后,想Session中放入用户数据
    2. 进入主页的时候要判断用户是否已经登录(在过滤器中实现)

    login

    package com.wang.Servlet;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    public class LoginServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //获取前端的请求
            String username = req.getParameter("username");
    
            //如果用户名是admin,则登录成功,将用户信息放到session中
            if (username.equals("admin")) {
                //将键值对为USER_SESSION:当前用户的session id的信息放入session中
                req.getSession().setAttribute("USER_SESSION", req.getSession().getId());
                //重定向,用response,给客户端响应一个url
                //转发用的是request,给服务器响应一个url
                resp.sendRedirect("/sys/success.jsp");
            }else {
                resp.sendRedirect("/error.jsp");
                //登录失败
            }
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
    

    logout

    package com.wang.Servlet;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    public class LogoutServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
            //获得session中USER_SESSION的值
            Object user_session = req.getSession().getAttribute("USER_SESSION");
    
            if (user_session != null) {
                //注销,移除session中存放的USER_SESSION属性
                req.getSession().removeAttribute("USER_SESSION");
                //注销后重定向到登录页面
                resp.sendRedirect("/login.jsp");
            }else {
                resp.sendRedirect("/login.jsp");
            }
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
    

    过滤器

    package com.wang.filter;
    
    import javax.servlet.*;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    public class SysFilter implements Filter {
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
    
        }
    
        @Override
        public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
    
            //过滤器中的为上一层的类型,为了能够取到session,我们要强转为HttpServletResponse和HttpServletRequest
            HttpServletRequest request = (HttpServletRequest) req;
            HttpServletResponse response = (HttpServletResponse) resp;
    
            //如果session的USER_SESSION字段为空,重定向到error.jsp
            if (request.getSession().getAttribute("USER_SESSION") == null) {
                response.sendRedirect("/error.jsp");
            }
    
            chain.doFilter(request, response);
        }
    
        @Override
        public void destroy() {
    
        }
    }
    

    前端(login)

    <%--
      Created by IntelliJ IDEA.
      User: Wang
      Date: 2020/8/21
      Time: 10:43
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    
    <%--action:提交后,会跳转到对应的页面--%>
    <form action="Servlet/login" method="post">
        <input type="text" name="username">
        <input type="submit">
    </form>
    
    </body>
    </html>
    
  • 相关阅读:
    BZOJ 1207
    Poj 2096 Collecting Bugs (概率DP求期望)
    HDU 5159 Card (概率求期望)
    HDU 4649 Professor Tian (概率DP)
    HDU 4652 Dice (概率DP)
    HDU5001 Walk(概率DP)
    C++中的 Round(),floor(),ceil()
    HDU 5245 Joyful(概率题求期望)
    poj 3071 Football (概率DP水题)
    关于一个群号分解的最大质数的求法
  • 原文地址:https://www.cnblogs.com/wang-sky/p/13540990.html
Copyright © 2011-2022 走看看