Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

define a method in JSP  RSS feed

 
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!
 
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.
 
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?
 
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 ]
 
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.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!