在SpringBoot中,有两种方式来自定义Servlet
不管是方式一还是方式二,都需要将自定义的Servlet继承自类HttpServlet
。我们所写的自定义类需要使用到注解@WebServlet
,在这个注解中,通过配置urlPatterns
来确定自定义Servlet的控制范围。
MyServlet.java
package com.boot.servlet.api.bootservlet.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;
@WebServlet(name = "MyServlet", urlPatterns = "/my")
public class MyServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("执行力servlet请求");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("执行力servlet请求");
super.doGet(req, resp);
}
}
在启动类中,除了正常使用的@SpringBootApplication之外,还需要通过注解@ServletComponentScan
来扫描自定义Servlet。
启动类
package com.boot.servlet.api.bootservlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
// 方式1
@ServletComponentScan
@SpringBootApplication
public class BootServletApplication {
public static void main(String[] args) {
SpringApplication.run(BootServletApplication.class, args);
}
}
与方式一不同,在通过配置类进行配置的时候,自定义Servlet中可以将方式一种原本使用的注解@WebServlet删除,用配置类取而代之。
配置类
@SpringBootConfiguration
public class ServletConfiguration {
@Bean
public ServletRegistrationBean<MyServlet> servletRegistrationBean() {
ServletRegistrationBean<MyServlet> bean = new ServletRegistrationBean<>(new MyServlet(), "/my");
return bean;
}
同样的,在启动项中,只需要将方式一种的注解@ServletComponentScan
删除即可
同样也和自定义Servlet一样,也有两种方式
自定义的Listener可以继承三个接口(根据自己需要选择即可),分别是ServletContextListener
, HttpSessionListener
, HttpSessionAttributeListener
,注解也由@WebServlet
变为了@WebServlet
MyListener.java
package com.boot.servlet.api.bootservlet.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionListener;
@WebListener
public class MyListener implements ServletContextListener, HttpSessionListener, HttpSessionAttributeListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("Servlet容器启动");
}
}
启动类也同样需要注解@ServletComponentScan
与配置自定义Servlet类似,只是返回的ServletRegistrationBean
变为了ServletListenerRegistrationBean
@Bean
public ServletListenerRegistrationBean<MyListener> listenerServletListenerRegistrationBean() {
ServletListenerRegistrationBean<MyListener> bean = new ServletListenerRegistrationBean<>();
bean.setListener(new MyListener());
return bean;
}
在启动项中,只需要将方式一种的注解@ServletComponentScan
删除即可
与自定义Listener类似,自定义Filter需要实现接口Filter
使用注解@WebFilter
MyFilter.java
package com.boot.servlet.api.bootservlet.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter(filterName = "myFilter", 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("执行了filter");
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() {
}
}
同样的,urlPatterns来定义Filter的作用范围,启动类和自定义Listener相同,都需要注解@ServletComponentScan
与配置自定义Listener类似,只是返回的ServletListenerRegistrationBean
变为了FilterRegistrationBean
@Bean
public FilterRegistrationBean<MyFilter> filterRegistrationBean() {
FilterRegistrationBean<MyFilter> bean = new FilterRegistrationBean();
bean.setFilter(new MyFilter());
bean.setUrlPatterns(Arrays.asList("/*"));
return bean;
}
在启动项中,只需要将方式一种的注解@ServletComponentScan
删除即可