本文共 4119 字,大约阅读时间需要 13 分钟。
springBoot整合servlet、Filter、Listener需要在pom中引入如下包:
org.springframework.boot spring-boot-starter-web
一、springBoot整合servlet
springBoot使用servlet的API有两种方法
方法1、使用@ServletComponentScan注解
package com.wzy.test;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.web.servlet.ServletComponentScan;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 = "/myServlet")//上面这段配置声明该类为servlet程序,等同于在web.xml中配置映射@ServletComponentScan //该注解的作用是让springBoot扫描@webServlet等注解@SpringBootApplication //声明为springBoot应用public class MyFirstServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("执行了MyFirstServlet的doGet方法......"); } //启动类 public static void main(String[] args) { SpringApplication.run(MyFirstServlet.class,args); }}
测试:浏览器中访问http://localhost:8080/myServlet,控制台打印结果如下:
方法2、使用@Bean注解
第1步、写一个HelloServlet类,继承HttpServlet类
package com.wzy.test;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;public class HelloServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("执行了HelloServlet的doGet方法......"); }}
第2步
package com.wzy.test;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.web.servlet.ServletRegistrationBean;import org.springframework.context.annotation.Bean;@SpringBootApplicationpublic class BeanTest { public static void main(String[] args) { SpringApplication.run(BeanTest.class,args); } //注册servlet @Bean public ServletRegistrationBean getServletRegistrationBean(){ ServletRegistrationBean bean=new ServletRegistrationBean(new HelloServlet()); //设置访问路径 bean.addUrlMappings("/helloServlet"); return bean; }}
第3步、测试
浏览器输入:http://localhost:8080/helloServlet
控制台打印结果如下:
二、springBoot整合Filter
使用@WebFilter注解
package com.wzy.test;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.web.servlet.ServletComponentScan;import javax.servlet.*;import javax.servlet.annotation.WebFilter;import java.io.IOException;@WebFilter(filterName = "myFilter",urlPatterns = "/myFilter")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("执行了前面代码"); //放行,执行目标资源:/myFilter filterChain.doFilter(servletRequest,servletResponse); System.out.println("执行了后面代码"); } /** * 销毁 * */ @Override public void destroy() { } public static void main(String[] args) { SpringApplication.run(MyFilter.class,args); }}
三、springBoot整合Listener
使用@WebListener注解
package com.wzy.test;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;import javax.servlet.annotation.WebListener;@WebListenerpublic class MyListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("servletContext对象创建了"); } @Override public void contextDestroyed(ServletContextEvent sce) { System.out.println("servletContext对象被销毁了"); }}
转载地址:http://fmuii.baihongyu.com/