Servlet,Listener和Filter如何获取ServletContext(既application)
?
?
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 { /** * Called by the servlet container to indicate to a servlet that the * servlet is being placed into service. * * <p>The servlet container calls the <code>init</code> * method exactly once after instantiating the servlet. * The <code>init</code> method must complete successfully * before the servlet can receive any requests. * * <p>The servlet container cannot place the servlet into service * if the <code>init</code> method * <ol> * <li>Throws a <code>ServletException</code> * <li>Does not return within a time period defined by the Web server * </ol> * * * @param configa <code>ServletConfig</code> object *containing the servlet's * configuration and initialization parameters * * @exception ServletException if an exception has occurred that *interferes with the servlet's normal *operation * * @see UnavailableException * @see #getServletConfig * */ public void init(ServletConfig config) throws ServletException; --------------------------------------- getServletConfig().getServletContext()??
?
我们的应用程序组件只能被动的遵守一定的规则,和容器打交道,和其他组件通信,也必须借助于容器的力量。这里面其实已经有一点控制反转的味道,既然是组件生活在容器中,就必须被动的接受容器喂给他吃的东西,不能(要)自己创造(new)。
?
Spring之所以称为容器(号称轻量级),就是因为被他控制的组件,被动的吃他喂过来的东西,不能(要)自己创造(new)。
/**
* Called by the servlet container to indicate to a servlet that the
* servlet is being placed into service.
*
* <p>The servlet container calls the <code>init</code>
* method exactly once after instantiating the servlet.
* The <code>init</code> method must complete successfully
* before the servlet can receive any requests.
*
* <p>The servlet container cannot place the servlet into service
* if the <code>init</code> method
* <ol>
* <li>Throws a <code>ServletException</code>
* <li>Does not return within a time period defined by the Web server
* </ol>
*
*
* @param configa <code>ServletConfig</code> object
*containing the servlet's
* configuration and initialization parameters
*
* @exception ServletException if an exception has occurred that
*interferes with the servlet's normal
*operation
*
* @see UnavailableException
* @see #getServletConfig
*
*/
public void init(ServletConfig config) throws ServletException;
---------------------------------------
getServletConfig().getServletContext()
</pre>
?
<p>?</p>
<p>?</p>
<p>我们的应用程序组件只能被动的遵守一定的规则,和容器打交道,和其他组件通信,也必须借助于容器的力量。这里面其实已经有一点控制反转的味道,既然是组件生活在容器中,就必须被动的接受容器喂给他吃的东西,不能(要)自己创造(new)。</p>
<p>?</p>
<p>Spring之所以称为容器(号称轻量级),就是因为被他控制的组件,被动的吃他喂过来的东西,不能(要)自己创造(new)。</p>
</div>
<p>?</p>