web.xml中Filter,Listener,Servlet的区别

原创
2018/05/04 10:39
阅读数 125

一、Servlet

Servlet是基本的服务端程序,他来自接口Servlet,接口中有方法service。而Servlet的一个重要实现类,则是tomcat服务器的核心,那就是HttpServlet

HttpServlet有方法:

复制代码

public abstract class HttpServlet extends GenericServlet
{
    private static final String METHOD_DELETE = "DELETE";
    private static final String METHOD_HEAD = "HEAD";
    private static final String METHOD_GET = "GET";
    private static final String METHOD_OPTIONS = "OPTIONS";
    private static final String METHOD_POST = "POST";
    private static final String METHOD_PUT = "PUT";
    private static final String METHOD_TRACE = "TRACE";

    private static final String HEADER_IFMODSINCE = "If-Modified-Since";
    private static final String HEADER_LASTMOD = "Last-Modified";
    
    private static final String LSTRING_FILE =
        "javax.servlet.http.LocalStrings";
    private static ResourceBundle lStrings =
        ResourceBundle.getBundle(LSTRING_FILE);
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    protected void doHead(HttpServletRequest req, HttpServletResponse resp)
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    protected void doPut(HttpServletRequest req, HttpServletResponse resp)
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
    protected void doOptions(HttpServletRequest req, HttpServletResponse resp)
    protected void doTrace(HttpServletRequest req, HttpServletResponse resp) 
}

复制代码

  几个do方法是核心,所有的客户端请求,最终都是通过Servlet的这几个方法处理的(除非被过滤器拦截)

  

二、Listener

Listener是监听器,用于监听Servlet,他是基于观察者模式的,他的核心接口是ServletContextListener,继承自EventListener。

有以下方法:

  public void contextInitialized(ServletContextEvent sce)

  public void contextDestroyed(ServletContextEvent sce)

其中ServletContextEvent继承自java.util.EventSource类,该类在构造方法中传递一个事件源进去,可以通过getServletContext方法来获取ServletContext,ServletContext为事件源。(单纯的一个简单的Listener,仅仅用于启动服务器时执行一些语句,则他的Source为一个ApplicationContextFacade,外观模式,核心是整个web程序的上下文)

  内置的监听器可用于监听属性变化,Session创建等,一般来说是用于监听Servlet的,Servlet监听器用于监听一些重要事件的发生,监听器对象可以在事情发生前、发生后可以做一些必要的处理。

  目前Servlet2.4和JSP2.0总共有8个监听器接口和6个Event类,其中HttpSessionAttributeListener与HttpSessionBindingListener 皆使用HttpSessionBindingEvent;HttpSessionListener和 HttpSessionActivationListener则都使用HttpSessionEvent;其余Listener对应的Event如下所示:

Listener接口

Event类

ServletContextListener

ServletContextEvent

ServletContextAttributeListener

ServletContextAttributeEvent

HttpSessionListener

HttpSessionEvent

HttpSessionActivationListener

HttpSessionAttributeListener

HttpSessionBindingEvent

HttpSessionBindingListener

ServletRequestListener

ServletRequestEvent

ServletRequestAttributeListener

ServletRequestAttributeEvent

  

  分别介绍:

  一、 ServletContext相关监听接口

  补充知识:

  通过ServletContext 的实例可以存取应用程序的全局对象以及初始化阶段的变量。

  在JSP文件中,application 是 ServletContext 的实例,由JSP容器默认创建。Servlet 中调用 getServletContext()方法得到 ServletContext 的实例。

  注意:

  全局对象即Application范围对象,初始化阶段的变量指在web.xml中,经由<context-param>元素所设定的变量,它的范围也是Application范围,例如:

  <context-param>

  <param-name>Name</param-name>

  <param-value>browser</param-value>

  </context-param>

  当容器启动时,会建立一个Application范围的对象,若要在JSP网页中取得此变量时:

  String name = (String)application.getInitParameter("Name");
  或者使用EL时:
  ${initPara.name}
  若是在Servlet中,取得Name的值方法:
  String name = (String)ServletContext.getInitParameter("Name");
  1.ServletContextListener:
  用于监听WEB 应用启动和销毁的事件,监听器类需要实现javax.servlet.ServletContextListener 接口。
  ServletContextListener 是 ServletContext 的监听者,如果 ServletContext 发生变化,如服务器启动时 ServletContext 被创建,服务器关闭时 ServletContext 将要被销毁。
  ServletContextListener接口的方法:
  void contextInitialized(ServletContextEvent sce)
  通知正在接受的对象,应用程序已经被加载及初始化。
  void contextDestroyed(ServletContextEvent sce)
  通知正在接受的对象,应用程序已经被载出。
  ServletContextEvent中的方法:
  ServletContext getServletContext()  
  取得ServletContext对象
  2.ServletContextAttributeListener:用于监听WEB应用属性改变的事件,包括:增加属性、删除属性、修改属性,监听器类需要实现javax.servlet.ServletContextAttributeListener接口。
  ServletContextAttributeListener接口方法:
  void attributeAdded(ServletContextAttributeEvent scab)
  若有对象加入Application的范围,通知正在收听的对象
  void attributeRemoved(ServletContextAttributeEvent scab)
  若有对象从Application的范围移除,通知正在收听的对象
  void attributeReplaced(ServletContextAttributeEvent scab)
  若在Application的范围中,有对象取代另一个对象时,通知正在收听的对象
  ServletContextAttributeEvent中的方法:
  java.lang.String getName()
  回传属性的名称
  java.lang.Object getValue()
  回传属性的值
  二、HttpSession相关监听接口
  1.HttpSessionBindingListener接口
  注意:HttpSessionBindingListener接口是唯一不需要再web.xml中设定的Listener
  当我们的类实现了HttpSessionBindingListener接口后,只要对象加入 Session范围(即调用HttpSession对象的setAttribute方法的时候)或从Session范围中移出(即调用HttpSession对象的 removeAttribute方法的时候或Session Time out的时候)时,容器分别会自动调用下列两个方法:
  void valueBound(HttpSessionBindingEvent event)
  void valueUnbound(HttpSessionBindingEvent event)
  思考:如何实现记录网站的客户登录日志, 统计在线人数?
  2.HttpSessionAttributeListener接口
  HttpSessionAttributeListener监听HttpSession中的属性的操作。
  当在Session增加一个属性时,激发attributeAdded(HttpSessionBindingEvent se) 方法;当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent se)方法;当在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEvent se) 方法。这和ServletContextAttributeListener比较类似。
  3.HttpSessionListener接口
  HttpSessionListener监听 HttpSession的操作。当创建一个Session时,激发session Created(HttpSessionEvent se)方法;当销毁一个Session时,激发sessionDestroyed (HttpSessionEvent se)方法。
  4.HttpSessionActivationListener接口
  主要用于同一个Session转移至不同的JVM的情形。
  四、ServletRequest监听接口
  1.ServletRequestListener接口
  和ServletContextListener接口类似的,这里由ServletContext改为ServletRequest
  2.ServletRequestAttributeListener接口
  和ServletContextListener接口类似的,这里由ServletContext改为ServletRequest
  有的listener可用于统计网站在线人数及访问量。 如下:
  服务器启动时(实现ServletContextListener监听器contextInitialized方法),读取数据库,并将其用一个计数变量保存在application范围内
  session创建时(实现HttpSessionListener监听器sessionCreated方法),读取计数变量加1并重新保存
  服务器关闭时(实现ServletContextListener监听器contextDestroyed方法),更新数据库。

三、Filter

Filter是过滤器,用于过滤到servlet的request,它使用户可以改变一个 request和修改一个response. Filter 不是一个servlet,它不能产生一个response,它能够在一个request到达servlet之前预处理request,也可以在离开 servlet时处理response.换种说法,filter其实是一个”servlet chaining”(servlet 链). 

一个Filter包括:

1)、在servlet被调用之前截获;

2)、在servlet被调用之前检查servlet request;

3)、根据需要修改request头和request数据;

4)、根据需要修改response头和response数据;

5)、在servlet被调用之后截获.

服务器每次只调用setFilterConfig方法一次准备filter 的处理;调用doFilter方法多次以处理不同的请求.FilterConfig接口有方法可以找到filter名字及初始化参数信息.服务器可以设置 FilterConfig为空来指明filter已经终结。

每一个filter从doFilter()方法中得到当前的request及response.在这个方法里,可以进行任何的针对request及 response的操作.(包括收集数据,包装数据等).filter调用chain.doFilter()方法把控制权交给下一个filter.一个 filter在doFilter()方法中结束.如果一个filter想停止request处理而获得对response的完全的控制,那它可以不调用下 一个filter。

 

这三者中,Servlet和Filter是可以配置mapping的,即针对哪些地址的请求使用这些Servlet或者Filter,而Listener则是根据实现接口来判断什么情况下调用这个Listener的,基本的Listener仅仅在启动时执行一些任务。如果一个请求,同时调用到这三个,则执行顺序是:Listener,Filter,Servlet。

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
12 收藏
0
分享
返回顶部
顶部