Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

whts your answer  RSS feed

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all
i was trying this free mock exam at jdiscuss.com n came across this 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() { ... }; %>
according to me the correct ans are 1 & 2. But the exam said 2 & 4 to be correct.wht do you all say?
 
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Exactly 2 & 4 are correct. JSP Declaration is used to declare and define variables & methods. You cannot do it in Scriptlet.


Bye for now
sat
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi..

I dont understand how option 4 is correct.Method defintion has been terminated with a semicolon.First of all is it valid as per Java syntax?.Can any1 explain this?.

Regards,
Priya.
 
Ranch Hand
Posts: 579
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1.<% public void m1() { ... } %>

2.<%! public void m1() { ... } %>

3.<%@ public void m1() { ... } %>

4.<%! public void m1() { ... }; %>

5.<% public void m1() { ... }; %>


I think only [2] is correct.
 
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Yes, the method terminated with ; is valid java symtax. It is actually not a method termination, but it is empty declaration statement.

You can put n number of ; in java code before or after any declaration.

thanks
 
Bhavna Jharbade
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all
thanx for your prompt responses, Narendra i very well understood why 4 is correct but can u also explain why 2 is incorrect. According to me it should just creat an instance method.wht do you say???
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bhavna,

I am not saying 2 is incorrect. I am just answering Priya's question about correctness of syntax. Option 2 is correct. You can define instance/static variables and methods in declration syntax.

The answer is 2 and 4.

Thanks
[ October 21, 2005: Message edited by: Narendra Dhande ]
 
Priya Jothi
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Narendra,

Thanks for ur response.Now I understood clearly!!.

Bhavna,

2 is also correct option only..U've also stated the same earlier.
In case u want to know how/why 2 is correct..here it goes..
If u declare any method using <%! %> it'll be considered as instance method only.It'll be copied as such from your jsp file to the container generated servlet for your jsp.That means apart from _jspservice method u'll get one more method public void m1(){ } in ur servlet class.

hth..

Regards,
Priya.
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Bhavna Jharbade:
according to me the correct ans are 1 & 2. But the exam said 2 & 4 to be correct.wht do you all say?



Option 1 cannot be correct because scriptlets are inserted into the _jspService() method. Since you can't define a method inside a method option 1 will not work.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to asked from this disussion

we can declare any function
in declaration(<%! %> ) only


can we declare any function in scriptlet(<%
pleaser tell
[ October 21, 2005: Message edited by: rakesh mittal ]
 
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
We can declare functions only in declaration section (<%! %> and not anywhere else. Whatever the code you write between scriptlet (<% %> goes into service method. As any method cannot go into another method in java, we cannot have functions defined in scriptlet section.
 
satishkumar janakiraman
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

can we declare any function in scriptlet(<%
pleaser tell




You cannot declare a function inside scriptlet. Simply you cannot declare one function inside another function

I hope, it will help you

bye for now
sat
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!