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

相关推荐
Rain的Java大神之路8 小时前
Spring Boot 3 升级实战:真正吃掉工期的,是 MyBatis-Plus 坐标和 Security 6 重写
java·后端·面试
源码_空间8 小时前
基于Springboot在线考试系统(源码+文档)
java·spring boot·后端
AI开源软件9 小时前
用MQTT跑通一台模拟设备:看懂IoT产品、物模型和告警
java·vue.js·spring boot·物联网·mqtt·iot·企业管理
geovindu9 小时前
python: PCA
开发语言·后端·python·算法
冰美式只要糖12 小时前
Prefill 长短请求混批,为什么 128 Token 也要等 2.58 秒?
后端
不甘平凡的小鸟12 小时前
vs code 打开spring boot项目
java·spring boot·后端
忆想不到的晖12 小时前
删掉 1800 行编排代码后,我用 Agent-Native + 薄 AOP 重写了内容工作台
后端·node.js·agent
冰美式只要糖12 小时前
Transformers 静态 KV Cache 更新更快,为什么 Decode 反而变慢?
后端
Penge66612 小时前
职场生存法则:识别服从性测试,建立自己的边界感
后端
想不到一个好的ID13 小时前
基于 Redis 的秒杀系统架构设计方案
后端