首页 > 编程语言 > 详细

springboot整合filter

时间:2019-08-04 18:08:42      阅读:100      评论:0      收藏:0      [点我收藏+]

 

新建一个项目

技术分享图片

 

技术分享图片

 

 技术分享图片

 

 技术分享图片

 

 技术分享图片

 

新建Firstfilter类

技术分享图片

 

 技术分享图片

 

Firstfliter.java

package com.example.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;


/**
 *SpringBoot 整合 Filter 方式一
 *<filter>
 * <filter-name>FirstFilter</filter-name>
 * <filter-class>com.bjsxt.filter.FirstFilter</filter-class>
 *</filter>
 *<filter-mapping>
 * <filter-name>FirstFilter</filter-name>
 * <url-pattern>/first</url-pattern>
 *</filter-mapping>
 */
@WebFilter(filterName = "Firstfilter",urlPatterns = "/first")
public class Firstfilter implements Filter{
    @Override
    public void destroy() {

    }
    @Override
    public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
            throws IOException, ServletException {
        System.out.println("进入 Filter");
        arg2.doFilter(arg0, arg1);
        System.out.println("离开 Filter");
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {

    }
}

 

 

 新建servlet包,同时在包内新建FirstServlet类

技术分享图片

 

 

FirstServlet.java类

 

package com.example.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 *SpringBoot整合Servlet方式一
 *
 *<servlet>
 *    <servlet-name>FirstServlet</servlet-name>
 *    <servlet-class>com.bjsxt.servlet.FirstServlet</servlet-class>
 *</servlet>
 *
 *<servlet-mapping>
 * <servlet-name>FirstServlet</servlet-name>
 * <url-pattern>/first</url-pattern>
 *</servlet-mapping>
 *
 */

@WebServlet(name="FirstServlet",urlPatterns="/first")
public class FirstServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("FirstServlet............");
    }
}

 

 

新建一个App.java启动类

技术分享图片

 

 技术分享图片

 

App.java类

package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
/**
 *SpringBoot 整合 Filter 方式一
 *
 */
@SpringBootApplication
@ServletComponentScan
public class App {
  public static void main(String[] args)
  {
      SpringApplication.run(App.class,args);
  }

}

 

 

 

运行启动类

技术分享图片

 

 技术分享图片

 

 在浏览器运行

技术分享图片

在终端打印出

技术分享图片

 

 

 

 现在用第二种方式整合filter

 新建SecndFilter类

技术分享图片

技术分享图片

 

 新建SecndServlet类

技术分享图片

 

SecondServlet.java

package com.example.servlet;

import java.io.IOException;

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

/**
 *SpringBoot整合Servlet方式二
 *
 */

public class SecondServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("SecondServlet..........");
    }
    
}

 

 

新建启动类App2.java

技术分享图片

 

 App2.java类

package com.example;

import com.example.filter.SecondFilter;
import com.example.servlet.SecondServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

/**
 * SpringBoot 整合 Filter 方式二
 * * *
 */
@SpringBootApplication
public class App2 {
    public  static void main(String[] args){
        SpringApplication.run(App2.class,args);
    }

    /**
     * 注册 Servlet
     * @return
     */
    @Bean
    public ServletRegistrationBean getServletRegistrationBean(){
        ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
        bean.addUrlMappings("/second");
        return bean;
    }
    
    /**
     * 注册 Filter
    */
 @Bean

public FilterRegistrationBean getFilterRegistrationBean() { FilterRegistrationBean bean = new FilterRegistrationBean(new SecondFilter()); //bean.addUrlPatterns(new String[]{"*.do","*.jsp"}); bean.addUrlPatterns("/second"); return bean; } }

 

 运行App2.java类

技术分享图片

 

 技术分享图片

 

 在浏览器输入

技术分享图片

 

可以看到终端打印出的结果

 技术分享图片

 

springboot整合filter

原文:https://www.cnblogs.com/braveym/p/11298820.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!