Spring Boot中使用Servlet

一、注解方式

1.1 使用Servlet3注解方式写一个Servlet

java 复制代码
@WebServlet(urlPatterns = "/testServlet",initParams = {
        @WebInitParam(name="name",value="wno704")
})
public class TestServlet extends HttpServlet {
    private static final long serialVersionUID = -4316351735748478174L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        resp.getWriter().println("he " + name + " hello world");
        resp.getWriter().flush();
        resp.getWriter().close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

1.2 在启动类添加注解扫描servlet

java 复制代码
@ServletComponentScan(basePackages = "com.wno704.boot.servlet")

1.3 测试

启动项目,访问http://localhost:8080/testServlet?name=wno7042

二、通过SpringBoot配置类实现

2.1 编写一个普通servlet类

java 复制代码
public class HeServlet extends HttpServlet {
    private static final long serialVersionUID = -4316351735748478173L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().println("he servlet hello world");
        resp.getWriter().flush();
        resp.getWriter().close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

2.2 编写一个springboot配置类

java 复制代码
@Configuration
public class WebConfig {
    @Bean
    public ServletRegistrationBean heServletRegistrationBean() {
        ServletRegistrationBean registration = new ServletRegistrationBean(new HeServlet(),"/heServlet");
        return registration;
    }
}

2.3 测试

启动项目,访问http://localhost:8080/heServlet

三、动态注册

3.1 编写一个普通的Servlet类

java 复制代码
public class HeServlet extends HttpServlet {
    private static final long serialVersionUID = -4316351735748478173L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().println("he servlet hello world");
        resp.getWriter().flush();
        resp.getWriter().close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

3.2编写一个Servlet注册类

java 复制代码
@Component
public class ServletConfig implements ServletContextInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        ServletRegistration iniServlet = servletContext.addServlet("iniServlet", HeServlet.class);
        iniServlet.addMapping("/iniServlet");
        //iniServlet.setInitParameter("name","wno704");
    }
}

3.3 测试

启动项目,访问http://localhost:8080/iniServlet

相关推荐
明月_清风1 天前
Muse 登顶 App Store 第一,SDK 直接开源:AI Agent 开始进入下一个阶段
人工智能·后端
hsfxuebao1 天前
Loop Engineering 保姆级教程 + 项目实战
人工智能·后端
打工仔折腾 AI1 天前
从 Demo 到生产级 Agent:8 个关键设计机制与 Python 实现拆解
java·jvm·人工智能·后端·python·langchain·ai agent 实战
架构技术专栏1 天前
交叉熵:AI 怎样给概率预测打分
后端
code_slave(码畜)1 天前
微服务架构落地:基础服务 —— 报表服务(中篇:元数据、查询引擎、缓存与权限落地)
spring boot·spring cloud·缓存·微服务·架构
ZealSinger1 天前
Boot4挂起函数丢traceId怎么修
spring boot·kotlin·协程·可观测性
IT_陈寒1 天前
SpringBoot自动配置坑了我一把,原来是这样绕过去的
前端·人工智能·后端
code_slave(码畜)1 天前
微服务架构落地:基础服务 —— 报表服务(AI 集成篇:AI 增强报表能力)
人工智能·spring boot·spring cloud·微服务·架构
要努力啊4691 天前
用 Codex 加速 Java 开发:从代码生成到测试覆盖的完整实战
后端
dora1 天前
LangChain4j 新手入门实战教程(Java版)
后端·langchain·agent