SpringBoot3 配置嵌入Servlet容器

1. 完整的正确配置

1.1 主应用类配置

java 复制代码
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication
// 扫描@WebServlet, @WebFilter, @WebListener注解
@ServletComponentScan  
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

1.2 自定义Servlet类

java 复制代码
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
 * 注册Servlet 三大组件: Servlet  Filter Listener
 */

// 方式1:指定具体的URL模式
@WebServlet(name = "myServlet", urlPatterns = "/servlet")
// 方式2:或者使用value属性(和urlPatterns等价)
// @WebServlet("/servlet")
public class MyServlet extends HttpServlet {
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp );
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("Hello MyServlet");
        System.out.println("name: " + req.getParameter("name"));
    }
}

1.3 自定义Filter类

java 复制代码
/**
 * 注册Servlet 三大组件: Servlet  Filter Listener
 */
@WebFilter("/")
public class MyFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("MyFilter--init");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("myFilter--doFilter");
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {
        System.out.println("MyFilter--destroy");
    }
}

1.4 自定义Listener类

java 复制代码
/**
 * 注册Servlet 三大组件: Servlet  Filter Listener
 */
@WebListener
public class MyListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        System.out.println("web项目启动了。。。");
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        System.out.println("web项目销毁了。。。");
    }
}

2. 常见问题及解决方案

问题1:Servlet没有被正确扫描

解决方案

java 复制代码
// 明确指定扫描的包路径
@ServletComponentScan(basePackages = "com.example.servlet")
// 或者
@ServletComponentScan(basePackageClasses = {MyServlet.class})

问题2:Spring Boot版本问题

在Springboot应用中 , @WebServlet("/servlet")

http://localhost:8081/servlet?name=lili 404 (Not Found)

对于Spring Boot 2.x+,确保使用正确的依赖:

pom.xml:

XML 复制代码
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <scope>provided</scope>
</dependency>

对于Spring Boot 3.x+(使用Jakarta EE):

XML 复制代码
<dependency>
    <groupId>jakarta.servlet</groupId>
    <artifactId>jakarta.servlet-api</artifactId>
    <scope>provided</scope>
</dependency>
相应的Servlet类:  // Spring Boot 3.x使用Jakarta
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

问题3:端口配置错误

检查application.properties或application.yml:

java 复制代码
# application.properties
server.port=8081

问题4:DispatcherServlet路径冲突

如果Spring MVC的DispatcherServlet映射到了/,可能会影响自定义Servlet。

解决方案:修改Servlet的URL模式:

java 复制代码
// 使用不同的路径
@WebServlet("/api/servlet")
// 或者使用路径前缀
@WebServlet("/custom/*")
相关推荐
猫头虎6 小时前
如何排查并解决项目启动时报错Error encountered while processing: java.io.IOException: closed 的问题
java·开发语言·jvm·spring boot·python·开源·maven
MZ_ZXD0018 小时前
springboot旅游信息管理系统-计算机毕业设计源码21675
java·c++·vue.js·spring boot·python·django·php
invicinble9 小时前
springboot的核心实现机制原理
java·spring boot·后端
space62123279 小时前
在SpringBoot项目中集成MongoDB
spring boot·后端·mongodb
金牌归来发现妻女流落街头10 小时前
【从SpringBoot到SpringCloud】
java·spring boot·spring cloud
皮卡丘不断更11 小时前
手搓本地 RAG:我用 Python 和 Spring Boot 给 AI 装上了“实时代码监控”
人工智能·spring boot·python·ai编程
lucky670711 小时前
Spring Boot集成Kafka:最佳实践与详细指南
spring boot·kafka·linq
Coder_Boy_11 小时前
基于Spring AI的分布式在线考试系统-事件处理架构实现方案
人工智能·spring boot·分布式·spring
毕设源码-钟学长13 小时前
【开题答辩全过程】以 基于Springboot的扶贫众筹平台为例,包含答辩的问题和答案
java·spring boot·后端