• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

define a method in JSP

 
Jamie Shi
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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() { ... }; %>

Select 2 correct options.

Can any one tell me why 2 is correct option? I think it is wrong because of no semicolon there at end. Thanks in advance!
 
Bhushan Jawle
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Semicolon is not mandatory in JSP method declaration. In fact I was thinking "Wouldn't semicolon cause a problem ? " :-)
If there are 2 correct options then I think container must be ignoring the semicolon after declaration.
 
Sudharsan Govindarajan
Ranch Hand
Posts: 319
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This seems to be a definition, not a declaration as you have those braces { } around. So, we don't need a semicolon after a method definition.
sudharsan
 
Jamie Shi
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question has 2 correct answers: 2 and 4. Option 2 confused me. For variables, semicolon is mandatory. However, why both are OK for methods?
 
Mark Howard
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Sudharsan said, you don't require a semicolon after a method declaration (option 2). The code within the curly braces would obviously require semicolons as per usual.
I'm not sure why a semicolon is required in option 4 though? I can only assume that it is ignored by the JSP engine.
As 2 options were required for the question, 2 and 4 are the best of the worst
 
Sudharsan Govindarajan
Ranch Hand
Posts: 319
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Method declarations need a semi-colon as a variable declaration do. But a method definition does'nt need a semicolon to terminate it. There is no concept of a variable definition though. . Also an extra semi-colon in your normal Java declaration/defintion statements would not affect the program syntax. It will just be ignored by the parser. you can try this

sudharsan
[ January 28, 2003: Message edited by: Sudharsan G'rajan ]
 
Dharmin Desai
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,
Options 2 and 4 is correct.
B'coz,
1. We can not define method in Scriplet (between <% and %> b'coz Scriplet code always goes to _jspService() method as it is.
And java is not allowing us to define a method in one method it self.
Where as, when we declare a variable or method between <%! and %> (JSP Declarations), it would be added to class which extends HttpJspBase class and not in a method.
So, Options 1 and 5 r wrong.
2. Whereas Page directive is to inform Jsp Engine about page properties. And Directives r predefined so the option 3 is wrong.
3. Now, comes to 2nd and 4th option:
2nd is true b'coz it is the way how we define a java method in our class. (here it is a class who extends HttpJspBase class while translation)
In the 4th option an additional ; is there, and we all know semicolon is mean to a complete statement in JAVA. Here in our example the semicolon would be added in a class which we can always do.
Hope, i m clear enough.
Best Regards, Dharmin
[ January 28, 2003: Message edited by: Dharmin Desai ]
 
Jamie Shi
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it. Thanks a lot.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic