[Logo]
Forums Register Login
whts your answer
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?
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
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.
 

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.
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
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???
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 ]
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.
 

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.
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 ]
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.
 

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

This thread has been viewed 852 times.

All times above are in ranch (not your local) time.
The current ranch time is
Oct 16, 2018 01:26:31.