• 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

What is the result of this?

 
Ranch Hand
Posts: 101
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 440
Hibernate Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Joey Sanchez
Ranch Hand
Posts: 101
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 440
Hibernate Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 2
Firefox Browser Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Creator of Enthuware JWS+ V6
Posts: 3411
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2
Firefox Browser Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
reply
    Bookmark Topic Watch Topic
  • New Topic