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创建成功。
作者:蜗牛__
链接:https://www.imooc.com/article/20930?block_id=tuijian_wz
来源:慕课网