zoukankan      html  css  js  c++  java
  • springboot开发之配置Servlet三大组件(Servlet、Filter、Listener)

    1、首先我们自己定义三大组件:MyServlet.java、MyFilter.java、MyListener.java

    MyServlet.java

    package com.gong.springbootcurd.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 {
        //处理get请求
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doPost(req,resp);
        }
        //处理post请求
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.getWriter().write("hello world");
        }
    }

    MyFilter.java

    package com.gong.springbootcurd.filter;
    
    import javax.servlet.*;
    import java.io.IOException;
    
    public class MyFilter 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("myfilter process");
            filterChain.doFilter(servletRequest,servletResponse);;
        }
    
        @Override
        public void destroy() {
    
        }
    }

    MyListener.java

    package com.gong.springbootcurd.listener;
    
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    
    public class MyListener implements ServletContextListener {
        @Override
        public void contextInitialized(ServletContextEvent sce) {
            System.out.println("contextInitialized..web应用启动了");
        }
    
        @Override
        public void contextDestroyed(ServletContextEvent sce) {
            System.out.println("contextDestroyed..web应用关闭了");
        }
    }

    (2)我们自定义注册三大组件的文件:MyServletConfig.java

    package com.gong.springbootcurd.config;
    
    import com.gong.springbootcurd.filter.MyFilter;
    import com.gong.springbootcurd.listener.MyListener;
    import com.gong.springbootcurd.servlet.MyServlet;
    import org.springframework.boot.web.servlet.FilterRegistrationBean;
    import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
    import org.springframework.boot.web.servlet.ServletRegistrationBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import javax.servlet.Filter;
    import javax.servlet.Servlet;
    import javax.servlet.ServletContextListener;
    import java.util.Arrays;
    
    @Configuration
    public class MyServletConfig {
        //注册三大组件
        @Bean
        public ServletRegistrationBean myServlet(){
            ServletRegistrationBean<Servlet> registrationBean = new ServletRegistrationBean<>(
                    new MyServlet(),"/myServlet"
            );
            registrationBean.setLoadOnStartup(1);
            return registrationBean;
        }
        @Bean
        public FilterRegistrationBean myFilter(){
            FilterRegistrationBean<Filter> registrationBean = new FilterRegistrationBean<>();
            registrationBean.setFilter(new MyFilter());
            registrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));
            return registrationBean;
        }
        @Bean
        public ServletListenerRegistrationBean myListener(){
            ServletListenerRegistrationBean<MyListener> registrationBean = new ServletListenerRegistrationBean<MyListener>(new MyListener());
            return registrationBean;
        }
    }

    我们启动服务器,并输入localhost:8080/curd/myServlet,可以看到:

    MyServlet起作用了。

    在控制台:

    MyListener起作用了,MyFilter也起作用 

    点击紫色框中按钮关闭服务:

    MyListener其作用了。 

  • 相关阅读:
    Android之旅十六 android中各种资源的使用
    XTU OJ 1207 Welcome to XTCPC (字符串签到题)
    scala并发编程原生线程Actor、Case Class下的消息传递和偏函数实战
    【云图】怎样设置支付宝里的家乐福全国连锁店地图?
    怎样在QML中使用multitouch
    软件project师周兆熊给IT学子的倾情奉献
    Linux系统下怎样配置SSH?怎样开启SSH?
    数学之路-python计算实战(4)-Lempel-Ziv压缩(2)
    Day5上午解题报告
    一份只有巨佬才能看懂的代码
  • 原文地址:https://www.cnblogs.com/xiximayou/p/12263022.html
Copyright © 2011-2022 走看看