• Day47-过滤器、监听器常见应用


    Day47-过滤器、监听器常见应用

    过滤器、监听器常见应用

    监听窗口:

    package com.xiaozhao.listener;
    
    import java.awt.*;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;
    
    /**
     * @author 小龚
     * @create 2022-09-08 9:47
     */
    public class Test {
        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(new Color(0,0,255));//设置背景颜色
            panel.setBounds(50,50,300,300);
            panel.setBackground(new Color(0,255,0));//设置背景颜色
    
            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("关闭中");
                }
    
                @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("激活");
                    System.exit(0);
                }
    
                @Override
                public void windowDeactivated(WindowEvent e) {
                    System.out.println("未激活");
                }
            });
    
        }
    }
    
    • 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
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63

    测试

    在这里插入图片描述

    缩小化

    在这里插入图片描述

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

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

    代码实现:

    控制器:

    LoginOutServlet(控制注销)

    package com.xiaozhao.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 小龚
     * @create 2022-09-08 22:39
     */
    public class LoginOutServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            Object user_session = req.getSession().getAttribute("USER_SESSION");
            //如果user_session不为空,说明在登陆中
            if(user_session!=null){
                req.getSession().removeAttribute("USER_SESSION");//移除这个对象
                resp.sendRedirect(req.getContextPath()+"/login.jsp");//重定向到登录页面
            }
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
    
    • 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

    LoginServlet(控制登录)

    package com.xiaozhao.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 小龚
     * @create 2022-09-08 21:49
     */
    public class LoginServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //获取前端请求的参数
            String username = req.getParameter("username");
            if(username.equals("admin")){
                //登录成功
                req.getSession().setAttribute("USER_SESSION",req.getSession().getId());
                resp.sendRedirect(req.getContextPath()+"/sys/success.jsp");
            }
            else{
                //登录失败
                resp.sendRedirect(req.getContextPath()+"/error.jsp");
                //因为重定向的根目录是站点目录,因此要在路径前面加上当前web应用的路径:req.getContextPath(),不然就会出现404错误
            }
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    
    • 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

    过滤器:

    SysFilter(用户登录之后才能进入主页)

    package com.xiaozhao.filter;
    
    import com.mysql.fabric.jdbc.FabricMySQLConnection;
    
    import javax.servlet.*;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    /**
     * @author 小龚
     * @create 2022-09-08 23:10
     */
    public class SysFilter implements Filter {
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
    
        }
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            //HttpServlet和Servlet不一样,需要强转
            HttpServletRequest req = (HttpServletRequest) servletRequest;
            HttpServletResponse resp = (HttpServletResponse) servletResponse;
            Object user_session = req.getSession().getAttribute("USER_SESSION");
            //如果user_session为空,说明没有登录成功,那就不能直接进入success.jsp界面
            if(user_session==null){
                resp.sendRedirect("../error.jsp");
            }
            filterChain.doFilter(servletRequest,servletResponse);//这句话必写,不然会在这里卡死
        }
    
        @Override
        public void destroy() {
    
        }
    }
    
    • 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

    视图:

    错误页面:

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    
    
        Title
    
    
    

    错误

    没有权限,用户名错误

    返回登录页面
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    成功页面

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    
    
        Title
    
    
    

    小赵欢迎你

    注销

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    登录页面

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    
    
        Title
    
    
    

    登录

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    web.xml设置

    过滤器:

        <filter>
            <filter-name>SysFiltlerfilter-name>
            <filter-class>com.xiaozhao.filter.SysFilterfilter-class>
        filter>
        <filter-mapping>
            <filter-name>SysFiltlerfilter-name>
            <url-pattern>/sys/*url-pattern>
        filter-mapping>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    控制器

        <servlet>
            <servlet-name>LoginServletservlet-name>
            <servlet-class>com.xiaozhao.servlet.LoginServletservlet-class>
        servlet>
        <servlet-mapping>
            <servlet-name>LoginServletservlet-name>
            <url-pattern>/servlet/loginurl-pattern>
        servlet-mapping>
        <servlet>
            <servlet-name>LoginOutServletservlet-name>
            <servlet-class>com.xiaozhao.servlet.LoginOutServletservlet-class>
        servlet>
        <servlet-mapping>
            <servlet-name>LoginOutServletservlet-name>
            <url-pattern>/servlet/loginouturl-pattern>
        servlet-mapping>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    测试:

    登录界面

    在这里插入图片描述

    登录成功界面

    在这里插入图片描述

    点击注销后再次访问成功页面

    在这里插入图片描述

    显示失败(过滤器的作用)

    在这里插入图片描述

  • 相关阅读:
    DNS 系列(三):如何免受 DNS 欺骗的侵害
    文档流下载——axios+fetch两种方式下载——技能提升
    eclipse中创建的Maven Module和Maven Project及其间区别
    机器学习实战-决策树
    笔记---fastadmin踩坑之旅,fastadmin thinkphp 编辑数据下拉菜单 默认选择之前的值
    网络安全(黑客)自学
    Leetcode 77. 组合
    如何加快发明专利的审查时间
    CentOS 7 更新阿里源;CentOS 7更新yum源为阿里源;解决File contains no section headers.错误问题
    持续集成环境-maven、tomcat安装和配置
  • 原文地址:https://blog.csdn.net/GHY0911/article/details/126774716