springboot2.0 使用servlet 、filter、listener

使用servlet、filter、listener、采用在主配置类中注册类的方式,用@Bean注解

package com.springboot2.thyemleaf;

import com.springboot2.thyemleaf.fileter.MyFilter;
import com.springboot2.thyemleaf.fileter.MyFilter2;
import com.springboot2.thyemleaf.lisener.Mylistener;
import com.springboot2.thyemleaf.servlet.MyServlet;
import com.springboot2.thyemleaf.servlet.MyServlet2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class ThyemleafdemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(ThyemleafdemoApplication.class, args);
	}


	// 在主类采用bean方法注册
	@Bean
	public ServletRegistrationBean getServlet1(){
		return new ServletRegistrationBean(new MyServlet(),"/my1");
	}

	@Bean
	public ServletRegistrationBean getServlet2(){
		return new ServletRegistrationBean(new MyServlet2(),"/my2");
	}

	@Bean
	public FilterRegistrationBean getFilter(){
		//过滤所有请求
//		return new FilterRegistrationBean(new MyFilter());
		// 过滤指定的servlet
		return new FilterRegistrationBean(new MyFilter(),getServlet1());
	}

	@Bean
	public FilterRegistrationBean getFilter2(){
		// 过滤多个指定的servlet
		return new FilterRegistrationBean(new MyFilter2(),getServlet2(),getServlet1());

	}

	@Bean
	public ServletListenerRegistrationBean getListener(){
		return new ServletListenerRegistrationBean<>(new Mylistener());
	}

}

springboot2.0 使用servlet 、filter、listener_第1张图片

类:servlet

package com.springboot2.thyemleaf.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by  lpw'ASUS on 2018/5/28.
 */
public class MyServlet extends HttpServlet {

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

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            System.out.println("这是自己写的servlet");
    }
}

filter:

package com.springboot2.thyemleaf.fileter;

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

/**
 * Created by  lpw'ASUS on 2018/5/28.
 */
public class MyFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            System.out.println("这是我的拦截器------");
            filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}

listener:

package com.springboot2.thyemleaf.lisener;


import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * Created by  lpw'ASUS on 2018/5/29.
 */
public class Mylistener implements HttpSessionListener{


    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {
        System.out.println("MyListener sessionCreated-----");
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
        System.out.println("MyListener sessionDestroyed-----");
    }
}
listener,监听的是servletSessionListener,所以创建、销毁session才会启用。

2. 通过实现

主配置类,实现servletContextInitializer,重写onStartup。


@Override
	public void onStartup(ServletContext servletContext) throws ServletException {
		servletContext.addServlet("my1",MyServlet.class).addMapping("/my1");
		servletContext.addFilter("myfilter1",MyFilter.class).addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST),true,"/*");
		servletContext.addListener(Mylistener.class);
	}

3.通过注解

在主配置配上加@ServletCompanentScan注解

package com.springboot2.thyemleaf;

import com.springboot2.thyemleaf.fileter.MyFilter;
import com.springboot2.thyemleaf.fileter.MyFilter2;
import com.springboot2.thyemleaf.lisener.Mylistener;
import com.springboot2.thyemleaf.servlet.MyServlet;
import com.springboot2.thyemleaf.servlet.MyServlet2;
import org.apache.tomcat.util.net.DispatchType;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.*;
import org.springframework.context.annotation.Bean;

import javax.servlet.DispatcherType;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.xml.ws.Dispatch;
import java.util.EnumSet;

@SpringBootApplication
@ServletComponentScan
public class ThyemleafdemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(ThyemleafdemoApplication.class, args);
	}
}

在servlet上加@Webservlet注解

package com.springboot2.thyemleaf.servlet;

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

/**
 * Created by  lpw'ASUS on 2018/5/28.
 */
@WebServlet(urlPatterns = "/my1")
public class MyServlet extends HttpServlet {

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

        System.out.println("这是自己写的servlet   get");
        req.getSession().setAttribute("mydemo","张三");
    }

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

            System.out.println("这是自己写的servlet");
        req.getSession().setAttribute("mydemo","张三");
    }


}

在filter上加@WebFilter注解

package com.springboot2.thyemleaf.fileter;

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

/**
 * Created by  lpw'ASUS on 2018/5/28.
 */
@WebFilter(urlPatterns = "/*")
public class MyFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            System.out.println("这是我的拦截器------");
            filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}

在listener上加@WebListener注解

package com.springboot2.thyemleaf.lisener;


import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * Created by  lpw'ASUS on 2018/5/29.
 */
@WebListener
public class Mylistener implements HttpSessionListener{


    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {
        System.out.println("MyListener sessionCreated-----");
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
        System.out.println("MyListener sessionDestroyed-----");
    }
}
第三种最简单,好用

你可能感兴趣的:(springboot,filter,servlet,listener)