springBoot(8):web开发-Servlets, Filters, listeners

简介:

一、说明

Web 开发使用 Controller 基本上可以完成大部分需求,但是我们还可能会用到 Servlet、 Filter、Listener 等等

二、在spring boot中的三种实现方式

2.1、代码

CustomServlet.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package  com.example.demo.utils.servlet;
 
import  javax.servlet.ServletException;
import  javax.servlet.http.HttpServlet;
import  javax.servlet.http.HttpServletRequest;
import  javax.servlet.http.HttpServletResponse;
import  java.io.IOException;
 
/**
  * 自定义 servlet
  *
  */
public  class  CustomServlet  extends  HttpServlet {
     private  static  final  long  serialVersionUID = 1L;
   @Override
   protected  void  doGet(HttpServletRequest request, HttpServletResponse response)  throws  ServletException, IOException {
         System.out.println( "servlet get method" );
     doPost(request, response);
   }
   @Override
   protected  void  doPost(HttpServletRequest request, HttpServletResponse response)  throws  ServletException, IOException {
         System.out.println( "servlet post method" );
     response.getWriter().write( "hello world" );
   }
}


CustomFilter.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package  com.example.demo.utils.filter;
 
import  javax.servlet.*;
import  java.io.IOException;
 
/**
  * 自定义 filter
  * Created by DELL on 2017/6/17.
  */
public  class  CustomFilter  implements  Filter {
     @Override
   public  void  init(FilterConfig filterConfig)  throws  ServletException {
       System.out.println( "init filter" );
   }
   @Override
   public  void  doFilter(ServletRequest request, ServletResponse response, FilterChain chain)  throws  IOException, ServletException {
         System.out.println( "do filter" );
     chain.doFilter(request, response);
   }
   @Override
   public  void  destroy() {
       System.out.println( "destroy filter" );
   }
}

CustomListener.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package  com.example.demo.utils.listener;
 
import  javax.servlet.ServletContextEvent;
import  javax.servlet.ServletContextListener;
 
/**
  * 自定义 listener
  * Created by DELL on 2017/6/17.
  */
public  class  CustomListener  implements  ServletContextListener {
   @Override
     public  void  contextInitialized(ServletContextEvent sce) {
         System.out.println( "contextInitialized" );
   }
   @Override
   public  void  contextDestroyed(ServletContextEvent sce) {
       System.out.println( "contextDestroyed" );
   }
}

2.2、方式一:通过注册ServletRegistrationBean、FilterRegistrationBean和ServletListenerRegistrationBean

2.2.1、注册ServletRegistrationBean

Application中注册bean:

1
2
3
4
5
/**注册CustomServlet*/
@Bean
public  ServletRegistrationBean servletRegistrationBean() {
     return  new  ServletRegistrationBean( new  CustomServlet(),  "/roncoo" );
}

2.2.2、注册FilterRegistrationBean

Application中注册bean:

1
2
3
4
5
/**注册CustomFilter*/
@Bean
public  FilterRegistrationBean filterRegistrationBean() {
     return  new  FilterRegistrationBean( new  CustomFilter(), servletRegistrationBean());
}

2.2.3、注册ServletListenerRegistrationBean

Application中注册bean:

1
2
3
4
5
/**注册CustomListener*/
@Bean
public  ServletListenerRegistrationBean<CustomListener> servletListenerRegistrationBean() {
    return  new  ServletListenerRegistrationBean<CustomListener>( new  CustomListener());
}


上面所有例子效果:

 项目启动:

  wKiom1lFO6jzmGuHAABjCXAE_Kw377.jpg

 访问:http://localhost:8080/roncoo 

  wKiom1lFPAXg95_YAAAdV1v0GPo566.jpg

  wKiom1lFPFjwyZ4SAAAxE7E8AoE664.jpg

2.3、方式二:通过实现 ServletContextInitializer 接口直接注册

Application实现 ServletContextInitializer 接口:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package  com.example.demo;
 
import  com.example.demo.utils.filter.CustomFilter;
import  com.example.demo.utils.listener.CustomListener;
import  com.example.demo.utils.servlet.CustomServlet;
import  org.springframework.boot.SpringApplication;
import  org.springframework.boot.autoconfigure.SpringBootApplication;
import  org.springframework.boot.web.servlet.FilterRegistrationBean;
import  org.springframework.boot.web.servlet.ServletContextInitializer;
import  org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import  org.springframework.boot.web.servlet.ServletRegistrationBean;
import  org.springframework.context.annotation.Bean;
 
import  javax.servlet.DispatcherType;
import  javax.servlet.ServletContext;
import  javax.servlet.ServletException;
import  java.util.EnumSet;
 
@SpringBootApplication
public  class  SpringbootDemo27Application  implements  ServletContextInitializer {
    @Override
    public  void  onStartup(ServletContext servletContext)  throws  ServletException {
       servletContext.addServlet( "customServlet" new  CustomServlet()).addMapping( "/roncoo" );
       servletContext.addFilter( "customFilter" new  CustomFilter()).addMappingForServletNames(EnumSet.of(DispatcherType.REQUEST),  true "customServlet" );
       servletContext.addListener( new  CustomListener());
    }
 
    public  static  void  main(String[] args) {
       SpringApplication.run(SpringbootDemo27Application. class , args);
    }
}                                                                                                                                                                                                                            
 

2.4、方式三:

在SpringBootApplication上使用@ServletComponentScan注解后,直接通过@WebServlet、@WebFilter、@WebListener注解自动注册

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@ServletComponentScan
@SpringBootApplication
public  class  SpringbootDemo27Application {
      //...
}
 
@WebServlet (urlPatterns =  "/roncoo" , name =  "customServlet" )
public  class  CustomServlet  extends  HttpServlet {
     //...
}
 
@WebFilter (urlPatterns =  "/*" )
public  class  CustomFilter  implements  Filter {
     //...
}
 
@WebListener
public  class  CustomListener  implements  ServletContextListener {
     //...
}
本文转自我爱大金子博客51CTO博客,原文链接http://blog.51cto.com/1754966750/1939406如需转载请自行联系原作者

我爱大金子
相关文章
|
20天前
|
监控 JavaScript 前端开发
《理解 WebSocket:Java Web 开发的实时通信技术》
【4月更文挑战第4天】WebSocket是Java Web实时通信的关键技术,提供双向持久连接,实现低延迟、高效率的实时交互。适用于聊天应用、在线游戏、数据监控和即时通知。开发涉及服务器端实现、客户端连接及数据协议定义,注意安全、错误处理、性能和兼容性。随着实时应用需求增加,WebSocket在Java Web开发中的地位将更加重要。
|
1月前
|
Web App开发 前端开发 开发工具
介绍Web开发的基础知识
介绍Web开发的基础知识
29 7
|
1月前
|
存储 资源调度 应用服务中间件
浅谈本地开发好的 Web 应用部署到 ABAP 应用服务器上的几种方式
浅谈本地开发好的 Web 应用部署到 ABAP 应用服务器上的几种方式
26 0
|
1月前
|
缓存 关系型数据库 API
后端开发:构建高效、可扩展的Web应用程序的关键
后端开发:构建高效、可扩展的Web应用程序的关键
21 0
|
6天前
|
JSON Java fastjson
Spring Boot 底层级探索系列 04 - Web 开发(2)
Spring Boot 底层级探索系列 04 - Web 开发(2)
15 0
|
6天前
|
安全 编译器 PHP
PHP 8.1版本发布:引领Web开发新潮流
PHP编程语言一直是Web开发的主力军,而最新发布的PHP 8.1版本则为开发者们带来了更多创新和便利。本文将介绍PHP 8.1版本的主要特性,包括更快的性能、新的语言功能和增强的安全性,以及如何利用这些功能来提升Web应用程序的质量和效率。
|
9天前
|
PHP
web简易开发——通过php与HTML+css+mysql实现用户的登录,注册
web简易开发——通过php与HTML+css+mysql实现用户的登录,注册
|
9天前
|
前端开发 数据挖掘 API
使用Python中的Flask框架进行Web应用开发
【4月更文挑战第15天】在Python的Web开发领域,Flask是一个备受欢迎的轻量级Web框架。它简洁、灵活且易于扩展,使得开发者能够快速地构建出高质量的Web应用。本文将深入探讨Flask框架的核心特性、使用方法以及在实际开发中的应用。
|
18天前
|
安全 前端开发 Java
Java Web开发知识点学习总结
Java Web开发涉及Java基础、Servlet、JSP、数据库操作(SQL+JDBC)、MVC设计模式、Spring框架、Hibernate ORM、Web服务(SOAP&RESTful)、安全认证(HTTP Basic/Digest/OAuth)及性能优化(缓存、异步、负载均衡)。
17 3
|
21天前
|
XML JSON JavaScript
使用JSON和XML:数据交换格式在Java Web开发中的应用
【4月更文挑战第3天】本文比较了JSON和XML在Java Web开发中的应用。JSON是一种轻量级、易读的数据交换格式,适合快速解析和节省空间,常用于API和Web服务。XML则提供更强的灵活性和数据描述能力,适合复杂数据结构。Java有Jackson和Gson等库处理JSON,JAXB和DOM/SAX处理XML。选择格式需根据应用场景和需求。