Servlet using simple example SpringBoot

1 comment manner (simple)

@WebServlet(name = "MyServlet",urlPatterns = "/rentao")
public class MyServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().print("rentao///////");
    }
}

Added packet scan path inlet main class at run @ServletComponentScan

@SpringBootApplication
@ServletComponentScan(basePackages = "com.qingnian.springboot.servlet")
public class SpringbootApplication {

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

}

2 by arranging the class management objects Servlet

public class HeServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().print("xinba ======");

    }
}

Configuration class:

@Configuration  //等价于Spring的xml文件
public class WebConfig implements WebMvcConfigurer {

    @Bean   //表示返回一个对象
    public ServletRegistrationBean getServletBean(){
        //第一个参数为对应servlet第二个参数为对应的访问路径
        return new ServletRegistrationBean(new HeServlet(),"/heServlet");
    }
}
Published 27 original articles · won praise 1 · views 851

Guess you like

Origin blog.csdn.net/weixin_44971379/article/details/104884130