• 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

A easy question please help  RSS feed

 
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?
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
#2
 
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?
 
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
=]
 
Ranch Hand
Posts: 640
1
  • 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
 
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 ]
 
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.
 
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]
 
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!