13、过滤器、监听器常见应用

过滤器、监听器常见应用

监听器: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 WindowListener() {
            @Override
            public void windowOpened(WindowEvent windowEvent) {
                System.out.println("打开");
            }

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

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

            @Override
            public void windowIconified(WindowEvent windowEvent) {

            }

            @Override
            public void windowDeiconified(WindowEvent windowEvent) {

            }

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

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

image-20210613145140203

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

步骤:

1.编写登录界面的代码 login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录界面</title>
</head>
<body>
<h1>登录界面</h1>
<form action="/servlet/login" method="post">
    <input type="text" name="username">
    <input type="submit" value="提交">
</form>
</body>
</html>

image-20210613164743290

2.编写LoginServlet,并且配置xml

public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取前端请求的参数
        String username = req.getParameter("username");
        req.getSession().setAttribute(Constant.USER_SESSION,username);
        if ( username.equals("admin") ) {
            //登录成功
            //重定向
            resp.sendRedirect("/sys/success.jsp");
        } else {
            //登录失败
            resp.sendRedirect("/error.jsp");
        }
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
	<servlet>
        <servlet-name>LoginServlet</servlet-name>
        <servlet-class>com.cc.servlet.LoginServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>LoginServlet</servlet-name>
        <url-pattern>/servlet/login</url-pattern>
    </servlet-mapping>

3.如果输入的用户名是 admin,则跳转到 /sys/success.jsp

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

<h1>欢迎您,${sessionScope.USER_SESSION}</h1>

<p><a href="/servlet/logout">注销</a></p>

</body>
</html>

image-20210613164829663

image-20210613164848726

4.注销用户后,会指向LogoutServlet,

  • LogoutServlet会判断session是否为空,不为空则把session中的数据给移除,再重定向到login.jsp中
  • session为空则直接重定向到login.jsp中
  • 配置xml
public class LogoutServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object session = req.getSession().getAttribute(Constant.USER_SESSION);
        if ( session != null ) {
            req.getSession().removeAttribute(Constant.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);
    }
}
<servlet>
        <servlet-name>LogoutServlet</servlet-name>
        <servlet-class>com.cc.servlet.LogoutServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>LogoutServlet</servlet-name>
        <url-pattern>/servlet/logout</url-pattern>
    </servlet-mapping>

5.当注销用户后,就不能再访问登录成功的页面了(用过滤器来实现)

注销用户后,session中没有数据,则会访问到error.jsp中

public class SysFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        Object userSession = req.getSession().getAttribute(Constant.USER_SESSION);
        if ( userSession == null ) {
            resp.sendRedirect("/error.jsp");
        }
        filterChain.doFilter(request,response);
    }
    @Override
    public void destroy() {
    }
}

配置过滤器(会过滤sys下的所有文件)

	<filter>        <filter-name>SysFilter</filter-name>        <filter-class>com.cc.filter.SysFilter</filter-class>    </filter>    <filter-mapping>        <filter-name>SysFilter</filter-name>        <url-pattern>/sys/*</url-pattern>    </filter-mapping>

image-20210613164942495

6.输入的用户不是 admin,则会跳转到 error.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <title>Title</title></head><body><h1>很抱歉,${sessionScope.USER_SESSION}没有权限登录</h1><p><a href="/login.jsp">返回首页</a></p></body></html>

image-20210613165010729

image-20210613165027222

为了代码的可扩展性,把不变的数据放在公共区

public class Constant {    public final static String USER_SESSION = "USER_SESSION";}
posted @ 2021-06-23 23:01  多瑞C  阅读(48)  评论(0)    收藏  举报