| Author |
whts your answer
|
Bhavna Jharbade
Ranch Hand
Joined: Sep 08, 2005
Posts: 69
|
|
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?
|
SCJP1.4 (86%)
|
 |
satishkumar janakiraman
Ranch Hand
Joined: May 03, 2004
Posts: 334
|
|
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
Joined: Jul 13, 2004
Posts: 168
|
|
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
Joined: Sep 01, 2005
Posts: 579
|
|
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.
|
<i>--Agrah Upadhyay--</i><br />Final Year B.Tech SCJP,SCWCD,SCBCD <br /> <br /><b>Now since the real test for any choice is having to make the same choice again,knowing full well what it might cost.</b>-Oracle
|
 |
Narendra Dhande
Ranch Hand
Joined: Dec 04, 2004
Posts: 950
|
|
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
|
Narendra Dhande
SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0
|
 |
Bhavna Jharbade
Ranch Hand
Joined: Sep 08, 2005
Posts: 69
|
|
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
Joined: Dec 04, 2004
Posts: 950
|
|
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
Joined: Jul 13, 2004
Posts: 168
|
|
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
Joined: Nov 09, 2004
Posts: 53
|
|
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.
|
SCJP 1.4, SCWCD 1.4
|
 |
rakesh mittal
Greenhorn
Joined: Oct 10, 2005
Posts: 27
|
|
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
Joined: Sep 02, 2005
Posts: 445
|
|
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.
|
Rizwan
SCJA, SCJP, SCWCD, SCBCD, SCDJWS.
|
 |
satishkumar janakiraman
Ranch Hand
Joined: May 03, 2004
Posts: 334
|
|
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
|
 |
 |
|
|
subject: whts your answer
|
|
|