It's not a secret anymore!*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes What is the result of this? 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 "What is the result of this?" Watch "What is the result of this?" New topic
Author

What is the result of this?

Joey Sanchez
Ranch Hand

Joined: Jun 23, 2011
Posts: 86

Whats is the result of executing:



I think that jspInit() is the initialization method for JSP, so num will be initialised to 12.
Hence the result will be 18. I guess?

But in the book says that: Not that the initialisation method for a JSP is jspInit() not _jspInit(), which isn't invoked at all. Hence num is initialised to 0. Also, the integers will be added together before being converted to a string. So answer is: The output to the response is '6' ¿? I think there is a mistake in code or the answer, isn't it?
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

Have you tried it yourself .?
First tell us that what result are you getting when you execute that piece of code and why do you think that you are getting the result ?


Muhammad Saif Asif Mirza
OCJA(5/6) OCJP(6) OCJWCD(6)
Joey Sanchez
Ranch Hand

Joined: Jun 23, 2011
Posts: 86

Note: the former code had a little mistake by declaring the first line <% instead of <%!, now it has been changed!


The result I got was 18.
But the result of the book says it is correct it is 6.
I was trying to figure out what the author of the book was explaining and I think, he had a mistake in the explanation or code.
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

Joey Sanchez wrote:Note: the former code had a little mistake by declaring the first line <% instead of <%!, now it has been changed!


The result I got was 18.
But the result of the book says it is correct it is 6.
I was trying to figure out what the author of the book was explaining and I think, he had a mistake in the explanation or code.


Well if you leave out the error in the JSP, then yes the result you will get is 18. There may be an error in the book , I suggest you ask in the compiling errata forum.

As for the jspinit() method, from the JavaDocs
JavaDocs wrote: The jspInit() method is invoked when the JSP page is initialized

so jspInit(0 will run once when the page is being initialized , same goes for the jspDestroy method.
Bernhard Weyrauch
Greenhorn

Joined: Oct 16, 2013
Posts: 2

Hi guys, thanks for this discussion here. I have same question as Joey initially had and did not find the final statement.
I also checked Errata http://garnerpress.com/catalogue/BK0340/errata.html but this corrections isn't there.

I also tried to change the code to:

but the JSP doesn't compile (of course).

Error:
An error occurred at line: [36] in the generated java file: [C:\Program Files\XAMPP\tomcat\work\Catalina\localhost\OCEJWCD\org\apache\jsp\jsp\Chapt13Quest12_jsp.java]
Duplicate method _jspInit() in type Chapt13Quest12_jsp

Stacktrace:
at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:366)
at org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:468)
...

So, what is the final answer?
Thank you so much!

Bernhard


OCJA(5/6), OCJP(6), Preparing for OCJWCD(6)
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1665
    
  25

Bernhard Weyrauch wrote:
So, what is the final answer?

Welcome to Javaranch!

The answer lies in the JSP specs:

JSP.11.1.1.1 Protocol Seen by the JSP Page Author
The JSP specification defines the contract between the JSP container and the JSP page author. This contract defines the assumptions an author can make for the actions described in the JSP page. The main portion of this contract is the _jspService method that is generated automatically by the JSP container from the JSP page. The details of this contract are provided in Chapter JSP.9. The contract also describes how a JSP author can indicate what actions will be taken when the init and destroy methods of the page implementation occur. In JSP 2.0 this is done by defining methods with the names jspInit and jspDestroy in a declaration scripting element in the JSP page. The jspInit method, if present, will be called to prepare the page before the first request is delivered. Similarly a JSP container can reclaim resources used by a JSP page when a request is not being serviced by the JSP page by invoking its jspDestroy method, if present. A JSP page author may not (re)define servlet methods through a declaration scripting element. The JSP specification reserves names for methods and variables starting with jsp, _jsp, jspx, and _jspx, in any combination of upper and lower case.

In other words: 18 is the correct answer (and the book is wrong) as you can override the jspInit method. Declaring a method with name _jspInit is not allowed.

Regards,
Frits
Bernhard Weyrauch
Greenhorn

Joined: Oct 16, 2013
Posts: 2

Frits Walraven wrote:
In other words: 18 is the correct answer (and the book is wrong) as you can override the jspInit method. Declaring a method with name _jspInit is not allowed.


Great!! Thank you so much :-)
Next time, I'll directly check JEE Spec

Bernhard
 
jQuery in Action, 2nd edition
 
subject: What is the result of this?