Springboot注册Servlet

第一步:先写一个Sevlet配置类

public class ServletConfig extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("进入了doget方法");
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("进入了dopost方法");
	}

	
}

第二步:新建一个@Configuration注册该配置类

@Configuration
public class CustomServletConfig {

	// 注册Servlet

	@Bean
	public ServletRegistrationBean MyServlet() {
	ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean<>();

	servletRegistrationBean.setServlet(new ServletConfig());
	servletRegistrationBean.addUrlMappings("/myServlet");
	
	return servletRegistrationBean;
     }
}

 

你可能感兴趣的:(Springboot,java,后端)