注册Servlet
/**
* 自定义Servlet组件
*/
public class HelloServlet extends HttpServlet {
//快捷键 ctrl+o
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("helloServlet seccess...");
}
}
//注册Serlet组件
@Configuration
public class MyServletConfig {
@Bean
public ServletRegistrationBean helloServlet(){
// 注册自己自定义组件
ServletRegistrationBean<HelloServlet> bean = new ServletRegistrationBean<>(new HelloServlet(), "/hello");
//设置servlet组件参数配置,如下面加载顺序
bean.setLoadOnStartup(1);
return bean;
}
}
注册Servlet三大组件Servlet/Filter/Listener