话不多说 直接上代码
首先是自己创建的一个servlet
package com.gm.servlet;
import org.springframework.context.annotation.Configuration;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(urlPatterns = "/myServlet")
public class TestServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("----get请求");
super.doGet(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("-----post请求");
super.doPost(req, resp);
}
@Override
public void init() throws ServletException {
System.out.println("-----初始化成功");
super.init();
}
}
然后注意 需要在启动类设置@ServletComponentScan 这样才能扫描到我们创建的servlet
@SpringBootApplication
@ServletComponentScan //启动器启动时,扫描本目录以及子目录带有的webservlet注解的
public class WaterPurifierApplication {
public static void main(String[] args) {
SpringApplication.run(WaterPurifierApplication.class, args);
}
}
测试。
ok!!!