• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Basic Doubt

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In JSP there are Declaration tag and Scriptlets tag.
In Declaration tag we can declare the variables and functions.
In Scriptlets tag also we can declare the variables.
What is the difference and why there is the new tag Scriptlets?
Please help me. Thanks in advance.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
declartion tag will declare your variables every time the jsp
page or servlet is run,
but sciptlet tag is just like defining the variables in
init() method of servlet.they are defined for the
first time a jsp page is invoked.
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Somasundaram,
Declaration var : vars defined like <%! int nthUser=0; %>
Scriplet var : vars defined like <% String userName=null; %>
Scriplet vars are like local vars defined inside servive(..) method of a Servlet. They are local to only servive(..) method and not known outside. Also whenever a browser calls a jsp page, these scriplet vars are initialized each and every time freshly!
They don't maintain their old vales between requests. In other words it is alive only for one request. They can be accessed between other <% %> only.
Declaration vars means PAGE level vars. <%! %> means you are declaring OUTSIDE of service(...) methods and are global to all other methods defined within this jsp page.
This also means <%! ...%> vars maintain their vals BETWEEN requests. They are persistant vars.
Simillarly if you define a method <%! public void sayHello() {...} %> , it means it is a whole new method written outside of service(...) method and can be accessed inside scriplets <% ..%> also. Got it!
Also note that, the implicit objects of a jsp page which are 'request', 'response','session'... etc are alive only inside scriplets <% %>, meaning inside service(....) method. Not outside. So you can't write a code like this. <%! session.getAttribute("login"); %> It is illegal.
But you can write a global method with HttpRequest object as parameter and pass the request object from a scriplet like this.

This code is legal!
regds
maha anna
It is like this

[This message has been edited by maha anna (edited March 23, 2001).]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic