SECTION 3: THE SERVLET CONTAINER MODEL

Section 3
3.1 Identify the uses for and the interfaces (or classes) and methods to achieve the following features:
  • Servlet context init. parameters
  • Servlet context listener
  • Servlet context attribute listener
  • Session attribute listeners
3.2 Identify the WebApp deployment descriptor element name that declares the following features:
  • Servlet context init. parameters
  • Servlet context listener
  • Servlet context attribute listener
  • Session attribute listeners
3.3 Distinguish the behavior of the following in a distributable:
  • Servlet context init. parameters
  • Servlet context listener
  • Servlet context attribute listener
  • Session attribute listeners

Section 3 - The Servlet Container Model

3.1 Identify the uses for and the interfaces (or classes) and methods to achieve the following features:

  1. Servlet context init. parameters
    The ServletContext interface defines a servlet’s view of the web application within which the servlet is running. The Container Provider is responsible for providing an implementation of the ServletContext interface in the servlet container. Using the ServletContext object, a servlet can log events, obtain URL references to resources, and set and store attributes that other servlets in the context can access.
    • purpose: defines init parameters accessible by all servlets in the web application context; settable at deployment-time, but accessible at run-time
    • interfaces (or classes): javax.servlet.ServletContext
    • methods:
      public Enumeration getInitParameterNames()
      public String getInitParameter(String name)
    • webapp deployment descriptor element name:
      <context-param> {param-name, param-value} </context-param>
    • behavior in a distributable: Consider that a different instance of the ServletContext may exist on each different JVM and/or machine. Therefore, the context should not be used to store application state. Any state should be stored externally, e.g. in a database or EJB.
  2. Servlet context listener
    • purpose: An object that implements the ServletContextListener interface is notified when its web app context is created or destroyed
    • interfaces (or classes): javax.servlet.ServletContextListener
    • methods:
      void contextInitialized(ServletContextEvent e):
      called during web server startup or when context is added or reloaded; requests will not be handled until this method returns
      void contextDestroyed(ServletContextEvent e):
      called during web server shutdown or when context is removed or reloaded; request handling will be stopped before this method is called
    • webapp deployment descriptor element name:
        <listener>
          <listener-class> {fully qualified class name} </listener-class> 
        </listener>
        
    • behavior in a distributable:
      Each context instance (on different jvm's and/or machines) will have its own instance of the listener object. Therefore, if a context on one jvm/machine is initialized or destroyed, it will not trigger a listener on any other jvm/machine.

  3. Servlet context attribute listener
    • purpose: An object that implements the ServletContextAttributeListener interface is notified when attributes are added to or removed from its web app context
    • interfaces (or classes): javax.servlet.ServletContextAttributeListener
    • methods:
      void attributeAdded/attributeRemoved/attributeReplaced(ServletContextAttributeEvent e)
    • webapp deployment descriptor element name:
        <listener> 
          <listener-class> {fully qualified class name} </listener-class>
        </listener>
        
    • behavior in a distributable:
      Addition, removal or replacement of an attribute in a context will only affect the listener for that context, and not other context "instances" on other jvm's and/or machines.

  4. Session attribute listeners (HttpSession, Session Activation)
    • purpose: An object that implements the HttpSessionAttributeListener interface is notified when a session attribute is added, removed or replaced
    • interfaces (or classes): javax.servlet.http.HttpSessionAttributeListener
    • methods:
      void attributeAdded/attributeRemoved/attributeReplaced(HttpSessionBindingEvent e)
    • webapp deployment descriptor element name:
        <listener> 
            <listener-class> {fully qualified class name} </listener-class> 
        </listener>
        
    • behavior in a distributable: sessions may migrate from one jvm or machine to another; hence the session unbind event may occur on a different jvm/machine than the session bind event.

  5. Http Session listeners
    • purpose: An object that implements the HttpSessionListener interface is notified when a session is created or destroyed in its web app context
    • interfaces (or classes): javax.servlet.http.HttpSessionListener
    • methods:
      void sessionCreated(HttpSessionEvent e)
      void sessionDestroyed(HttpSessionEvent e)
      - called when session is destroyed (invalidated)
    • webapp deployment descriptor element name:
          <listener> 
             <listener-class> {fully qualified class name} </listener-class> 
          </listener>
        
    • behavior in a distributable: sessions may migrate from one jvm or machine to another; hence the session destroy event may occur on a different jvm/machine than the session create event.

  6. Http Session Activation listeners
    • purpose: Designed to handle sessions that migrate from one server to another. The listener is notified when any session is about to passivate(move) and when the session is about to activate(become alive) on the second host. It gives an app the chance to, for example, persist nonserializable data across jvm's
    • methods:
      void sessionWillPassivate(HttpSessionEvent e)
      session is about to move; it will already be out of service when this method is called
      void sessionDidActivate(HttpSessionEvent e)
      session has been activated on new server; session will not yet be in service when this method is called
    • webapp deployment descriptor element name:
         <listener>
            <listener-class> {fully qualified class name} </listener-class>
         </listener>
         

3.2 Identify the WebApp deployment descriptor element name that declares the following features:

  1. Servlet context init. parameters
     <context-param> {param-name, param-value} </context-param>
      
  2. Servlet context listener
      <listener>
        <listener-class> {fully qualified class name} </listener-class> 
      </listener>
      
  3. Servlet context attribute listener
      <listener> 
        <listener-class> {fully qualified class name} </listener-class>
      </listener>
      
  4. Session attribute listeners
      <listener> 
          <listener-class> {fully qualified class name} </listener-class> 
      </listener>
      

3.3 Distinguish the behavior of the following in a distributable:

  1. Servlet context init. parameters

    behavior in a distributable: Consider that a different instance of the ServletContext may exist on each different JVM and/or machine. Therefore, the context should not be used to store application state. Any state should be stored externally, e.g. in a database or ejb.

  2. Servlet context listener

    behavior in a distributable: Each context instance (on different jvm's and/or machines) will have its own instance of the listener object. Therefore, if a context on one jvm/machine is initialized or destroyed, it will not trigger a listener on any other jvm/machine.

  3. Servlet context attribute listener

    behavior in a distributable: Addition, removal or replacement of an attribute in a context will only affect the listener for that context, and not other context "instances" on other jvm's and/or machines

  4. Session attribute listeners

    behavior in a distributable: sessions may migrate from one jvm or machine to another; hence the session unbind event may occur on a different jvm/machine than the session bind event.

Return to top