SpringBoot整合Servlet,Filter,Listener

一:SpringBoot整合Servlet有两种方式

1.方式一:通过注解扫描完成Servlet组件的注册

1.1编写Servlet

@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.2编写启动类

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

该方式的重点在@ServletComponentScan

该注解在 springBoot 启动时会扫描@WebServlet,并将该类实例化。

2.方式二:通过方法完成Servlet组件的注册

2.1编写Servlet

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

2.2编写启动类

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

该方法的重点在于springBoot的配置类:ServletRegistrationBean类的使用以及@bean注解

二:SptingBoot整合Filter有两种方式

1.通过注解扫描注册Filter组件

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

1.2编写启动类

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

该方式的重点在@ServletComponentScan

该注解在 springBoot 启动时会扫描@WebFilter,并将该类实例化。

2.通过方法注册Filter组件

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编写启动类

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

该方法的重点在于springBoot的配置类:FilterRegistrationBean类的使用以及@bean注解

三.SpringBoot整合Listener有两种方式

1.通过注解扫描的方式注册Listener组件

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......");
}
}

1.2编写启动类

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

该方式的重点在@ServletComponentScan

该注解在 springBoot 启动时会扫描@WebListener,并将该类实例化。

2.通过SpringBoot的注册类注册Listener

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.....");
}
}

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

该方式的重点在于SpringBoot的配置类ServletListenerRegistrationBean的使用以及@Bean注解的使用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

城南皮卡丘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值