Discussions

General J2EE: About init(ServletConfig config) in Servlet 2.2

  1. About init(ServletConfig config) in Servlet 2.2 (1 messages)

    Hi,
      From servlet 2.1 onwards, the servlet author just overrides init() method and uses getInitParameter("name") to retieve init params configured for it.Assuming the servlet has overriden init(ServletConfig config).Which is the best way to access the init parameters ?
    (This was a question in one of J2EE certification exams !)
    The options are
    a)config.getInitParameter("name");
    b)getInitParameter("name");
    c)ServletConfig.getInitParameter("name");
    d)ServletContext.getServletConfig().getInitParameter("name");
  2. a)config.getInitParameter("name");
    wud be the best way since the container has passed ServletConfig object to the servlet which enables you to work on the config object & obtain initparameters.
    This is a direct approach.

    b) wud let the superclass of ur servlet(GenericServlet) to get the refernce to ServletConfig & invoke getinitParameter();

    C & D won't work since getInitParameter("name") is not a staic method. Also
    ServletContext does not have a method like getServletConfig(). Actually it's the other way around.