整合 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. php自定义截取中文字符串-utf8版

    php自定义截取中文字符串-utf8版 UTF-8的编码范围(utf-8使用1-6个字节编码字符,实际上只使用了1-4字节): 1个字节:00——7F 2个字节:C080——DFBF 3个字符:E08 ...

  2. 深入理解计算机系统 第九章 虚拟内存 Part1 第二遍

    这次花了4小时40分钟,看了第 559~575 页,共 17 页 第一遍对应地址 https://www.cnblogs.com/stone94/p/10264044.html 注意:本章的练习题一定 ...

  3. jenkins里的定时构建

    1. 定时构建语法:* * * * * (五颗星,多个时间点,中间用逗号隔开)第一个*表示分钟,取值0~59第二个*表示小时,取值0~23第三个*表示一个月的第几天,取值1~31第四个*表示第几月,取 ...

  4. lqb 入门训练 Fibonacci数列 (循环 PS:提柜要栈溢出)

    入门训练 Fibonacci数列 时间限制:1.0s   内存限制:256.0MB     问题描述 Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1. 当n比较大时, ...

  5. 扛把子组Scrum立会报告+燃尽图 07

    此作业要求参见https://edu.cnblogs.com/campus/nenu/2019fall/homework/8684 一.小组情况组长:迟俊文组员:宋晓丽 梁梦瑶 韩昊 刘信鹏队名:扛把 ...

  6. 数据库求闭包,求最小函数依赖集,求候选码,判断模式分解是否为无损连接,3NF,BCNF

    1.说白话一点:闭包就是由一个属性直接或间接推导出的所有属性的集合. 例(1):   设有关系模式R(U,F),其中U={A,B,C,D,E,I},F={A→D,AB→E,BI→E,CD→I,E→C} ...

  7. JVM(2)--深入理解java对象创建始终

    java对象探秘 java是一门面向对象的语言,我们无时无刻不在创建对象和使用对象,那么java虚拟机是如何创建对象的?又是如何访问对象的?java对象中究竟存储了什么运行时所必需的数据?在学习了ja ...

  8. java 数组注意细节,例子解析

    1. int x[]; 或int [] x; 此时却无物理的存在数组.需:数组名= new 数组元素类型[size]: a = new int [10]; 2. 不能使用任何未初始化的数组. 默认的初 ...

  9. Python实现简单框架及三大框架对比

    手撸web框架 简单的请求响应实现 要实现最简单的web框架,首先要对网络熟悉,首先HTTP协议是应用层的协议,只要我们给数据加上HTTP格式的响应报头,我们的数据就能基于socket进行实现了 im ...

  10. 系统目录结构、ls命令、文件类型、alias命令 使用介绍

    1周第5次课(3月23日) 课程内容: 2.1/2.2 系统目录结构2.3 ls命令2.4 文件类型2.5 alias命令 Linux系统目录结构 在Linux系统里面也是同样存在很多文件和文件夹,而 ...