Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Basic Doubt

 
Somasundaram Karthikeyan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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.
 
asheet anand
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • 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.
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • 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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic