簡単な例のSpringBootを使用してサーブレット

1コメント方法(シンプル)

@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///////");
    }
}

付加されたパケット走査経路は、実行時にメインクラスを入口@ServletComponentScan

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

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

}

クラスの管理を配置することにより、図2は、サーブレットオブジェクト

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クラス:

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

    @Bean   //表示返回一个对象
    public ServletRegistrationBean getServletBean(){
        //第一个参数为对应servlet第二个参数为对应的访问路径
        return new ServletRegistrationBean(new HeServlet(),"/heServlet");
    }
}
公開された27元の記事 ウォンの賞賛1 ビュー851

おすすめ

転載: blog.csdn.net/weixin_44971379/article/details/104884130
おすすめ