Kite的学习历程之SpringBoot中嵌入式Servlet容器三大组件配置

Kite学习框架的第十八天

1. 配置Servlet

1 .1首先创建一个MyServlet继承HttpServlet

实现doPost,doGet请求

package cn.kitey.springboot.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请求
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("Hello Servlet!");

    }
}


1.2 在MyServerConfig中进行Servlet的注册

 /**
     * 注册Servlet
     * @return
     */
    @Bean
    public ServletRegistrationBean myServlet(){
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new MyServlet(), "/myServlet");
        return servletRegistrationBean;
    }

1.3 通过浏览器访问myServlet

Kite的学习历程之SpringBoot中嵌入式Servlet容器三大组件配置_第1张图片
这时Servlet注册成功!

2. 注册Filter拦截器

2.1 首先创建一个MyFilter继承Filter

实现doFilter方法

package cn.kitey.springboot.filter;

import javax.servlet.*;
import java.io.IOException;

public class MyFilter implements Filter  {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("MyFilter process");
        //放行
        filterChain.doFilter(servletRequest,servletResponse);
    }
}


2.2 在ServerConfig中注册

我这里拦截是/MyServlet,如果成功,控制台将进行打印

 /**
     * 注册filter
     * @return
     */
    @Bean
    public FilterRegistrationBean myFilter(){
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        //设置拦截器
        registrationBean.setFilter(new MyFilter());
        registrationBean.setUrlPatterns(Arrays.asList("/myServlet"));
        return registrationBean;
    }

2.3 访问/MyServlet后控制台结果

在这里插入图片描述

3. 配置Listener监听器

3.1 创建MyListener类继承ServletContextListener

监听web应用的创建和销毁

package cn.kitey.springboot.listener;

import javax.servlet.Servlet;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * 配置listener监听器
 */
public class MyListener  implements ServletContextListener{
    /**
     * wb应用启动
     * @param sce
     */
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("contextInitialized。。。。当前web应用启动!");
    }

    /**
     * web应用销毁
     * @param sce
     */
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("contextDestroyed。。。。当前web应用销毁!");

    }
}


3.2 在ServerConfig中注册


    /**
     * 注册监听器
     * @return
     */
    @Bean
    public ServletListenerRegistrationBean myListener(){
        ServletListenerRegistrationBean<MyListener> registrationBean = new ServletListenerRegistrationBean<>(new MyListener());
        return registrationBean;
    }

3.3 运行后控制台打印结果

在这里插入图片描述
当关闭web服务器时
Kite的学习历程之SpringBoot中嵌入式Servlet容器三大组件配置_第2张图片
表明listener监听器注册成功!

你可能感兴趣的:(每天的学习笔记)