多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] # Servlet ~~~ import lombok.extern.slf4j.Slf4j; 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; import java.io.PrintWriter; /** * @WebServlet(urlPatterns = "/myservlet") // 定义访问路径 */ @Slf4j @WebServlet(urlPatterns = "/myservlet") public class MyServlet extends HttpServlet { @Override public void init() throws ServletException { log.info("Servlet 开始初始化"); super.init(); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { log.info("Servlet 开始处理 GET 方法"); PrintWriter writer = resp.getWriter(); writer.println("Hello Servlet"); writer.flush(); writer.close(); } @Override protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } @Override public void destroy() { log.info("Servlet 开始销毁"); super.destroy(); } } ~~~ # Filter ~~~ import lombok.extern.slf4j.Slf4j; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException; @Slf4j @WebFilter(urlPatterns = "/*") public class MyFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException { log.info("拦截器开始拦截"); filterChain.doFilter(request, response); } } ~~~ # Listener ~~~ import lombok.extern.slf4j.Slf4j; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; @Slf4j @WebListener public class MyListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { log.info("监听器开始初始化"); } @Override public void contextDestroyed(ServletContextEvent sce) { log.info("监听器开始销毁"); } } ~~~ # 添加到容器 添加到容器有两种方式,第一种使用注解扫描 ~~~ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.context.annotation.ComponentScan; /** * @ServletComponentScan 扫描Servlet,Filter,Listener 添加到容器 */ @SpringBootApplication @ServletComponentScan public class BootApplication { public static void main(String[] args) { SpringApplication.run(BootApplication.class, args); } } ~~~ 或者使用配置类想容器中添加。 ~~~ /** * 在这里注册Servlet Filter Listener 或者使用 @ServletComponentScan */ @Configuration public class WebCoreConfig { @Bean public ServletRegistrationBean myServlet() { return new ServletRegistrationBean<>(new MyServlet()); } @Bean public FilterRegistrationBean myFitler() { return new FilterRegistrationBean<>(new MyFilter()); } @Bean public ServletListenerRegistrationBean myListener() { return new ServletListenerRegistrationBean(new MyListener()); } } ~~~ 启动可以在控制台看到监听器启动。 ~~~ 11:35:03.744 INFO 8616 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1364 ms 11:35:03.798 INFO 8616 --- [ main] net.codingme.boot.listener.MyListener : 监听器开始初始化 11:35:03.892 INFO 8616 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 11:35:04.055 INFO 8616 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' ~~~ 访问 Servlet 可以看到拦截器和 Servlet 生效。 ~~~ 11:36:55.552 INFO 3760 --- [nio-8080-exec-1] net.codingme.boot.servlet.MyServlet : Servlet 开始初始化 11:36:55.556 INFO 3760 --- [nio-8080-exec-1] net.codingme.boot.filter.MyFilter : 拦截器开始拦截 11:36:55.556 INFO 3760 --- [nio-8080-exec-1] net.codingme.boot.servlet.MyServlet : Servlet 开始处理 GET 方法 ~~~