xiongxin / Blog

xiongxin's blog base on issues
0 stars 0 forks source link

Servlet学习笔记三 过滤器与监听器 #4

Open xiongxin opened 8 years ago

xiongxin commented 8 years ago

过滤器

主要作用

200d0158-e0fe-4951-91de-cdf00044b784

应用场景

xiongxin commented 8 years ago

过滤器示例代码 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         xsi:schemaLocation="
         http://java.sun.com/xml/ns/javaee
         http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         id="ServketWeb" version="3.0">

    <welcome-file-list>
        <welcome-file>
            /index.html
        </welcome-file>
    </welcome-file-list>

    <filter>
        <filter-name>MyFiter</filter-name>
        <filter-class>com.netease.MyFitler</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>MyFiter</filter-name>
        <url-pattern>/hello/world</url-pattern>
    </filter-mapping>

    <servlet>
        <servlet-name>HelloWrold</servlet-name>
        <servlet-class>com.netease.HelloWorld</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloWrold</servlet-name>
        <url-pattern>/hello/world</url-pattern>
    </servlet-mapping>
</web-app>

控制器代码

package com.netease;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by xiongxin on 2/26/16.
 */
public class MyFitler implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("filter init");
    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("filter do");
        HttpServletRequest res =
                (HttpServletRequest) servletRequest;
        HttpSession session = res.getSession();
        if (session.getAttribute("username") == null) {
            HttpServletResponse resp =
                    (HttpServletResponse) servletResponse;
            resp.sendRedirect("../index.html");
        } else {
            filterChain.doFilter(servletRequest, servletResponse);
        }

        //将请求对象和响应对象传递出去,否则会失效
        filterChain.doFilter(servletRequest, servletResponse);
    }

    public void destroy() {
        System.out.println("filter destroy");
    }
}
xiongxin commented 8 years ago

监听器

工作原理

0b9a080d-8db4-45e0-ade7-5f8c7dbb661a

监听器分类

2f0251db-af9f-4ff1-96ec-7e930f3c2889

应用场景

web.xml配置

    <listener>
        <listener-class>com.netease.MyListener</listener-class>
    </listener>

过滤器类代码

package com.netease;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

/**
 * Created by xiongxin on 2/26/16.
 */
public class MyListener implements HttpSessionAttributeListener,
        ServletContextListener, ServletRequestListener{

    @Override
    public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {

    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {

    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {

    }

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {

    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }

    @Override
    public void requestDestroyed(ServletRequestEvent servletRequestEvent) {

    }

    @Override
    public void requestInitialized(ServletRequestEvent servletRequestEvent) {

    }
}