Filter-Interceptor

Posted fan-mister

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Filter-Interceptor相关的知识,希望对你有一定的参考价值。

基于springMVC

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <servlet>
        <servlet-name>HelloMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.form</url-pattern>
    </servlet-mapping>

    <filter>
        <filter-name>firstTestFilter</filter-name>
        <filter-class>controller.filter.FirstTestFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>firstTestFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter>
        <filter-name>secondTestFilter</filter-name>
        <filter-class>controller.filter.secondTestFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>secondTestFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


</web-app>

mvc的xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--    控制器类所在的包-->
    <context:component-scan base-package="controller"/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--    /WEB-INF/page/意为页面的路径,.jsp为页面格式-->
        <property name="prefix" value="/WEB-INF/page/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <mvc:interceptors>
        <mvc:interceptor>
            <!-- 对所有请求都拦截,公共拦截器 -->
            <mvc:mapping path="/**"/>
            <bean name="testInterceptor"  class="controller.interceptor.TestInterceptor" />
        </mvc:interceptor>
    </mvc:interceptors>
</beans>

过滤器

FirstTestFilter

package controller.filter;

import javax.servlet.*;
import java.io.IOException;

public class FirstTestFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("FirstTestFilter.init()");
    }

    public FirstTestFilter() {
        System.out.println("FirstTestFilter.FirstTestFilter()构造方法");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("FirstTestFilter.doFilter()进来了");
        filterChain.doFilter(servletRequest,servletResponse);
        System.out.println("FirstTestFilter.doFilter()出去了");
    }

    @Override
    public void destroy() {

    }
}

  secondTestFilter

package controller.filter;

import javax.servlet.*;
import java.io.IOException;

public class secondTestFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("secondTestFilter.init()");
    }

    public secondTestFilter() {
        System.out.println("secondTestFilter.secondTestFilter()");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("secondTestFilter.doFilter()进来了");
        filterChain.doFilter(servletRequest, servletResponse);
        System.out.println("secondTestFilter.doFilter()出去了");
    }

    @Override
    public void destroy() {

    }
}

  两个过滤在web.xml中配置,按顺序依次执行,先进先出原则

 

拦截器

TestInterceptor

package controller.interceptor;


import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestInterceptor implements HandlerInterceptor {


    public TestInterceptor() {
        System.out.println("TestInterceptor的构造方法");
    }


    //访问action方法之前
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("TestInterceptor进入拦截器");
        return true;
    }

    //访问action之后,返回到页面之前
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        request.setAttribute("hh","ss");
    }

    //action结束之后,用于释放资源
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        request.setAttribute("hh","aa");
    }
}

  先执行过滤器后执行拦截器,主要用于对访问action之前和之后的拦截,在mvc的配置文件中配置

以上是关于Filter-Interceptor的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段——CSS选择器

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?

VSCode自定义代码片段——.vue文件的模板

VSCode自定义代码片段6——CSS选择器

VSCode自定义代码片段——声明函数