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....
Best regards,<br />derrick<br />~Thanks in advance~
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
<%! 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.