JBridge

JBridge: Certification Question Of The Day

JBridge Home >> Certification Questions >> Question for Monday May 19th 2003 Monday May 19th 2003

Which of the following are listener interface methods which would potentially be triggered as a result of starting a web application containing the following servlet? (Consider that the servlet is configured to load on start up of the web application). (3 correct answers).

public class Filters extends HttpServlet {
  public void init() throws ServletException {		
    ServletContext sc = this.getServletContext();
    sc.setAttribute("MyAttribute", "MyAttributeValue");
    sc.setAttribute("MyAttribute", "MyAttributeValue");
  }
}


A ServletContextListener.contextInitialized(ServletContextEvent sce)
B ServletContextListener.contextStarted(ServletContextEvent sce)
C ServletContextListener.contextAdded(ServletContextEvent sce)
D ServletContextListener.applicationInitialized(ServletContextEvent sce)
E ServletContextAttributeListener.attributeInitialized(ServletContextAttributeEvent scae)
F ServletContextAttributeListener.attributeAdded(ServletContextAttributeEvent scae)
G ServletContextAttributeListener.attributeChanged(ServletContextAttributeEvent scae)
H ServletContextAttributeListener.attributeReplaced(ServletContextAttributeEvent scae)
Page down for the answer...











































The Answer

The correct answers are A, F and H. The ServletContextListener.contextInitialized(ServletContextEvent sce) fires just on starting the web application. Then the init() method of the servlet runs. This sets an attribute, causing the ServletContextAttributeListener.attributeAdded(ServletContextAttributeEvent scae) to fire. Immediately after, the code sets the same-named attribute. Although it is being set to the same value (no real change), the method ServletContextAttributeListener.attributeReplaced(ServletContextAttributeEvent scae) will still fire.
The firing of any events depends on an appropriate Listener being written and set up in the web deployment descriptor (that's why the question says the methods are "potentially" triggered).
All the other answers contain plausible-sounding made-up method names.


EMail: dbridgewater@jbridge.co.uk
Phone: +44 (0)1943 877414
Fax: +44 (0)1943 877414
Mail: David Bridgewater, Willow Dene, Bradford Road, Menston, Ilkley, West Yorkshire, LS29 6ED, UK
Copyright © 2003 David Bridgewater. All rights reserved.