Spring Boot基础教程——web 应用开发-Servlets, Filters, listeners
Spring Boot基础教程——web 应用开发-Servlets, Filters, listeners
哎小艾 发表于5个月前
Spring Boot基础教程——web 应用开发-Servlets, Filters, listeners
  • 发表于 5个月前
  • 阅读 17
  • 收藏 1
  • 点赞 0
  • 评论 0

腾讯云 学生专属云服务套餐 10元起购>>>   

一、Web 开发使用 Controller 基本上可以完成大部分需求, 但是我们还可能会用到 Servlet、 Filter、Listener 等等。


二、在 spring boot 中的三种实现方式
方法一: 通过注册 ServletRegistrationBean、 FilterRegistrationBean 和ServletListenerRegistrationBean 获得控制。

1、自定义servlet

/**
* 自定义 servlet
*
* @author wujing
*/
public class CustomServlet extends HttpServlet {
   /**
   *
   */
   private static final long serialVersionUID = 1L;

   @Override
   protected void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
       System.out.println("servlet get method");
       doPost(request, response);
   }

   @Override
   protected void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
       System.out.println("servlet post method");
       response.getWriter().write("hello world");
   }
}

2、自定义filter

/**
   * 自定义 filter
   *
   * @author wujing
   */
public class CustomFilter implements Filter {
   @Override
   public void init(FilterConfig filterConfig) throws ServletException {
      System.out.println("init filter");
  }

  @Override
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
  throws IOException, ServletException {
     System.out.println("do filter");
  chain.doFilter(request, response);
  }

  @Override
  public void destroy() {
     System.out.println("destroy filter");
  }
}

3、自定义listener

/**
 * 自定义 listener
 *
 * @author wujing
 */
public class CustomListener implements ServletContextListener {
 @Override
 public void contextInitialized(ServletContextEvent sce) {
    System.out.println("contextInitialized");
 }

 @Override
 public void contextDestroyed(ServletContextEvent sce) {
    System.out.println("contextDestroyed");
 }
}

4、在启动类里面注册bean

@Bean
public ServletRegistrationBean servletRegistrationBean() {
   return new ServletRegistrationBean(new CustomServlet(), "/roncoo");
}

@Bean
public FilterRegistrationBean filterRegistrationBean() {
   //return new FilterRegistrationBean(new CustomFilter(), servletRegistrationBean());表示仅对/roncoo进行拦截;return new FilterRegistrationBean(new CustomFilter());表示对所有请求进行拦截
   return new FilterRegistrationBean(new CustomFilter(), servletRegistrationBean());
}

@Bean
public ServletListenerRegistrationBean<CustomListener> servletListenerRegistrationBean() {
   return new ServletListenerRegistrationBean<CustomListener>(new CustomListener());
}

方法二:启动类通过实现 ServletContextInitializer 接口直接注册

implements ServletContextInitializer {
   @Override
   public void onStartup(ServletContext servletContext) throws
   ServletException {
   servletContext.addServlet("customServlet", new CustomServlet()).addMapping("/roncoo");
   servletContext.addFilter("customFilter", new
   CustomFilter()).addMappingForServletNames(EnumSet.of(DispatcherType.REQUEST), true,     "customServlet");
   servletContext.addListener(new CustomListener());
}


方法三: 在 SpringBootApplication 上使用@ServletComponentScan 注解后,直接通过@WebServlet、@WebFilter、 @WebListener 注解自动注册

1、自定义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;

/**
 * 自定义servlet
 * 
 * @author wujing
 */
@WebServlet(name = "customServlet", urlPatterns = "/roncoo")
public class CustomServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("servlet get method");
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("servlet post method");
		response.getWriter().write("hello world");
		response.flushBuffer();
	}

}

2、自定义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;

/**
 * @author wujing
 */
// /*表示对所有请求进行拦截  /roncoo 表示仅对该请求拦截
@WebFilter(filterName = "customFilter", urlPatterns = "/*")
public class CustomFilter implements Filter {

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		System.out.println("init filter");
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		System.out.println("do filter");
		chain.doFilter(request, response);
	}

	@Override
	public void destroy() {
		System.out.println("destroy filter");
	}

}

3、自定义listener

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 * @author wujing
 */
@WebListener
public class CustomListener implements ServletContextListener {

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		System.out.println("contextInitialized");
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		System.out.println("contextDestroyed");
	}

}

4、启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@ServletComponentScan
@SpringBootApplication
public class SpringBootDemo103Application {

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

 

共有 人打赏支持
粉丝 5
博文 78
码字总数 31619
×
哎小艾
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: