SpringBoot整合web层

1.通过注解扫描完成Servlet 组件的注册

使用eclipse的话需要在maven工程的pom.xml中添加相应的依赖,这里不再讲述依赖

1.1编写servlet

如果是在web.xml中的配置
/** *SpringBoot 整合 Servlet 方式一 
 *  
 *		 FirstServlet
 *		 com.rx.FirstServlet
 *  
 *  
 * 		FirstServlet
 * 		 /first
 *  
 * */
	 //继承HttpServlet类,重写doGet方法
	 @WebServlet(name="FirstServlet",urlPatterns="/first") 
	 public class FirstServlet extends HttpServlet {
	 @Override
	 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, 				  		 IOException {
	 	// TODO Auto-generated method stub 
		 super.doGet(req, resp);
	 	 }
	  }
  
	//启动类
	/** 
	* SpringBoot 整合 Servlet 方式一 
	*
	* */ 
	@SpringBootApplication 
	@ServletComponentScan 
	//在 springBoot 启动时会扫描@WebServlet,并将该类实例 化 
	 public class App {
		public static void main(String[] args) { 
		SpringApplication.run(App.class, args); 
		}
	}

1.2通过方法完成servlet的注册


//编写Servlet类
public class SecondServlet extends HttpServlet {
	@Override 
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
		System.out.println("SecondServlet.........."); 
		}
	}

	//启动类
	/** 
	* SpringBoot 整合 Servlet 方式二 
	* * * */ 
	@SpringBootApplication public class App2 {
		public static void main(String[] args) { 
			SpringApplication.run(App2.class, args); 
		}
		
	@Bean 
		public ServletRegistrationBean getServletRegistrationBean(){ 
			 ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet()); 
			 bean.addUrlMappings("/second");			 			     
		     return bean; 
			 } 
	 }

2.整合Filter

## 2.1通过注解扫描完成Filter 组件的注册
/** 
*SpringBoot 整合 Filter 方式一 
* 
*	 FirstFilter 
*	 com.rx.filter.FirstFilter 
* 
* 
* 	FirstFilter 
* 	/first 
* 
**/ 
//@WebFilter(filterName="FirstFilter",urlPatterns={"*.do","*.jsp"}) 拦截多种路径@WebFilter(filterName="FirstFilter",urlPatterns="/first") 
public class FirstFilter implements Filter {

	@Override public void destroy() { 
		// TODO Auto-generated method stub
	} 
	
	@Override 
	public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws 		IOException, ServletException { 
		System.out.println("进入 Filter"); 
		arg2.doFilter(arg0, arg1); 
		System.out.println("离开 Filter"); 
	}
		
	@Override 
	public void init(FilterConfig arg0) throws ServletException { 
	// TODO Auto-generated method stub 
	}
}

启动类:

	/** 
	*SpringBoot 整合 Filter 方式一 
	*/ 
	@SpringBootApplication 
	@ServletComponentScan 
	public class App {
		public static void main(String[] args) { 
			SpringApplication.run(App.class, args);
		}
	}

##2.2通过方法完成Filter 组件的注册
/** 
 *SpringBoot 整合 Filter 方式二 
 * 
 */ 
public class SecondFilter implements Filter { 

	@Override
	 public void destroy() { 
 		// TODO Auto-generated method stub 
	 } 
	 
	@Override 
	public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { 
		System.out.println("进入 SecondFilter"); 
		arg2.doFilter(arg0, arg1); 
		System.out.println("离开 SecondFilter"); 
	}
		
	@Override 
	public void init(FilterConfig arg0) throws ServletException { 
		// TODO Auto-generated method stub 
	}
}

//启动类
/** 
* SpringBoot 整合 Filter 方式二 
*
* 
*/ 
@SpringBootApplication 
public class App2 {
	public static void main(String[] args) { 
		SpringApplication.run(App2.class, args); 
	}
	
	/** 
	 * 注册 Servlet 
	 * @return 
	 */ 
	@Bean 
	public ServletRegistrationBean getServletRegistrationBean(){ 
		ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet()); 
		bean.addUrlMappings("/second"); 
		return bean; 
		}
		
   /** 
	* 注册 Filter 
	*/ 
	@Bean 
	public FilterRegistrationBean getFilterRegistrationBean(){ 
		FilterRegistrationBean bean = new FilterRegistrationBean(new SecondFilter()); 	
		//bean.addUrlPatterns(new String[]{"*.do","*.jsp"}); //配置多个拦截路径	
		bean.addUrlPatterns("/second"); 
		return bean;
		}
}

你可能感兴趣的:(java小二,bug日记本)