![]() |
JBridge: J2EE Question Of The Day |
| JBridge Home >> Certification Questions >> Monday 28/4/2003 Question | Last updated: Monday 28th April 2003 |
|
Consider the following JSP page (correctly placed in an appropriate application server web application directory). Note that the line numbering is for convenience of referring to the source, and should not be considered as a potential contributor to a compilation error.
11 <html><head><title>J2EE Question Of The Day</title></head>
12 <body>
13 "The Mikado" by <%= message %> & <%= this.message %>
14 <% String message = "Gilbert"; %>
15 </body>
16 </html>
17 <%! private static String message = "Sullivan" %>
If you attempt to access the JSP in a web browser, what will occur?
A The page displays '"The Mikado" by Gilbert & Sullivan'. B The page displays '"The Mikado" by Gilbert & Gilbert'. C The page displays '"The Mikado" by Sullivan & Sullivan'. D A translation-time error occurs because the declaration of the private static variable at line 17 is wrongly placed. E A compilation error occurs because you may not declare static variables in a JSP. F A compilation error occurs for other reasons. Page down for the answer... The AnswerThe correct answer is F, a compilation error occurs for other reasons. The fault is that the declaration of the private static variable "message" is not terminated with a semi-colon. More interestingly, were this error corrected, the JSP would display '"The Mikado" by Sullivan & Sullivan', as per answer C. Static variables in JSP declarations are perfectly legal, so discounting answer E. Equally, their placement in the JSP does not matter, so eliminating D: the variable just becomes a data member in the translated and compiled JSP class regardless. The sciptlet declaring a local variable "message" at line 14 follows the expressions at line 13. This is reflected in the inline generated code: the expression code precedes the declaration. This means that at line 13, the only variable "message" that the JSP knows about is the one declared in line 17. |
EMail: dbridgewater@jbridge.co.uk
Phone: +44 (0)1943 877414
Fax: +44 (0)1943 877414
Mail: David Bridgewater, Willow Dene, Bradford Road, Menston, Ilkley, West Yorkshire, LS29 6ED, UK
Copyright © 2003 David Bridgewater. All rights reserved.