JavaWeb-過濾器,監聽器的實現流程

過濾器

Filter:過濾器 ,用來過濾網站的數據;

  • 處理中文亂碼
  • 登錄驗證….

在這裏插入圖片描述

1.導包:過濾器要到的包是 javax.servlet
2.編寫過濾器:實現Filter接口,重寫對應的方法即可

public class CharacterEncodingFilter implements Filter {

    //初始化:web服務器啓動,就以及初始化了,隨時等待過濾對象出現!
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("CharacterEncodingFilter初始化");
    }

    //Chain : 鏈
    /*
    1. 過濾中的所有代碼,在過濾特定請求的時候都會執行
    2. 必須要讓過濾器繼續同行
        chain.doFilter(request,response);
     */
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");

        System.out.println("CharacterEncodingFilter執行前....");
        chain.doFilter(request,response); //讓我們的請求繼續走,如果不寫,程序到這裏就被攔截停止!
        System.out.println("CharacterEncodingFilter執行後....");
    }

    //銷燬:web服務器關閉的時候,過濾會銷燬
    public void destroy() {
        System.out.println("CharacterEncodingFilter銷燬");
    }
}

3.在web.xml中配置Filter

<filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>com.yang.servlet.CharacterEncodingFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <!--只要是 /servlet的任何請求,會經過這個過濾器-->
    <url-pattern>/zifu</url-pattern>
</filter-mapping>

監聽器

實現監聽器的接口

public class OnlineCountListener implements HttpSessionListener {

    //創建session監聽: 看你的一舉一動
    //一旦創建Session就會觸發一次這個事件!
    public void sessionCreated(HttpSessionEvent se) {
        ServletContext ctx = se.getSession().getServletContext();

        System.out.println(se.getSession().getId());

        Integer onlineCount = (Integer) ctx.getAttribute("OnlineCount");

        if (onlineCount==null){
            onlineCount = new Integer(1);
        }else {
            int count = onlineCount.intValue();
            onlineCount = new Integer(count+1);
        }

        ctx.setAttribute("OnlineCount",onlineCount);

    }

    //銷燬session監聽
    //一旦銷燬Session就會觸發一次這個事件!
    public void sessionDestroyed(HttpSessionEvent se) {
        ServletContext ctx = se.getSession().getServletContext();

        Integer onlineCount = (Integer) ctx.getAttribute("OnlineCount");

        if (onlineCount==null){
            onlineCount = new Integer(0);
        }else {
            int count = onlineCount.intValue();
            onlineCount = new Integer(count-1);
        }

        ctx.setAttribute("OnlineCount",onlineCount);

    }
}

編寫測試,用index.jsp即可

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

<h1>當前:<span><%=this.getServletConfig().getServletContext().getAttribute("OnlineCount")%>人在使用</span></h1>

</html>

在web.xml註冊監聽器

<!--註冊監聽器-->
<listener>
    <listener-class>com.yang.servlet.OnlineCountListener</listener-class>
</listener>

應用

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) {
               System.exit(0);
            }
        });
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章