过滤器、监听器常见应用

过滤器、监听器常见应用

监听器:GUI编程中经常使用;

public class TestPanel {
    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 WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
            }
        });

    }
}

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

1.用户登录之后,向Sesison中放入用户的数据

2.进入主页的时候要判断用户是否已经登录;要求:在过滤器中实现!

HttpServletRequest request = (HttpServletRequest) req;//强转
HttpServletResponse response = (HttpServletResponse) resp;//强转

if (request.getSession().getAttribute(Constant.USER_SESSION)==null){
    response.sendRedirect("/error.jsp");
}

chain.doFilter(request,response);

具体内容看视频

posted @ 2022-05-31 17:48  无关风月7707  阅读(16)  评论(0编辑  收藏  举报