Springboot注册Servlet

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_38959210/article/details/89228734

第一步:先写一个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<Servlet> servletRegistrationBean = new ServletRegistrationBean<>();

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

猜你喜欢

转载自blog.csdn.net/weixin_38959210/article/details/89228734