• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

The different/limitations between declaration and scriptlet tags

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
author and cow tipper
Posts: 5001
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67590
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
It means our mission is in jeapordy! Quick, read this tiny ad!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic