ITPub博客

首页 > IT职业 > IT职场 > SpringBoot中使用Servlet

SpringBoot中使用Servlet

IT职场 作者:hu261 时间:2021-09-09 09:06:53 0 删除 编辑

1.在入口Application类上加入注解@ServletComponentScan

@SpringBootApplication
@ServletComponentScan
public class UseServletInSpringbootApplication {

    public static void main(String[] args) {
        SpringApplication.run(UseServletInSpringbootApplication.class, args);
    }
}

2.新建Servlet类,继承HttpServlet并且加入注解 @WebServlet(name="TestServlet",urlPatterns="/test")

@WebServlet(name="TestServlet",urlPatterns="/test")
public class TestServlet extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        System.out.println("doGet");
    }
}

3.之后运行代码,在地址栏输入 http://localhost:8080/test,若看到控制台打印doGet,则表示Servlet创建成功。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3112/viewspace-2799593/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-30

  • 博文量
    77
  • 访问量
    762