整合 Servlet
 
方式一:
 
编写 servlet
package com.bjsxt.controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException; @WebServlet(name = "Servelt",urlPatterns ="/servlet" )
public class Servelt extends HttpServlet { @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("Servelt.doGet........");
}
}

编写启动类

package com.bjsxt;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan; @SpringBootApplication
@ServletComponentScan//在springboot启动时会自动扫描@WebServlet注解的配置信息,并将它实例化
public class SpringBootRun { public static void main(String[] args) {
SpringApplication.run(SpringBootRun.class,args);
}
}

方式二:

package com.bjsxt.controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException; public class Servelt2 extends HttpServlet { @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("Servelt.doGet2........");
}
}
@SpringBootApplication
public class SpringBootServletRun2 {
public static void main(String[] args) {
SpringApplication.run(SpringBootServletRun2.class,args);
} @Bean
public ServletRegistrationBean getServletRegistrationBean(){
ServletRegistrationBean<Servelt2> bean = new ServletRegistrationBean<>(new Servelt2());
bean.addUrlMappings("/servlet2");
return bean;
}
}

整合Filter(也有俩种方式)

编写 Filter

package com.bjsxt.controller;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException; @WebFilter(filterName = "Filter1",urlPatterns = "/filter1")
public class Filter1 implements Filter { @Override
public void init(FilterConfig filterConfig) throws ServletException { } @Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("进入拦截器Filter1");
filterChain.doFilter(servletRequest,servletResponse);
System.out.println("离开拦截器Filter1");
} @Override
public void destroy() { }
}
编写启动类
package com.bjsxt;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan; @SpringBootApplication
@ServletComponentScan//在springboot启动时会自动扫描@Web...注解的配置信息,并将它实例化
public class SpringBootFilterRun1 { public static void main(String[] args) {
SpringApplication.run(SpringBootFilterRun1.class,args);
}
}

方式二

package com.bjsxt.controller;

import javax.servlet.*;
import java.io.IOException; public class Filter2 implements Filter { @Override
public void init(FilterConfig filterConfig) throws ServletException { } @Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("进入拦截器Filter2");
filterChain.doFilter(servletRequest,servletResponse);
System.out.println("离开拦截器Filter2");
} @Override
public void destroy() { }
}
package com.bjsxt;

import com.bjsxt.controller.Filter2;
import com.bjsxt.controller.Servelt2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean; @SpringBootApplication
public class SpringBootFilterRun2 { public static void main(String[] args) {
SpringApplication.run(SpringBootFilterRun2.class,args);
} @Bean
public ServletRegistrationBean getRegistrationBean(){
ServletRegistrationBean<Servelt2> bean = new ServletRegistrationBean<>(new Servelt2());
bean.addUrlMappings("/filter2");
return bean;
} @Bean
public FilterRegistrationBean getFilterRegistrationBean(){
FilterRegistrationBean<Filter2> bean = new FilterRegistrationBean<>(new Filter2());
bean.addUrlPatterns("/filter2");
return bean;
}
}
编写 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......");
}
}
编写启动类
package com.bjsxt;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan; @SpringBootApplication
@ServletComponentScan//在springboot启动时会自动扫描@WebServlet注解的配置信息,并将它实例化
public class SpringBootRun { public static void main(String[] args) {
SpringApplication.run(SpringBootRun.class,args);
}
}

方式二:

 @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......");
}
}
package com.bjsxt;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan; @SpringBootApplication
@ServletComponentScan//在springboot启动时会自动扫描@WebServlet注解的配置信息,并将它实例化
public class SpringBootRun { public static void main(String[] args) {
SpringApplication.run(SpringBootRun.class,args);
}
}

Spring Boot整合Servlet、Filter、Listener的更多相关文章

  1. Spring Boot整合Servlet,Filter,Listener,访问静态资源

    目录 Spring Boot整合Servlet(两种方式) 第一种方式(通过注解扫描方式完成Servlet组件的注册): 第二种方式(通过方法完成Servlet组件的注册) Springboot整合F ...

  2. spring boot整合servlet、filter、Listener等组件方式

    创建一个maven项目,然后此项目继承一个父项目:org.springframework.boot 1.创建一个maven项目: 2.点击next后配置父项目及版本号 3.点击finish后就可查看p ...

  3. spring boot 2.x 系列 —— spring boot 整合 servlet 3.0

    文章目录 一.说明 1.1 项目结构说明 1.2 项目依赖 二.采用spring 注册方式整合 servlet 2.1 新建过滤器.监听器和servlet 2.2 注册过滤器.监听器和servlet ...

  4. Spring Boot 整合Servlet

    冷知识,几乎用不到 在spring boot中使用Servlet有两种实现方法: 方法一: 正常创建servlet,然后只用注解@ServletComponentScan package clc.us ...

  5. SpringBoot整合WEB开发--(九)整合Servlet,Filter,Listener

    简介: 如果需要整合第三方框架时,可能还是不得不使用Servlet,Filter,Listener,Springboot中也有提供支持. @WebServlet("/my") pu ...

  6. Spring Boot 整合 Web 开发

    这一节我们主要学习如何整合 Web 相关技术: Servlet Filter Listener 访问静态资源 文件上传 文件下载 Web三大基本组件分别是:Servlet,Listener,Filte ...

  7. Spring Boot整合实战Spring Security JWT权限鉴权系统

    目前流行的前后端分离让Java程序员可以更加专注的做好后台业务逻辑的功能实现,提供如返回Json格式的数据接口就可以.像以前做项目的安全认证基于 session 的登录拦截,属于后端全栈式的开发的模式 ...

  8. Spring Boot 注册 Servlet 的三种方法,真是太有用了!

    本文栈长教你如何在 Spring Boot 注册 Servlet.Filter.Listener. 你所需具备的基础 什么是 Spring Boot? Spring Boot 核心配置文件详解 Spr ...

  9. spring boot配置Servlet容器

    Spring boot 默认使用Tomcat作为嵌入式Servlet容器,只需要引入spring-boot-start-web依赖,默认采用的Tomcat作为容器 01  定制和修改Servlet容器 ...

随机推荐

  1. python机器学习——使用scikit-learn训练感知机模型

    这一篇我们将开始使用scikit-learn的API来实现模型并进行训练,这个包大大方便了我们的学习过程,其中包含了对常用算法的实现,并进行高度优化,以及含有数据预处理.调参和模型评估的很多方法. 我 ...

  2. 201871010114-李岩松《面向对象程序设计(java)》第十二周学习总结

    项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...

  3. 020.掌握Pod-Pod基础使用

    一 Pod定义详解 1.1 完整Pod定义文件 apiVersion: v1 #必选,版本号,例如v1,版本号必须可以用 kubectl api-versions 查询到 kind: Pod #必选, ...

  4. hdu 1024 Max Sum Plus Plus (动态规划)

    Max Sum Plus PlusTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. Linux网络基本配置命令

    修改方法: 命令方式,大多是立即生效.临时有效: GUI图形方式, 修改配置文件,重启服务有效 1.修改主机名 hostname查看 hostname name临时修改 hostnamectl set ...

  6. runlevel init

    init概念存在于cnetos7以下,配置文件/etc/inittab init 以及 文本和图形界面切换(可以用ctrl+alt+n 或者 init3 5切换,不是重启切) 命令init N 0 关 ...

  7. error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'

    用Python打开图像始终提示错误 error: OpenCV(4.1.1) C:\projects\opencv-python\opencv\modules\highgui\src\window.c ...

  8. 并行模式之Master-Worker模式

    并行模式之Master-Worker模式 一).Master-Worker模式 作用: 将一个大任务分解成若干个小任务,分发给多个子线程执行. 注: 将大任务分解成小任务,小任务的实现逻辑要相同. 二 ...

  9. TreeMap的源码学习

    TreeMap的源码学习 一).TreeMap的特点 根据key值进行排序. 二).按key值排序的两种排序算法实现 1).在构造方法中传入比较器 public TreeMap(Comparator& ...

  10. EFK教程(3) - ElasticSearch冷热数据分离

    基于ElasticSearch多实例架构,实现资源合理分配.冷热数据分离 作者:"发颠的小狼",欢迎转载与投稿 目录 ▪ 用途 ▪ 架构 ▪ 192.168.1.51 elasti ...