一、整合Servlet

1.1 通过注解扫描完成Servlet注解
1.1.1 编写Servlet

/**
*SpringBoot 整合 Servlet 方式一
*
*<servlet>
*	<servlet-name>FirstServlet</servlet-name>
*	<servlet-class>com.bjsxt.servlet.FirstServlet</servlet-class>
*</servlet>
*
*<servlet-mapping>
* 	<servlet-name>FirstServlet</servlet-name>
* 	<url-pattern>/first</url-pattern>
*</servlet-mapping>
*
*/
@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);
		}
}

1.1.2 编写启动类

/**
* SpringBoot 整合 Servlet 方式一
*
*
*/
@SpringBootApplication
@ServletComponentScan //在 springBoot 启动时会扫描@WebServlet,并将该类实例化
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

1.2 通过方法完成 Servlet 组件的注册
1.2.1 编写Servlet

/**
*SpringBoot 整合 Servlet 方式二
*
*/
public class SecondServlet extends HttpServlet {
		@Override
		protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		System.out.println("SecondServlet..........");
	}
}

1.2.2 编写启动类

/**
* 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;
	}
}

二、整合 Filter

2.1 通过注解扫描完成 Filter 组件的注册
2.1.1 编写Filter

/**
*SpringBoot 整合 Filter 方式一
*<filter>
* 	<filter-name>FirstFilter</filter-name>
* 	<filter-class>com.bjsxt.filter.FirstFilter</filter-class>
*</filter>
*<filter-mapping>
* 	<filter-name>FirstFilter</filter-name>
* 	<url-pattern>/first</url-pattern>
*</filter-mapping>
*/

//@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
	}
}

2.1.2 编写启动类

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

2.2 通过方法完成 Filter 组件的注册
2.2.1 编写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
	}
}

2.2.2 编写启动类

/**
* 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;
	}
}

三、整合 Listener

3.1 通过注解扫描完成 Listener 组件的注册
3.1.1 编写 Listener

/**
* springBoot 整合 Listener
*
*<listener>
*	 <listener-class>com.bjsxt.listener.FirstListener</listener-class>
*</listener>
*/
@WebListener
public class FirstListener implements ServletContextListener {

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO Auto-generated method stub
	}
	
	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		System.out.println("Listener...init......");
	}
}

3.1.2 编写启动类

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

3.2 通过方法完成 Listener 组件注册
3.2.1 编写 Listener

/**
* springBoot 整合 Listener 方式二。
*
*
*/
public class SecondListener implements ServletContextListener {

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO Auto-generated method stub
	}
	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		System.out.println("SecondListener..init.....");
	}
}

3.2.2 编写启动类

/**
* SpringBoot 整合 Listener 方式二
*
*
*/
@SpringBootApplication
public class App2 {
	public static void main(String[] args) {
	SpringApplication.run(App2.class, args);
}
/**
* 注册 listener
*/
@Bean
public ServletListenerRegistrationBean<SecondListener>
getServletListenerRegistrationBean(){
		ServletListenerRegistrationBean<SecondListener> bean= new
		ServletListenerRegistrationBean<SecondListener>(new SecondListener());
		return bean;
	}
}
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐