• Post Reply Bookmark Topic Watch Topic
  • New Topic

JSP scriplets

 
Vinney Shanmugam
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we declare a method in scriptlets? If so, what is the use of declaration tag?
Can someone throw some information on this....
 
Hany Shafik
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't ,methods and member variables must be declared inside declaration tag.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should not, ever, declare methods in JSPs. That style of coding went out of fashion years ago, and is considered bad design now.
 
Keith Flo
Ranch Hand
Posts: 128
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinney,

The reason you cant do this is because any scriptlets you write in a JSP page are placed in the service method of the servlet the container creates from your JSP page. In Java, you can't have a method within a method. You can have a class within a method, but not a method by itself.



Declarations, on the other hand, are compiled into members (variables, methods) of the generated servlet. So, you can write methods there.

The bottom line is this ... listen to Mr. Dittmer .. dont do it!!
Write you java code in classes, custom tags or servlets. Whoever has to maintenance your code later ... and that might be you ... will thank you for it!
 
Vinney Shanmugam
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks keith for the detailed and thoughtful reply. It helped me understand lot other things too....
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!