由于SpringBoot默认是以jar包的方式启动嵌入式的Servlet容器来启动SpringBoot的web应用,没有web.xml文件。

注册三大组件,class MyServlet()/class MyListener/ class MyFilter 需要自己亲自编写!!

1. class MyServlet

package com.szs.springboot04.servlet;

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 MyServlet extends HttpServlet { @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
} @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().print("Hello MyServlet!!!");
}
}

ServletRegistrationBean 注册

@Configuration
public class MyServerConfig {
/**
* 注册三大组件
*/
@Bean
public ServletRegistrationBean myServlet(){
ServletRegistrationBean registrationBean =
new ServletRegistrationBean(new MyServlet(),"/myServlet");
registrationBean.setLoadOnStartup(1);
return registrationBean;
}

效果展示


2.class MyFilter implements Filter

import javax.servlet.*;
import java.io.IOException; public class MyFilter implements Filter {
public MyFilter() {
super();
} @Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("myFilter正在初始化...");
} @Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("MyFilter 正在Process...");
filterChain.doFilter(servletRequest, servletResponse);
} @Override
public void destroy() { }
}

FilterRegistrationBean 注册

@Configuration
public class MyServerConfig {
@Bean
public FilterRegistrationBean myFilter(){
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
registrationBean.setFilter(new MyFilter());
registrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));
return registrationBean;
}




3.class MyListener implements ServletContextListener

public class MyListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("contextInitialized ...web应用启动");
} @Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
System.out.println("contextDestroyed... web项目已经销毁");
}
}

ServletListenerRegistrationBean myListener()

@Configuration
public class MyServerConfig {
@Bean
public ServletListenerRegistrationBean myListener(){
return new ServletListenerRegistrationBean<>(new MyListener());
}



SpringBoot 注册Servlet三大组件【Servlet、Filter、Listener】-原生代码+@Bean+效果展示的更多相关文章

  1. 18. Spring Boot 、注册Servlet三大组件Servlet、Filter、Listener

    由于SpringBoot默认是以jar包的方式启动嵌入式的Servlet容器来启动SpringBoot的web应用,没有web.xml文件 public class MyServlet extends ...

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

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

  3. 0017SpringBoot注册Servlet三大组件(Servlet、Filter、Listener)

    由于SpringBoot默认是以jar包的形式启动嵌入式servlet容器来启动SpringBoot的web应用,所以没有web.xml文件,那么如何配置Servlet.Filter.Listener ...

  4. JavaEE--JavaWeb三大组件Servlet、Filter、Listener

    Servlet.Filter.Listener是JavaEE Web服务规定的服务器动态组件,由开发者编写由Web容器创建,并保证单例以及线程安全性,其中加载顺序为Linstener -> Fl ...

  5. Spring Boot之注册servlet三大组件

    由于Spring Boot默认是以jar包的形式启动嵌入式的Servlet容器来启动Spring Boot的web应用是,没有web.xml配置文件 注册三大组件用以下方式 ServletRegist ...

  6. servlet三大组件

    servlet大致可以分为三个:简单servlet.过滤servlet.监听servlet servlet: servlet的创建 创建一个类并实现Servlet接口. 重写service方法. 在服 ...

  7. JavaWeb三大组件之一Filter知识总结

    [1] Filter简介    > Filter翻译为中文是过滤器的意思.    > Filter是JavaWeb的三大web组件之一Servlet.Filter.Listener    ...

  8. JavaWeb三大组件—过滤器filter

    JavaWeb三大组件 1. 都需要在web.xml中进行配置ServletListener(2个感知监听器不需要配置)Filter 2. 过滤器 它会在一组资源(jsp.servlet..css.. ...

  9. JavaWeb三大组件之Filter

    对请求或者响应进行拦截,并做额外的逻辑处理 filter能在一个请求访问目标资源之前对其进行拦截然后做某些逻辑处理,例如权限检查,也可以在一个输出响应到达客户端之前对其进行拦截并做一些额外的操作(例如 ...

随机推荐

  1. 对get post等http请求方式的理解

    本文是关于get,post等几种请求方式的资料搜集和学习,HTTP,HTTP2协议的涉及点,然后提到了socket协议,RPC 先是和朋友的一些交流对话,问着问着就到了我的知识盲区.需要恶补一下这方面 ...

  2. django models.update

    json/dict类型数据更新字段目前主流的web开放方式都讲究前后端分离,分离之后前后端交互的数据格式大都用通用的jason型,那么如何用最少的代码方便的更新json格式数据到数据库呢?同样可以使用 ...

  3. redis主从分节

    概述 在现有企业中80%公司大部分使用的是redis单机服务,在实际的场景当中单一节点的redis容易面临风险. 面临问题 机器故障.我们部署到一台 Redis 服务器,当发生机器故障时,需要迁移到另 ...

  4. 【LEETCODE】39、第561题 Array Partition I

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  5. Android--打开指定程序(微博/微信/QQ等)

    Intent intent = new Intent(); ComponentName cmp = new ComponentName("com.sina.weibo"," ...

  6. ubuntu 16.04 循环登陆问题

    换了个titan x重装显卡驱动失败之后一直循环登陆,试了N种处理显卡驱动的方法,并没有啥用. 最后查看了一下.Xerrer文件(具体的文件名我给忘记了),发现是.Xauthority. 现象:在Ub ...

  7. 深度学习 Bottleneck layer / Bottleneck feature

    最近在学习deeplearning的时候接触到了bottle-neck layer,好奇它的作用于是便扒了一些论文(论文链接放在文末吧),系统的了解一下bottle-neck feature究竟有什么 ...

  8. WPF 的 Application.Current.Dispatcher 中,为什么 Current 可能为 null

    原文:WPF 的 Application.Current.Dispatcher 中,为什么 Current 可能为 null 在 WPF 程序中,可能会存在 Application.Current.D ...

  9. .Net 取树形结构的数据

    最近遇到了无限层级数据要读取的问题,所有就写了个. 根据当前所有父级,查询出子级内容 private void GetTypeOfWorkforTree(out List<TypeOfWorkD ...

  10. atan、atanf、atanl、atan2、atan2f、atan2l

    很久不发博客了,今天在园中计算各种角,于是复习下fan正切函数 计算x的反正切值 (atan.atanf和 atanl) 或y/x 的反正切值 (atan2.atan2f和 atan2l).     ...