• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

accessing initialization parameters

 
geeta vemula
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • 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?
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • 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...
 
priya rishi
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • 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
 
Himanshu Kansal
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • 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
  • 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 ;)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic