Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP method declaration

 
ravi janap
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Could you confirm the answer?
Question:Which of the follwing JSP fragments correctly define a method?
1.
<% public void m1() { ... } %>
2.
<%! public void m1() { ... } %>
3.
<%@ public void m1() { ... } %>
4.
<%! public void m1() { ... }; %>
5.
<% public void m1() { ... }; %>
Answer: 2 & 4
Isn't 2 a wrong answer since each variable declaration statement must be terminated with a semicolon.
Thanks
Ravi
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answer 2 & 4 are correct.
Isn't 2 a wrong answer since each variable declaration statement must be terminated with a semicolon.
This is a declaration, but it is not a "variable" declaration. You are just declaring a *method*. All things that are going inside a scriptlet will go in _jspService() method, so you cannot declare your own methods there (you can't declare a method inside a method). So you use a declaration <%! for that.
So you're right, *variable* declarations must end with semicolon. There's no restriction form *method* declarations.
hope it helps
 
ravi janap
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Andres
Thanks for the explanation. I guess the option (2) is more appropriate but having the additional semicolon in (4) does no harm either.
-- Ravi
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic