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