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

相关推荐
张一凡9312 小时前
给 Coding Agent 加一道"架构闸门":Jin 是如何做到"先审架构、再写代码、不漂移"的
前端·后端·agent
Jvav练习生12 小时前
AI Agent 总在关键时刻“失忆”翻车?一文讲透上下文工程(附可运行 Python 代码)
后端
leo在掘金12 小时前
只挂了11小时,5000台开发机被塞进SSH公钥
后端·架构
xjz184212 小时前
Rust 运算符优先级:括号不只用来改变顺序
后端
写码跑山的老艾12 小时前
为什么有了 MySQL,还要 Redis 和 Elasticsearch?从一件商品看数据系统的权衡
后端
对象存储与RustFS13 小时前
Jenkins 的构建产物别堆在控制器磁盘:用 RustFS 当它的 S3 工件仓库
后端·docker·云原生
王中阳Go13 小时前
100 件货卖出 116 件,库存停在 -16
后端·mysql·go
泡海椒13 小时前
jquick-pdf 多重折线(JMultiLineChartData)实战:Java 生成工单 SLA 月报
后端
Swot13 小时前
SvelteKit Remote Functions 使用指南
前端·后端