wood burning stoves 2.0*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes JSP doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "JSP doubt" Watch "JSP doubt" New topic
Author

JSP doubt

Minu Jain
Ranch Hand

Joined: Mar 24, 2008
Posts: 74
you are creating a new JSP page and you need to execute some code that acts when the page is first executed, but only once. which three are possible mechanisms for performing the initialization code? (choose 3)
A. in the init method
B. in the jspInit mehtod
C. in the constructor of the JSP's java code
D. in a JSP declaration, which includes an initializer block
E. in a JSP declaration, which includes a static initializer block

SCBCD5, SCWCD5, SCJP5
"Even if you're on the right track, you'll get run over if you just sit there."
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
a) in the init method : JSP do not have a method named init(). Its only for servlet that are written by implementing Servlet interface.
b) In the jspInit mehtod : This is a correct answer. Write down a JSP for yourself. As you know JSP are converted into servlets, the servlet container will have a jspInit() method that behaves excatly same as init() of servlet. jspInit() is invoked only once during the lifetime of a servlet [jsp converted servlet]
c) In the constructor of the JSP's java code : Since most of the containers create only one instance of the servlet , this code will also run only once. Containers use the same instance to serve multiple requests.
d) in a JSP declaration, which includes an initializer block: Same explanation as above
e) In a JSP declaration, which includes a static initializer block Same explanation as above


Try it yourself


Result can be seen in logs\stdout.log

So actually there are 4 options that are correct.


SCJP, SCWCD, SCBCD
Ralf Wahner
Greenhorn

Joined: Jun 03, 2007
Posts: 21
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


SCJP (SE6), SCWCD (EE5)
Parth Twari
Ranch Hand

Joined: Jan 20, 2010
Posts: 163
this question brings out , how irresponsibly the free mock exams are rewritten .

This shows that they just search forum for questions and copy them they never test it.


see this question
you are creating a new JSP page and you need to execute some code that acts when the page is first executed, but only once. which three are possible mechanisms for performing the initialization code? (choose 3)
A. in the init method
B. in the jspInit mehtod
C. in the constructor of the JSP's java code
D. in a JSP declaration, which includes an initializer block
E. in a JSP declaration, which includes a static initializer block


they ask for 3 options but actually all are correct

i tested it myself with this code , tell me if I am wrong



which prooves that all the options are correct!

But can someone tell me why does initializer run before constructor?


Parth Tiwari
| Pursuing Bachelor of Engineering | OSUM Club Leader | SCJP 6 | SCWCD 5 |...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JSP doubt
 
Similar Threads
Static and Instance Blocks..
for - scope (including own initializer)
Non static initialization
Initialization for a JSP
why get's printed