方法1
1檐迟、新建一個(gè)類繼承HttpServlet類,實(shí)現(xiàn)相關(guān)方法鹃答,并加上@WebServlet注解;urlPatterns為請(qǐng)求路徑
@WebServlet(name = "FristServlet",urlPatterns = "/ ")
public class FristServlet extends HttpServlet {
? ? @Override
? ? protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
? ? ? ? System.out.println("FristServlet Runnint...");
? ? ? ? PrintWriter writer = resp.getWriter();
? ? ? ? writer.write("success...");
? ? ? ? writer.flush();
? ? ? ? writer.close();
? ? }
}
2、在SpringBoot啟動(dòng)類上加上@ServletComponentScan注解掃描Servlet
@SpringBootApplication
@EnableAspectJAutoProxy(proxyTargetClass = true)
@ServletComponentScan
public class BootDemoApplication {
? ? public static void main(String[] args) {
? ? ? ? SpringApplication springApplication = new SpringApplication(BootDemoApplication.class);
? ? ? ? springApplication.setBannerMode(Banner.Mode.OFF);
? ? ? ? ApplicationContext ac = springApplication.run(args);
? ? }
}
方法2
1振坚、新建一個(gè)類繼承HttpServlet類,實(shí)現(xiàn)相關(guān)方法
public class FristServlet extends HttpServlet {
? ? @Override
? ? protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
? ? ? ? System.out.println("FristServlet Runnint...");
? ? ? ? PrintWriter writer = resp.getWriter();
? ? ? ? writer.write("success...");
? ? ? ? writer.flush();
? ? ? ? writer.close();
? ? }
}
2皿曲、在SpringBoot啟動(dòng)類上加上@ServletComponentScan注解掃描Servlet状勤,并將Servlet注冊(cè)到容器
@SpringBootApplication
@EnableAspectJAutoProxy(proxyTargetClass = true)
@ServletComponentScan
public class BootDemoApplication {
? ? public static void main(String[] args) {
? ? ? ? SpringApplication springApplication = new SpringApplication(BootDemoApplication.class);
? ? ? ? springApplication.setBannerMode(Banner.Mode.OFF);
? ? ? ? ApplicationContext ac = springApplication.run(args);
? ? }
? ? @Bean
? ? public ServletRegistrationBean servletRegistrationBean(){
? ? ? ? ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new FristServlet());
? ? ? ? servletRegistrationBean.addUrlMappings("/frist");
? ? ? ? return servletRegistrationBean;
? ? }
}