Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

whts your answer

 
Bhavna Jharbade
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?
 
satishkumar janakiraman
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
 
Priya Jothi
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.
 
agrah upadhyay
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.
 
Narendra Dhande
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.
 
Sergei Iakhnin
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.
 
rakesh mittal
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 ]
 
Rizwan Mohammad
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic