listener、filter、servlet获得ServletContext

Listener的项目上下文(既ServletContext既application)是从event中获取的,event是Listener和容器之间交流的中间人

      public interface ServletContextListener extends EventListener {  
      /** 
      ** Notification that the web application initialization 
      ** process is starting. 
      ** All ServletContextListeners are notified of context 
      ** initialization before any filter or servlet in the web 
      ** application is initialized. 
      */  
   
     public void contextInitialized ( ServletContextEvent sce );  
   
   
   
 --------------------------------------------  
   
  ServletContext servletContext;  
   
   
    public void contextInitialized(ServletContextEvent sce)  
    {  
       servletContext = sce.getServletContext();  
    }  

 而Filter的项目上下文(既ServletContext既application)是从FilterConfig中获取的,FilterConfig是Filter和容器之间交流的中间人

 public interface Filter {  
  
     /**  
    * Called by the web container to indicate to a filter that it is being placed into 
    * service. The servlet container calls the init method exactly once after instantiating the 
    * filter. The init method must complete successfully before the filter is asked to do any 
     * filtering work. <br><br> 
  
       * The web container cannot place the filter into service if the init method either<br> 
       * 1.Throws a ServletException <br> 
         * 2.Does not return within a time period defined by the web container  
     */  
    public void init(FilterConfig filterConfig) throws ServletException;  
      
  
   
 ------------------------  
            filterConfig.getServletContext()  

 而Servlet的项目上下文(既ServletContext既application)是从ServletConfig中获取的,ServletConfig是Servlet和容器之间交流的中间人

 

   public interface Servlet {  
   
      public void init(ServletConfig config) throws ServletException;  
      
      ---------------------------------------  
  
      getServletConfig().getServletContext()  
 

我们的应用程序组件只能被动的遵守一定的规则,和容器打交道,和其他组件通信,也必须借助于容器的力量。这里面其实已经有一点控制反转的味道,既然是组件生活在容器中,就必须被动的接受容器喂给他吃的东西,不能(要)自己创造(new)。

 

Spring之所以称为容器(号称轻量级),就是因为被他控制的组件,被动的吃他喂过来的东西,不能(要)自己创(new)。

 

 

你可能感兴趣的:(spring,Web,servlet,生活)