Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

The different/limitations between declaration and scriptlet tags  RSS feed

 
derrick tan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm new in jsp... i have do some research on it already... i kow that <% %> is for code fragment/scriptlet, and <%! %> is for declaration... but i have found that <% %> also can do declaration... so actually what are the different between them... i was so confuse between these... It is because actually i just use <% %> already can do declaration and scriptlet, so what the purpose we still need <%! %>??? This is my problem...

Thanks you in advance....
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<% %> ends up as an inline script, basically code you would put inside of a Java method

<%! %> is actually a declaration of a method itself. This construct actually creates a method inside of a JSP that you can call later on.

That's the difference between the two.

<%! %> is somewhat frowned upon, as methods used in this type of a declaration cannot easily be reused, as might a method in a JavaBean.

Cheers!

-Cameron McKenzie
 
derrick tan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for yours help, but i still got a bit confuse with the last sentence... i'm not so understand what you means that the method declaration in <%! %> cannot easily be reused???

Anyway you have help me a lot... cheers...
 
Mahesh Barik
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

when you use declarative tags the variables become the part of doGet() or doPost() method
and if you use inside scriptlet then the variables become instance variable of the servlet classtha is being generated

in cases where you need to know how many times your page has been accessed,you can use a variable say count which increments each time page is reloaded then
you will notice the difference.

if the count variable is declared using scriptlet it wont increase ,however if count is a part of declarative tags then it becomes the part od doGet() or doPost()method and obviously it will increase each time you reload the page
 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With a quick example


<%! int meaningOfLife = 42; %>
<% int devilsNumber = 666; %>

will produce a servlet somewhing along the lines of this



So you see one is declared as a method local variable, the other as a class attribute.
Because JSPs are multi-threaded by default, any class attributes are shared among all instances of the JSP. Basically it means that the value is not threadSafe.

I normally aim for 100% scriptlet free pages, and don't use either <%! %> or <% %> tags unless I absolutely HAVE to.
JSTL all the way.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66203
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stefan Evans:

I normally aim for 100% scriptlet free pages, and don't use either <%! %> or <% %> tags unless I absolutely HAVE to.
JSTL all the way.


+1

If you are using JSP 2.0, there is no good reason to be writing JSPs with scriplets anymore.

If you are still stuck in the JSP 1.x world, approach <%! %> as if it were a radioactive stock-pile. As Stefan points out, that way lies pain, aggravation and threading issues.

A good number of "my JSP is actiing weird" problems can be traced back to the improper use of <%! %>.
[ October 10, 2006: Message edited by: Bear Bibeault ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!