Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

accessing initialization parameters

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got this from http://www.javaranch.com/carl/SCWCD.htm#Question 31)


Which statements are correct about the way a servlet can acces its initialization parameters?

1) By simply calling getInitParameter from any of the servlets methods (for example doGet)
2) It must be done by calling getServletConfig().getInitParaemter
3) It can only be done by overriding the method init(ServletConfig config)
4) It can be done by calling getServletContext().getInitParameter method

Answer is 1.
But why not 2 and 4 are correct?
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You do not need to call getServletConfig().

HttpServlet inherits from GenericServlet which implements ServletConfig and therefor has a getInitParameter() method that accesses the servlet config parameters.

As for no. 4, this calls the servlet CONTEXT init parameters, which are a totally different story...
 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
2.incorrect.
- though the statement is correct, but its not a must.
note:getInitParaemter(spelling)
4. incorrect.
- when the question does not specifically mention ServletConfig or ServletContext init parameters,
the default you should consider is ServletConfig init parameters.
whereas getServletContext().getInitParameter method will return ServletContext init parameters
 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can't the reason simply be "getInitParameter" is available to the HttpServlet class from a super class in its hierarchy(GenericServlet) !
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Himanshu Kansal wrote:Can't the reason simply be "getInitParameter" is available to the HttpServlet class from a super class in its hierarchy(GenericServlet) !



Just as I said ;)
 
"To do good, you actually have to do something." -- Yvon Chouinard
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic