SpringBoot定义三大组件Servlet,过滤器Filter,监听器Listener

1.定义组件的配置类:

com.example.mybatis2018.config.MyServletConfig

package com.example.mybatis2018.config;import com.example.mybatis2018.filter.myFilter;
import com.example.mybatis2018.listener.myListener;
import com.example.mybatis2018.servlte.MyServlet;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.util.Arrays;
import java.util.Collections;@Configuration
public class MyServletConfig {//注册Servlet@Beanpublic ServletRegistrationBean MyServlet(){ServletRegistrationBean<MyServlet> myServletServletRegistrationBean = new ServletRegistrationBean<MyServlet>(new MyServlet(), "/myServlet");return myServletServletRegistrationBean;}//注册过滤器@Beanpublic FilterRegistrationBean  myFilter(){FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();filterRegistrationBean.setFilter(new myFilter());//添加过滤器filterRegistrationBean.setUrlPatterns(Arrays.asList("/myServlet")); //设置过滤路径return filterRegistrationBean;}//注册监听器public ServletListenerRegistrationBean myListener(){ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean();servletListenerRegistrationBean.setListener(new myListener());//添加过滤器return servletListenerRegistrationBean;}}

2.创建servlet

com.example.mybatis2018.servlte.MyServlet

package com.example.mybatis2018.servlte;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;public class MyServlet extends HttpServlet{@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {PrintWriter witer = resp.getWriter();witer.write("hello servler!");}
}

3.过滤器Filter

com.example.mybatis2018.filter.myFilter

package com.example.mybatis2018.filter;import javax.servlet.*;
import java.io.IOException;
import java.util.logging.LogRecord;public class myFilter implements Filter {@Overridepublic void init(FilterConfig filterConfig) throws ServletException {}@Overridepublic void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {System.out.println("过滤器执行并放行!");filterChain.doFilter(servletRequest,servletResponse);//放行}@Overridepublic void destroy() {}
}

4.监听器Listener

com.example.mybatis2018.listener.myListener

package com.example.mybatis2018.listener;import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;public class myListener implements ServletContextListener {@Overridepublic void contextInitialized(ServletContextEvent sce) {System.out.println("servletContext 对象创建。。。。");}@Overridepublic void contextDestroyed(ServletContextEvent sce) {System.out.println("servletContext 对象销毁。。。。");}
}

SpringBoot定义三大组件Servlet,过滤器Filter,监听器Listener相关推荐

  1. Spring Boot怎么样注册Servlet三大组件[Servlet、Filter、Listener]

    前言 由于SpringBoot默认是以jar包的方式启动嵌入式的Servlet容器来启动SpringBoot的web应用,没有web.xml文件. 注册三大组件用以下方式 一.ServletRegis ...

  2. 【小家Spring】SpringBoot中使用Servlet、Filter、Listener三大组件的三种方式以及原理剖析

    每篇一句 要么就安逸的穷,要么就拼命的干 前提概要 web开发使用Controller基本能解决大部分的需求,但是有时候我们也需要使用Servlet,因为相对于拦截和监听来说,有时候原生的还是比较好用 ...

  3. JavaWeb三大组件(Servlet,Filter,Listener 自己整理,初学者可以借鉴一下)

    JavaWeb三大组件(Servlet,Filter,Listener 自己整理,初学者可以借鉴一下) Reference 转载于:https://www.cnblogs.com/Jeremy2001 ...

  4. Servlet,过滤器,监听器,拦截器的区别

    由于最近两个月工作比较清闲,个人也比较"上进",利用工作空余时间,也继续学习了一下,某天突然想起struts2和struts1的区别的时 候,发现为什么struts1要用servl ...

  5. SpringBoot(四)-- 整合Servlet、Filter、Listener

    SpringBoot中有两种方式可以添加 Servlet.Filter.Listener. 1.代码注册 通过ServletRegistrationBean. FilterRegistrationBe ...

  6. SpringBoot中如何配置使用过滤器(Filter)呢?

    转自: SpringBoot中如何配置使用过滤器(Filter)呢? 下文笔者讲述springboot中配置过滤器的方法分享,如下所示 实现思路:1.定义filter2.将filter注册进sprin ...

  7. springboot 整合 Servlet、Filter、Listener、访问静态资源

    springboot 整合 Servlet.Filter.Listener.访问静态资源 1.引入pom.xml依赖 <dependency><groupId>org.spri ...

  8. SpringBoot注册Servlet、Filter、Listener

    SpringBoot默认是以jar包的方式启动嵌入式的Servlet容易来启动SpringBoot的Web应用,没有web.xml文件 因此我们可以使用以下方式来注册Servlet.Filter.Li ...

  9. SpringBoot之Servlet、Filter、Listener配置

    在SpringBoot中是不需要配置web.xml的,那么原来在web.xml中配置的Servlet.Filter.Listener现在怎么弄呢? SpringBoot提供了三种Bean Filter ...

最新文章

  1. 【收藏】23个机器学习最佳入门项目(附源代码)
  2. Openstack组件部署 — Overview和前期环境准备
  3. 数据科学行业的8个关键角色:职责与技能
  4. ERROR: Could not read unit serial number!
  5. [JavaWeb-JavaScript]JavaScript运算符
  6. 压测工具下载地址说明及汇总
  7. 在线预览文档简单例子
  8. Leetcode动态规划:300.longest-increasing-subsequence(最长递增子序列)
  9. Nginx内置模块简介
  10. Android Studio设置,鼠标放上去有提示
  11. java redis监听问题_springboot+redis过期事件监听实现过程解析
  12. insert into select from要注意什么_老外说put two and two together什么意思?是2+2?
  13. c++ 调用windows tts_有关调用约定的历史 – 第三部分
  14. 大型体检系统源码 医院PEIS管理系统源码 BS架构
  15. 云计算的概念与价值02技术与价值
  16. #10064. 「一本通 3.1 例 1」黑暗城堡
  17. 基于Python实现的作家风格识别
  18. bios无cfg lock的情况如何disable cfg lock
  19. 激活ltsb2016_激活windows10 LTSB 2016
  20. Packstack搭建Openstack R版本

热门文章

  1. SuSE Linux 应用与安装
  2. 【[USACO13NOV]没有找零No Change】
  3. html静态页面实现微信分享思路
  4. linux 日志编程(总结)
  5. iOS地图之MapKit框架
  6. cocos2dx中菜单menu 和 menuItem
  7. 【液晶模块系列基础视频】4.5.X-GUI图形界面库-进度条等函数简介
  8. 查看google chrome版本号及浏览器驱动下载
  9. SSLH:让 HTTPS 和 SSH 共享同一个端口
  10. PostgreSQL 配置内存参数