Springboot注册Servlet

2020/01/16 16:54
阅读数 86

第一步:先写一个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;
     }
}

 

展开阅读全文
加载中

作者的其它热门文章

打赏
0
0 收藏
分享
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部