JBridge

JBridge: Certification Question Of The Day

JBridge Home >> Certification Questions >> Question for Sunday May 18th 2003 Sunday May 18th 2003

Which of the following are appropriate ways to set up information that is meant to be available anywhere within a given web application? (2 correct answers)



A 
<servlet>
  <servlet-name>InitialisationServlet</servlet-name>
  <servlet-class>uk.co.jbridge.servlet.Initialisation</servlet-class>
  <init-param>
    <param-name>initFile</param-name>
    <param-value>initFile.ini</param-value>
  </init-param>
</servlet>

B 
<context>
  <init-param>
    <param-name>initFile</param-name>
    <param-value>initFile.ini</param-value>
  </init-param>
</context>

C Within servlet code:
ServletContext sc = getServletContext();
sc.setParameter("initFile", "initFile.ini");

D 
<context-param>
  <param-name>initFile</param-name>
  <param-value>initFile.ini</param-value>
</context-param>

E 
<servlet>
  <servlet-name>InitialisationServlet</servlet-name>
  <servlet-class>uk.co.jbridge.servlet.Initialisation</servlet-class>
  <context-param>
    <param-name>initFile</param-name>
    <param-value>initFile.ini</param-value>
  </context-param>
</servlet>

F Within servlet code:
ServletContext sc = getServletContext();
sc.setAttribute("initFile", "initFile.ini");

Page down for the answer...











































The Answer

D and F are the appropriate ways. Answer D shows the correct way to embed a context parameter within the web.xml deployment descriptor. Answer F shows how to set up a context-level attribute. The ServletContext is the appropriate scope for information available to a whole web application.
Answer A is perfectly legal, and shows how to set up initialisation parameters for a servlet. However, these are only accessible within that particular servlet - not the whole web application.
Answer B contains wrong element names and structure as far as web.xml goes.
Answer C invokes a non-existent method on ServletContext; you can't "set" parameters on a ServletContext through any method, only through setting them up in the deployment desciptor.
Answer E tries to persuade you that context parameters are set within a servlet definition in web.xml. This is not so. The <context-param> is a direct child of the top-level element, <web-app>.


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.