在springboot项目中创建一个自定义的Servlet 继承HttpServlet
加入这个@WebServlet注解,访问的就是my这个路径
注意my前面一定要加/,否则启动报错
@WebServlet(urlPatterns = "/my")
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("123456");
}
}
我们访问my 发现404
![](https://img-blog.csdnimg.cn/img_convert/869573cd4fd50ec048e704a52971af21.png)
还需要在启动类加入@ServletComponentScan注解,扫描那个包
![](https://img-blog.csdnimg.cn/img_convert/9effb7622a5ea1fa6cc6151d7598db16.png)
这次访问就有了
![](https://img-blog.csdnimg.cn/img_convert/d1fe4d3086ddb5c49cef54b13c26109a.png)
创建一个过滤器,使用@WebFilter注解,拦截所有请求
在项目启动的时候,调用初始化
@Slf4j
@WebFilter(urlPatterns = {"/*"})
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
log.info("MyFilter初始化");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
log.info("MyFilter 处理业务");
//往下一个过滤器走
chain.doFilter(request, response);
}
@Override
public void destroy() {
log.info("MyFilter销毁");
}
}
当我们访问http://localhost:8080/my的时候,进行过滤器处理
![](https://img-blog.csdnimg.cn/img_convert/daf84635c4e2e9f38e799b1738853aa5.png)
创建监听器,使用@WebListener注解
@Slf4j
@WebListener
public class MyServletContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
log.info("MyServletContextListener监听到项目初始化完成");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
log.info("MyServletContextListener监听到项目销毁完成");
}
}
![](https://img-blog.csdnimg.cn/img_convert/9301b32c27b2bd4513ca7ad6d39dd622.png)
我们还有另一种注入的方式,先把那三个webxxx注解去掉
使用RegistrationBean的方式注入
@Configuration
public class TestConfig {
//注入servlet
@Bean
public ServletRegistrationBean servletRegistrationBean(){
MyServlet myServlet=new MyServlet();
return new ServletRegistrationBean(myServlet,"/my");
}
//注入过滤器
@Bean
public FilterRegistrationBean filterRegistrationBean(){
MyFilter myFilter=new MyFilter();
FilterRegistrationBean filterRegistrationBean=new FilterRegistrationBean();
filterRegistrationBean.setFilter(myFilter);
filterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
return filterRegistrationBean;
}
//注入监听器
@Bean
public ServletListenerRegistrationBean servletListenerRegistrationBean(){
ServletListenerRegistrationBean servletListenerRegistrationBean=new ServletListenerRegistrationBean();
servletListenerRegistrationBean.setListener(new MyServletContextListener());
return servletListenerRegistrationBean;
}
}
我们进入DispatcherServletAutoConfiguration类
可以看到在这里注入的DispatcherServlet,和webmvc的属性进行了绑定
![](https://img-blog.csdnimg.cn/img_convert/28bfc9ea4cf04ab891ca6173a3ed6589.png)
进入WebMvcProperties类,看到prefix = "spring.mvc"
我们在Yml文件对springmvc的一些属性的配置,都是来自这个类
![](https://img-blog.csdnimg.cn/img_convert/b3cd45d633e13ab7549f9411117183f2.png)
我们在看下DispatcherServletRegistrationConfiguration类
注册DispatcherServlet配置
进入webMvcProperties.getServlet().getPath()
![](https://img-blog.csdnimg.cn/img_convert/2796a9574112a1e2094b349b2fd83b04.png)
这个path就是servlet的路径,默认是/,我们可以在yml文件去修改
![](https://img-blog.csdnimg.cn/img_convert/2fa4da6d870beed2b2195b8d208a80a1.png)
spring:
mvc:
servlet:
path: /res
浏览器输入http://localhost:8080/res/
注意res后面还有个/,进入index.html界面
![](https://img-blog.csdnimg.cn/img_convert/606f14942fd967edc7eb76f248f74306.png)