Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A easy question please help

 
David Liu
Greenhorn
Posts: 24
  • 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() { ... }; %>
which 2 options are correct?
 
Sri, Shankar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
#2
 
Nazmul Huda Sarkar
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Options 2 and 4 are correct. Nothing is wrong putting a ; at the end of method declaration.
 
David Liu
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Sarkar
whether put a ; at the end of the declaration
doesn't matter
is it true?
 
AJAY RANA
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear David,
The extra ; has nothing to do with JSP declaration.In normal java syntax u can put any number of ';'.They won't do anything(but won't give compiler error.)The exact details of this are given in JLS.
so this code is also valid :
<%! public void m1() {//code here for ur method }
//method ends
;;;;;
%>
-U can do same in scriptlet also as long as scripting language is java(Till date we can use only java as scripting language in JSP).
Ajay Rana
SCJP,SCWCD
[ October 19, 2002: Message edited by: AJAY RANA ]
 
David Liu
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much
=]
 
trupti nigam
Ranch Hand
Posts: 627
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the correct answers can be 1 and 5 as it is a valid java code which we can put under scriptlets..
thanks
trupti
 
Andre Mermegas
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cant put methods in scriplet code, as that code goes right into _jspService I believe.
-Andre
[ October 24, 2002: Message edited by: Andre Mermegas ]
 
satya uppaluri
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IT may be worth looking at /CATALINA_HOME/work/localhost, where you can have all these jsps converted into java servlet source/java class files.( if you are on a different application server the location will vary)
It is perticularly helpful in debugging your jsps if you have lot of includes.
 
Vishal Chugh
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you ... I also checked generated servlet code using 2 and 4

[To me also it was disturbing why 4 is correct , but its okay now]
 
muktesh tripathi
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure but is option 5 wrong ....and why pse explain??
 
Vishal Chugh
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Muktesh ,


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


As we know that whatever is inside <% %> lands directly in _jspService()method of generated servlet of JSP , that means this method definition will come under _jspService() definition which is never allowed : To define methods within the methods as per OOPS rules.

method A { method B {} } is wrong.

I hope ,it will be okay with you now
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic