整合 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. pxe批量部署

    功能: 批量全自动安装操作系统方法: dhcp 自动分配IP tftp 微系统 用来安装系统 httpd 网络源 操作流程: #检查环境 getenforce #检查selinux systemctl ...

  2. element - ui tree

    一行代码两行泪,没有外网真可怕!好久没写博客了,更新一把. <template> <div> <el-tree :data="data" :props ...

  3. 类型擦除真的能完全擦除一切信息吗?java 泛型揭秘

    背景 我们都知道泛型本质上是提供类型的"类型参数",它们也被称为参数化类型(parameterized type)或参量多态(parametric polymorphism).其实 ...

  4. thinkphp volist标签中加if判断的写法

    <if condition="$vo['devstatus'] eq 1">在线<else /> 离线</if> IF标签用法 <if c ...

  5. C++中对C的扩展学习新增语法——动态内存管理

    1.C语言动态内存管理的缺点: 1.malloc对象的大小需要自己计算. 2.需要手动转换指针类型. 3.C++的对象不适合使用malloc和free. 2.C++中new/delete基本使用: 3 ...

  6. [Git]Git常用命令速查手册

    看的别人的文章,来源:https://mp.weixin.qq.com/s/SGRcE9EPOu4Tph65tzPzQw

  7. pat 1065 A+B and C (64bit)(20 分)(大数, Java)

    1065 A+B and C (64bit)(20 分) Given three integers A, B and C in [−2​63​​,2​63​​], you are supposed t ...

  8. suseoj 1209: 独立任务最优调度问题(动态规划)

    1209: 独立任务最优调度问题 时间限制: 1 Sec  内存限制: 128 MB提交: 3  解决: 2[提交][状态][讨论版][命题人:liyuansong] 题目描述 用2台处理机A和B处理 ...

  9. Mirantis 收购 Docker | 云原生生态周报 Vol. 28

    作者 | 禅鸣.进超.心水.心贵 业界要闻 Docker 将 Docker Enterprise 卖给 Mirantis Mirantis 是一家扎根于 OpenStack 的云公司,最近专注于 Ku ...

  10. ThreadLocal深度解析和应用示例

    开篇明意 ThreadLocal是JDK包提供的线程本地变量,如果创建了ThreadLocal<T>变量,那么访问这个变量的每个线程都会有这个变量的一个副本,在实际多线程操作的时候,操作的 ...