Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JWebplus Question ID :995984030593

 
Albretch Mueller
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
please, let me first introduced the question in its context.
// - - - - - QUESTION - - - - - -
Consider the following code for the init() method of a servlet:
public void init()
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// 1 Get DBURL here
theConnection = DriverManager.getConnection(dbUrl, "admin", "admin");
}
Which of the following LOCs "may" correctly retrieve the DBURL parameter at //1?
Assume that servletcontext is a reference to ServletContext for this servlet.
A) servletcontext.getInitParameter("DBURL");
B) this.getParameter("DBURL");
C) servletcontext.getParameter("DBURL");
D) this.getContextParameter("DBURL");
E) this.getInitParameter("DBURL");
// - - - - - - OPTION COMMENTS - - - - -
A) This will retrieve the DBURL if it is defined as a servlet context parameter.
B) There is no such method in HttpServlet/GenericServlet.
C) There is no such method in ServletContext.
D) There is no such method.
E) This will retrieve the DBURL if it is defined as a servlet parameter.
// - - - - - - RIGHT ANSWERS - - - - -
A) servletcontext.getInitParameter("DBURL");
E) this.getInitParameter("DBURL");
// - - - - - - GENERAL COMMENTS - - - - -
This question does not specify where DBURL parameter is defined. However, there are only two places where you can define initialization parameters:
1. As a context parameter:
<context-param>
<param-name>DBURL</param-name>
<param-value>jdbc dbc:MySQLODBC</param-value>
</context-param>
In this case, it can be used by any servlet and can be retrieved using servletcontext.getInitParameter("DBURL");
2. As a servlet init parameter:
<servlet>
<servlet-name>catalog</servlet-name>
<servlet-class>com.mycorp.CatalogServlet</servlet-class>
<init-param>
<param-name>DBURL</param-name>
<param-value>jdbc dbc:MySQLODBC</param-value>
</init-param>
</servlet>
In this case, it is accessible only to this servlet and can be retrieved using: this.getInitParameter("DBURL");
// - - - - - - MY DOUBT - - - - - - - - -
Now, this q is confusing, from where/how can you call "this" in an init method if the actual "object", namely, the servlet; is not -fully- built?
 
Axel Janssen
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Albretch Mueller:
Now, this q is confusing, from where/how can you call "this" in an init method if the actual "object", namely, the servlet; is not -fully- built?

What do you mean exactly with fully build?
The life-cycle method init() is called after the servlet is instantiated.
 
Raj Paul
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, A & E are correct.
init() method will be called after instantiation only.
Raj Paul
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic