整合 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. 清空 npm 缓存

    清空 npm 缓存 npm cache clean -f

  2. maven编码配置

    在环境变量里添加变量 MAVEN_OPTS -Xms256m -Xmx512m -Dfile.encoding=UTF-8

  3. jstl-将List中的数据展示到表格中

    功能: 使用jstl将List中的数据动态展示到Jsp表格中,并实现隔行换色功能. 效果图: Jsp代码: <%@ page import="java.util.ArrayList&q ...

  4. python06-列表表达式、生成器表达式及其面试题、解耦简单介绍、函数递归相关

    目录: 一.列表推导式 二.生成器表达式 三.集合生成器 四.生成器面试题 五.解耦简单介绍 六.函数递归相关 一.列表推导式 需求:将[1,3,5]中的每个元素平方 正常思路: new_list = ...

  5. 替换"marquee",实现无缝滚动

    js的marquee标签,可以实现元素循环滚动,但是不能无缝连接,要实现“无缝滚动”的效果必须使用js(借鉴百度),思路是使要滚动元素相对位置不断改变,上下滚动就相对top或者bottom,左右滚动就 ...

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

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

  7. 关于Prometheus监控的思考:多标签埋点及Mbean

    使用 grafana+prometheus+jmx 作为普通的监控手段,是比较有用的.我之前的文章介绍了相应的实现办法. 但是,按照之前的实现,我们更多的只能是监控 单值型的数据,如请求量,tps 等 ...

  8. java编程思想第四版第三章要点总结

    1. 静态导入 使用import static方式导入一个类的所有方法. 例如: import static net.mindview.util.Print.*; 首先定义了一个Print类,里面有静 ...

  9. nyoj 260-数数小木块 (打表)

    260-数数小木块 内存限制:64MB 时间限制:3000ms 特判: No 通过数:17 提交数:24 难度:1 题目描述: 在墙角堆放着一堆完全相同的正方体小木块,如下图所示: 因为木块堆得实在是 ...

  10. ES6学习 let const

    1.前言 发现网易云笔记 单纯的记笔记没什么意思,所以今天来博客园写学习感受了,毕设做了休息时间就来写写新学的知识 哈哈哈 !! 2.ES6 就是JavaScript 语言的下一代标准,2015年6月 ...