Dear Colleagues
Answer A: method init(). It it not true, that a servlet generated from a JSP page has no
init() method, because the servlets type (
HttpJspBase with
Tomcat) derives from
GenericServlet, where
init() is implemented (defined in interface
Servlet). Therefore, it is possible to override
init() by means of a JSP declaration. Bridgewater actually goes further (abbreviated from pp. 57f): "Initialization code for a servlet could go in the constructor [...] but it's more usual to override the
public void init() method and place initialization code there. The servlet container is guaranteed to call this method once - and only once - on instantiation of the servlet."
So, why is
init() deemed not to be a correct answer? Potentially, overriding
init() in a JSP-generated servlet is not regarded best practice? Bridgewater says in Chapter 6
JavaServer Pages (p.384): "You can also use declarations to override some methods that appear further up in the JSP servlet hierarchy - namely
jspInit() and
jspDestroy()." (Method
init() not mentioned.)
Answers C and D: constructor vs. dynamic initialization block. Constructor and non-static initialization blocks both execute when a new object is created. What can a constructor possibly do, what an init block can not (or vice versa)? Why is the one correct and the other is wrong?
Please let me off the hook, if these are recurring items. I couldn't proof so. I'm looking forward to read your comments.
Best regards,
Ralf Wahner