为什么我每次运行项目时request都initialize了三次

来源:3-3 ServletRequest的事件监听器

qq_何足道_1

2017-12-23 10:49

代码如下

```

public class FirstListener implements ServletRequestListener {

   // Public constructor is required by servlet spec
   public FirstListener() {
   }

@Override
   public void requestDestroyed(ServletRequestEvent servletRequestEvent) {
       System.out.println("request destroyed initParam=" + servletRequestEvent.getServletContext().getAttribute("initParam"));
   }

   @Override
   public void requestInitialized(ServletRequestEvent servletRequestEvent) {
       servletRequestEvent.getServletContext().setAttribute("intiParam","yang");
       System.out.println("request initialized");
   }
}

```

运行结果

```

Connected to server

[2017-12-23 10:44:47,157] Artifact ListenerDemo:war exploded: Artifact is being deployed, please wait...

[2017-12-23 10:44:47,776] Artifact ListenerDemo:war exploded: Artifact is deployed successfully

[2017-12-23 10:44:47,776] Artifact ListenerDemo:war exploded: Deploy took 619 milliseconds

request initialized

request destroyed initParam=null

request initialized

request destroyed initParam=null

request initialized

request destroyed initParam=null

```

写回答 关注

1回答

  • qq_何足道_1
    2017-12-23 10:50:14

    额,代码竟然不显示

    我再贴一下

    public class FirstListener implements ServletRequestListener {
    
       // Public constructor is required by servlet spec
       public FirstListener() {
       }
    @Override
       public void requestDestroyed(ServletRequestEvent servletRequestEvent) {
           System.out.println("request destroyed initParam=" + servletRequestEvent.getServletContext().getAttribute("initParam"));
       }
    
       @Override
       public void requestInitialized(ServletRequestEvent servletRequestEvent) {
           servletRequestEvent.getServletContext().setAttribute("intiParam","yang");
           System.out.println("request initialized");
       }
    }


JAVA Web开发技术应用——监听器

本课程从Java Web中的监听器的概念和用途入手,结合实例讲解

76034 学习 · 155 问题

查看课程

相似问题