`

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 config			a <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)。

分享到:
评论
2 楼 gary_bu 2014-10-13  
感谢分享,有用
1 楼 mutex_js 2011-03-03  
<div class="quote_title">congdepeng 写道</div>
<div class="quote_div">
<p> </p>
<p> </p>
<p>Listener的项目上下文(既ServletContext既application)是从event中获取的,event是Listener和容器之间交流的中间人</p>
<p> </p>
<pre name="code" class="java"> 


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();
   }


</pre>
 
<p> </p>
<p> </p>
<p> </p>
<p>而Filter的项目上下文(既ServletContext既application)是从FilterConfig中获取的,FilterConfig是Filter和容器之间交流的中间人</p>
<p> </p>
<pre name="code" class="java"> 

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. &lt;br&gt;&lt;br&gt;

     * The web container cannot place the filter into service if the init method either&lt;br&gt;
        * 1.Throws a ServletException &lt;br&gt;
        * 2.Does not return within a time period defined by the web container
*/
public void init(FilterConfig filterConfig) throws ServletException;



------------------------
filterConfig.getServletContext()</pre>
<p> </p>
<p> </p>
<p>而Servlet的项目上下文(既ServletContext既application)是从ServletConfig中获取的,ServletConfig是Servlet和容器之间交流的中间人</p>
<p> </p>
<pre name="code" class="java">public interface Servlet {

    /**
     * Called by the servlet container to indicate to a servlet that the
     * servlet is being placed into service.
     *
     * &lt;p&gt;The servlet container calls the &lt;code&gt;init&lt;/code&gt;
     * method exactly once after instantiating the servlet.
     * The &lt;code&gt;init&lt;/code&gt; method must complete successfully
     * before the servlet can receive any requests.
     *
     * &lt;p&gt;The servlet container cannot place the servlet into service
     * if the &lt;code&gt;init&lt;/code&gt; method
     * &lt;ol&gt;
     * &lt;li&gt;Throws a &lt;code&gt;ServletException&lt;/code&gt;
     * &lt;li&gt;Does not return within a time period defined by the Web server
     * &lt;/ol&gt;
     *
     *
     * @param config a &lt;code&gt;ServletConfig&lt;/code&gt; 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>

相关推荐

Global site tag (gtag.js) - Google Analytics