Spring Boot 内嵌servlet容器的Servlets +Filters + listeners

时间:2022-02-03 18:29:28

一:servlet

(1)启动类

@SpringBootApplication
@ServletComponentScan
public class Application {
public static void main(String[] args){
SpringApplication.run(Application.
class, args);
}
}

启动类用@ServletComponentScan注解标注

(2)继承HttpServlet

@WebServlet(name = "webServlet", urlPatterns = "/webServlet", loadOnStartup = 2)
public class WebServletTest extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println(
"########doGet###########");
System.out.println(
"WebServletTest.doGet");
}

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

(3)访问http://localhost:8080/webServlet    运行结果:

Spring Boot  内嵌servlet容器的Servlets +Filters + listeners

 

 二:Filters

测试示例1

@WebFilter(urlPatterns = "/*",filterName="CharacterEncodeFilter",
initParams
={
@WebInitParam(name
="encoding",value="UTF-8"),
@WebInitParam(name
= "forceEncoding", value = "true")
})
@Singleton
public class CharacterEncodingFilter implements Filter {
private String encoding = "UTF-8";
private boolean forceEncoding = true;

@Override
public void init(FilterConfig filterConfig) throws ServletException {
this.encoding = filterConfig.getInitParameter("encoding");
String force
= filterConfig.getInitParameter("forceEncoding");
this.forceEncoding = (force == null) || Boolean.valueOf(force);
}

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
if (this.forceEncoding || servletRequest.getCharacterEncoding() == null) {
servletRequest.setCharacterEncoding(
this.encoding);
servletResponse.setCharacterEncoding(
this.encoding);
}
filterChain.doFilter(servletRequest, servletResponse);
}

@Override
public void destroy() {

}
}

测试示例2

@WebFilter(urlPatterns = "/*", filterName = "indexFilter")
public class IndexFilter implements Filter {
private final static Logger logger = LoggerFactory.getLogger(IndexFilter.class);

@Override
public void init(FilterConfig filterConfig) throws ServletException {
logger.info(
"init IndexFilter");
}

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
logger.info(
"IndexFilter.doFilter exec");
filterChain.doFilter(servletRequest,servletResponse);

}

@Override
public void destroy() {

}
}

 

三: listeners

示例

@WebListener
public class MyServletContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println(
"ServletContex初始化");
}

@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
System.out.println(
"ServletContex销毁");
}
}

运行结果:

Spring Boot  内嵌servlet容器的Servlets +Filters + listeners