• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JSP doubt

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic