• springboot中配置servlet三大组件


    /**
     * 注册servlet的三大组件
     */
    @Configuration
    public class MyServerConfig {
    
        /**
         * 自定义servlet
         */
        @Bean
        public ServletRegistrationBean myServlet(){
            ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServlet(),"/myServlet");
            registrationBean.setLoadOnStartup(1);
            return registrationBean;
        }
    
        /**
         * 自定义过滤器
         */
        @Bean
        public FilterRegistrationBean myFilter(){
            FilterRegistrationBean registrationBean = new FilterRegistrationBean();
            registrationBean.setFilter(new MyFilter());
            registrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));
            return registrationBean;
        }
    
        /**
         * 自定义监听器
         */
        @Bean
        public ServletListenerRegistrationBean myListener(){
            ServletListenerRegistrationBean<MyListener> registrationBean = new ServletListenerRegistrationBean<>(new MyListener());
            return registrationBean;
        }
    
    
        /**
         * 配置内嵌式的servlet容器
         */
        @Bean
        public WebServerFactoryCustomizer embeddedServletContainerCustomizer(){
            return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
                @Override
                public void customize(ConfigurableWebServerFactory factory) {
                    factory.setPort(8083);
                }
            };
        }
    
    }
    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().write("Hello MyServlet");
        }
    }
    public class MyFilter implements Filter {
    
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
    
        }
    
        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            System.out.println("MyFilter process...");
            chain.doFilter(request,response);
        }
    
        @Override
        public void destroy() {
    
        }
    }
    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项目销毁");
        }
    }
  • 相关阅读:
    Systemd 入门教程:实战篇
    Systemd 入门教程:命令篇
    awk详解
    yum Multilib version problems
    Oracle ORA 12541 报错解决过程
    创建表空间
    大文件按行分离脚本
    heartbeat安装与配置
    Keepalived安装配置
    速查表的名称和建表语句
  • 原文地址:https://www.cnblogs.com/vegeta-xiao/p/12509196.html
Copyright © 2020-2023  润新知