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

相关推荐
Song_RsHt18 分钟前
Redis 与微服务,什么场景下无可替代
java·redis·后端·学习·微服务
学长毕设38 分钟前
基于SpringBoot的建筑材料管理系统-附源码
java·spring boot·后端
程序员cxuan41 分钟前
掀桌子了!Kimi K3.1 Pro 这效果也太离谱了。
人工智能·后端·程序员
摸爬滚打的小李1 小时前
关于 Arthas
java·后端
武子康1 小时前
黑图、彩色噪声、NaN、偏色怎么排查
人工智能·后端·agent
苏三说技术1 小时前
企业智能知识库 Python 后端实战:FastAPI + RAG + KAG 企业级 AI 项目
后端
Mk598781 小时前
从跑通到上生产:AgentScope-Java 六篇实战收官盘点
后端
彭亚川Allen2 小时前
从零手搓 RAG 之 Embedding 技术
前端·后端·面试
星光开发者2 小时前
基于springboot智慧草场牛羊管理系统-计算机毕设【课程设计】50360
vue.js·spring boot·后端·mysql·php·课程设计·express
weixin_BYSJ19872 小时前
电竞先锋网站---附源码02032
java·vue.js·spring boot·python·django·tomcat·php