【Javaweb】过滤器和监听器的常见应用

1.监听器:在GUI中经常使用


package com.hxl.listener;

import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class TestPanel {
    public static void main(String[] args) {
        Frame frame = new Frame("2021中秋节快乐");//新建一个窗体
        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() {
            public void windowOpened(WindowEvent e) {
                System.out.println("打开");
            }

            public void windowClosing(WindowEvent e) {
                System.out.println("关闭ing");
                System.exit(0);
            }

            public void windowClosed(WindowEvent e) {
                System.out.println("关闭ed");
            }

            public void windowIconified(WindowEvent e) {
                
            }

            public void windowDeiconified(WindowEvent e) {

            }

            public void windowActivated(WindowEvent e) {
                System.out.println("激活");
            }

            public void windowDeactivated(WindowEvent e) {
                System.out.println("未激活");
            }
        });
    }
}


【Javaweb】过滤器和监听器的常见应用


2.用户登录后才能进入主页,用户注销后就不能进入首页


  • 用户登录之后,向Session中放入用户的数据


  • 进入主页的时候要判断用户是否登录,要求在过滤器中实现


    • 首先我们先进入登录页面


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录</title>
</head>
<body>

<form action="/servlet/login" method="post">
    <input type="text" name="username">
    <input type="submit">
</form>

</body>
</html>


    • 进入登录页面后我们走一个请求


package com.hxl.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");
        if(username.equals("admin")){
            //登录成功,存了一个session
            req.getSession().setAttribute("Constant.USER_SESSION", req.getSession().getId());
            resp.sendRedirect("/sys/success.jsp");
        }else{
            //登录失败
            resp.sendRedirect("/sys/error.jsp");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}


    • 成功走成功的页面


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>主页</title>
</head>
<body>

<%--
//没有下面的代码,就算没有admin也可以进入成功页面。这是jsp做的一种方法。我们一般不用
<%
    Object user_session = request.getSession().getAttribute("USER_SESSION");
    if(user_session==null){
        response.sendRedirect("/Login.jsp");
    }
%>
--%>


<h3>成功了</h3>
<p><a href="/servlet/logout">注销</a> </p>
</body>
</html>


    • 失败走失败的页面


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>错误</title>
</head>
<body>
<h3>用户名写错了</h3>
<p><a href="/Login.jsp">返回到登录页面</a> </p>
</body>
</html>


    • 我们再加个权限验证过滤器


package com.hxl.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");
        if(username.equals("admin")){
            //登录成功,存了一个session
            req.getSession().setAttribute("Constant.USER_SESSION", req.getSession().getId());
            resp.sendRedirect("/sys/success.jsp");
        }else{
            //登录失败
            resp.sendRedirect("/sys/error.jsp");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}


    • 我们发现session的一个名字比较常用,所以我们将他变成常量
package com.hxl.util;

public class Constant {
    public final static String USER_SESSION = "USER_SESSION";
}
    • 每一个servlet、Filter都需要在xml中进行配置
<servlet>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>com.hxl.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/servlet/login</url-pattern>
</servlet-mapping>
<servlet>
    <servlet-name>LoginOutServlet</servlet-name>
    <servlet-class>com.hxl.servlet.LoginOutServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>LoginOutServlet</servlet-name>
    <url-pattern>/servlet/logout</url-pattern>
</servlet-mapping>
<filter>
    <filter-name>SysFilter</filter-name>
    <filter-class>com.hxl.filter.SysFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>SysFilter</filter-name>
    <url-pattern>/sys/*</url-pattern>
</filter-mapping>


上一篇:智能化时代你还在手动刷新浏览器抢火车票?那你就真的Out了!假期快来了,冰河提前放大招,教你如何使用Python脚本自动抢火车票


下一篇:奇淫怪巧之给Delphi的PrintDialog增加一个页码选定范围打印的Edit