本文介绍在springboot中使用servlet原生相关功能的方式,主要有两种:
@WebServlet
,@WebFilter
,@WebListner
等注解,需要注意,这些注意是在servlet3.0为了实现无web.xm(注意:这是个人根据学习的猜测!!!)
正式引入的,因此想要使用的话必须是servlet3.0及以上版本
。springboot的XXXBean
完成相关组件的注册。好的,那我们就正式开始吧!!!
idea
。maven
。jdk8
。2.2.10.RELEASE
。选择左侧菜单中的Spring Initilizr
,starter选择default
。
根据个人录入GAV
,选择java8
,项目构建为`jar,等。
主要选择web下spring-web。springboot版本选择2.2.10
。
项目名称根据个人决定是否修改,正常不用管,和ArtifactId
保持一致。
@WebServlet("/user/servlet")
public class MyAnnotationServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.getWriter().write("user servlet");
}
}
@WebFilter("/user/servlet")
public class MyAnnotationFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain filterChain) throws IOException, ServletException {
System.out.println("income log filter " + servletRequest.getRemoteHost());
filterChain.doFilter(servletRequest, servletResponse);
}
}
@WebListener
public class MyAnnotationServletContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("app startup at " + LocalDateTime.now().toString());
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
}
@ServletComponentScan
注解@SpringBootApplication
@ServletComponentScan
public class SpringbootTestservletApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootTestservletApplication.class, args);
}
}
2020-09-29 15:36:38.732 INFO 4720 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1957 ms
app startup at 2020-09-29T15:36:38.791
public class MyBeanConfigurationServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.getWriter().write("this is springboot servlet by bean configuration");
}
}
public class MyBeanConfigurationFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain filterChain) throws IOException, ServletException {
System.out.println("spring boot filter by bean configuration");
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void init(FilterConfig filterConfig) {
}
@Override
public void destroy() {
}
}
public class MyBeanConfigurationServletContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("springboot servlet context listener by bean configuration started!");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
}
@SpringBootConfiguration
public class MyBeanConfiguration {
// 注册servlet
@Bean
public ServletRegistrationBean createMyBeanConfigurationServlet() {
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(
new MyBeanConfigurationServlet(), "/book.do");
return servletRegistrationBean;
}
// 注册filter
@Bean
public FilterRegistrationBean createMyBeanConfigurationFilter() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(new MyBeanConfigurationFilter());
filterRegistrationBean.setUrlPatterns(Arrays.asList("/book.do"));
return filterRegistrationBean;
}
// 注册监听器
@Bean
public ServletListenerRegistrationBean createMyBeanConfigurationServletContextListener() {
ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean(new MyBeanConfigurationServletContextListener());
return servletListenerRegistrationBean;
}
}
2020-09-29 15:57:18.374 INFO 18772 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1891 ms
springboot servlet context listener by bean configuration started!
点击这里。