过滤器、监听器常用

过滤器、监听器常用

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

public class GUIDemo1 {
    public static void main(String[] args) {
        JFrame jFrame = new JFrame("监听");
        Panel panel = new Panel(null);
        jFrame.setBounds(300,300,500,500);
        jFrame.setBackground(new Color(53, 220, 64));
        jFrame.setLayout(null);
        panel.setBounds(30,30,300,300);
        panel.setBackground(new Color(19, 0, 121));
        jFrame.add(panel);
        jFrame.setVisible(true);
        jFrame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
            }
        });
    }
}

用户登入后才能进入主页,用户注销后就进入登入页面。

  1. 用户登入之后向session中放入用户的数据
  2. 进入主页的时候要判断用户是否登入:用Filter实现。

Login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <h1>登入页面</h1>
    <head>
        <title>Title</title>
    </head>
    <body>
        <form action="login" method="post">
            <input type="text" name="username">
            <br>
            <input type="submit" value="登入">
        </form>
    </body>
</html>

success.jsp

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

        <h1>登入成功,执行下面操作</h1>
        <br>
        <a href="../logout">注销</a>
    </body>
</html>

error.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>Title</title>
    </head>
    <body>
        <h1>登入失败</h1>
        <h2>你没有访问的权限</h2>
        <a href="Login.jsp">重新登入</a>
    </body>
</html>

LoginServlet

@WebServlet("/login")
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("sys/success.jsp");
        }else{
            resp.sendRedirect("error.jsp");
        }
    }

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

LogoutServlet

@WebServlet("/logout")
public class LogoutServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object user_session = req.getSession().getAttribute("USER_SESSION");
        if(user_session!=null){
            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);
    }
}

SysFilter

@WebFilter("/sys/*")
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 {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;
        if(req.getSession().getAttribute(USER_SESSION)==null){
            resp.sendRedirect("../error.jsp");
        }
        filterChain.doFilter(req,resp);
    }

    @Override
    public void destroy() {

    }
}
posted @ 2021-07-12 16:48  拒绝平庸*  阅读(38)  评论(0编辑  收藏  举报