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)。
分享到:
相关推荐
详细的解释了servlet filter listener 额知识,本文档适合初学者阅读
关于filter、servlet在web.xml配置及加载顺序
web.xml 中的listener、 filter、servlet 加载顺序及其详解
Servlet中的八大Listener.docServlet中的八大Listener.docServlet中的八大Listener.docServlet中的八大Listener.docServlet中的八大Listener.docServlet中的八大Listener.docServlet中的八大Listener.docServlet中的...
servlet_filter_listener
关于所有ServletListener的介绍,非常有用的一个小东东
Servlet、Filter、Listener深入理解.docx
Java WEB 篇九 Java servlet、filter、listener、interceptor 之间的区别和联系?
杂记(一):Listener、Filter和工具 杂记(一):Listener、Filter和工具
通常JavaWeb项目启动时我们需要... 下面代码是模拟初始化的一个示例,可以在控制台看到程序的输出,和Listener、Filter、Servlet的启动先后顺序,强烈建议跟我一样喜欢动手的Coder操作一下,废话不多说,直接上代码了。
servlet--listener 各种接口的简单例子 ,将每个接口的实现都简单的举例,特别容易学会,下载后直接 放到工作空间后运行
SpringBoot整合Listener的两种方式: 1.通过注解扫描完成Listener组件的注册 1.创建一个类实现 ServletContextListener (具体实现哪个Listener根据情况来判断) 2.在类上加入注解 @WebListener 3.重写 context...
本源码将详细介绍web.xml配置中servlet,filter,listener的加载顺序,可以让学习者更好的了解web.xml各种属性配置,自己写的东西,不足之处请大家见谅,顺便收点积分也好下资料,谢谢
Filter和Listener区别
特殊情况(ActionForm,Servlet, Filter, Listener)下Spring如何注入对象,本文源码下载地址http://usc.googlecode.com/files/UsersManagerWithAJAX.rar
listener和filter 监听器和过滤器的介绍以及简单使用(csdn)————程序
doFilter(ServletRequest, ServletResponse, FilterChain) - Method in interface javax.servlet.Filter The doFilter method of the Filter is called by the container each time a request/response pair is ...
web服务器三大组件servlet、Filter、Listene——浅浅笔记