• springboot-11-servlet, listener, fitlter的添加


    springboot中添加servlet, filter, listener有2种方式: 代码注册servlet 和自动注解注册(在使用druid监控有使用过)

    代码注册通过ServletRegistrationBean、 FilterRegistrationBean 和 ServletListenerRegistrationBean 获得控制。 也可以通过servletContextInitializer接口直接注册

    在 SpringBootApplication 上使用@ServletComponentScan注解后,Servlet、Filter、Listener 可以直接通过 @WebServlet、@WebFilter、@WebListener 注解自动注册,无需其他代码。

    编写测试类: 

    Servlet:

    1), 使用代码注册的方式

    package com.iwhere.test.web.servlet;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * 通过代码注册servlet
     * 代码注册通过ServletRegistrationBean、 FilterRegistrationBean 和 ServletListenerRegistrationBean 获得控制。 
        也可以通过实现 ServletContextInitializer 接口直接注册。
     * @author wenbronk
     * @time 2017年3月17日  下午2:23:21  2017
     */
    
    public class MyServlet extends HttpServlet {
        private static final long serialVersionUID = 6272693061640286219L;
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //        super.doGet(req, resp);
            System.out.println("method deGet is run");
            doPost(req, resp);
        }
        
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //        super.doPost(req, resp);
            System.out.println("method doPost is run");
            
            resp.setContentType("text/html"); 
            PrintWriter out = resp.getWriter(); 
            out.println("<html>"); 
            out.println("<head>"); 
            out.println("<title>Hello World</title>"); 
            out.println("</head>"); 
            out.println("<body>"); 
            out.println("<h1>这是:MyServlet1</h1>"); 
            out.println("</body>"); 
            out.println("</html>");
        }
        
    }

    然后在 App.java (或者任意一类中, 该类必须@component被spring管理) 

        /**
         * 使用代码注册Servlet
         * 此方法不需要加注解  @ServletComponentScan
         * @return
         */
        @Bean
        public ServletRegistrationBean myServlet() {
             return new ServletRegistrationBean(new MyServlet(),"/myServlet/*");
        }

    2), 使用注解的方式实现: 

    package com.iwhere.test.web.servlet;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * 使用注解的方式注册servlet
     * 
     * @author wenbronk
     * @time 2017年3月17日 下午4:10:33 2017
     */
    
    @WebServlet(urlPatterns = "/myServlet2/*", description = "Servlet注解方式")
    public class AnnotionServlet extends HttpServlet {
        private static final long serialVersionUID = 1121465382990088487L;
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //        super.doGet(req, resp);
            System.err.println("do Get");
            doPost(req, resp);
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //        super.doPost(req, resp);
            System.err.println("do post");
    
            resp.setContentType("text/html");
            PrintWriter out = resp.getWriter();
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Hello World</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>这是:myServlet2</h1>");
            out.println("</body>");
            out.println("</html>");
        }
    
    }

    测试: 可通过http://localhost:8080/springboot-learn/myServlet进行访问: (有加context配置)

    Filter的配置:

    此处只讲注解方式, 代码方式和servlet一样

    package com.iwhere.test.web.filter;
    
    import java.io.IOException;
    
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.annotation.WebFilter;
    
    /**
     * 使用注解的方式创建filter, 实现一个filter接口
     * 必须加注解@ServletComponentScan
     * @author wenbronk
     * @time 2017年3月17日  下午4:48:19  2017
     */
    
    @WebFilter(filterName="myFilter", urlPatterns="/*")
    public class MyFilter implements Filter {
    
        @Override
        public void destroy() {
            System.out.println("filter is destroy");
        }
    
        @Override
        public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
                throws IOException, ServletException {
            System.out.println("running filter");
            arg2.doFilter(arg0, arg1);
        }
    
        @Override
        public void init(FilterConfig arg0) throws ServletException {
            System.out.println("filter is init");
        }
    
        
    }

    之后必须在main类上添加包扫描或者指定类

    @SpringBootApplication
    @EnableScheduling
    @EnableTransactionManagement(proxyTargetClass = true)
    @ServletComponentScan(basePackageClasses = {com.binfo.sqgf.config.SqlInjectFilter.class})
    public class SqgfApplication {

    或者

     @SpringBootApplication

    @EnableAutoConfiguration
    @EnableWebMvc
    @ServletComponentScan(basePackages = "com.fanyin.eghm")
    public class EghmApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(EghmApplication.class, args);
        }
    }
    

    listener

    listener有contextListener, sessionListener,pageListener, beanListener 好多种

    servletContextListener;

    package com.iwhere.test.web.listener;
    
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    import javax.servlet.annotation.WebListener;
    
    /**
     * 使用注解的方式实现Contextlistener, 必须实现listener接口
     * 
     * @author wenbronk
     * @time 2017年3月17日  下午4:51:24  2017
     */
    @WebListener
    public class MyListener implements ServletContextListener{
    
        @Override
        public void contextDestroyed(ServletContextEvent arg0) {
            System.out.println("servletContext destroyed");
        }
    
        @Override
        public void contextInitialized(ServletContextEvent arg0) {
            System.out.println("servletContext init");
        }
    
    }

    sessionListener

    package com.iwhere.test.web.listener;
    
    import javax.servlet.annotation.WebListener;
    import javax.servlet.http.HttpSessionEvent;
    import javax.servlet.http.HttpSessionListener;
    
    /**
     * sessionListener, 监听session的创建和销毁
     * @author wenbronk
     * @time 2017年3月17日  下午4:56:22  2017
     */
    @WebListener
    public class MySessionListener implements HttpSessionListener {
    
        @Override
        public void sessionCreated(HttpSessionEvent arg0) {
            System.out.println("HttpSession created");
        }
    
        @Override
        public void sessionDestroyed(HttpSessionEvent arg0) {
            System.out.println("HttpSession destroyed");
            
        }
    
    }

    补充一点, 在listener中使用 springboot的 工厂

    mongoTemplate = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext()).getBean(MongoTemplate.class);
  • 相关阅读:
    AX2009直接交运的bug
    数据库日志
    新蛋中国最新的分类导航,右侧展开菜单,可以修改向左或者向右展开
    用图片代替滚动条的代码
    新蛋网的大图展示效果,缩略图点击显示大图,上一个下一个
    Banner 切换,大小图不同,支持FF和OPERA,IE系列
    下拉菜单,支持所有浏览器
    电容选型
    000.数字电子技术分类
    Altium design16设计技巧
  • 原文地址:https://www.cnblogs.com/wenbronk/p/6567262.html
Copyright © 2020-2023  润新知