Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Variables Scope

 
amit sanghai
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you set variables' scope to for eg. page, session, etc. in JSP?
 
Jason Brawner
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By default, a variables scope is of 'page' if your declaring it in a jsp page (within an scriplet or declaration). If you want the variable to be of scope session, do something like the following...
session.setAttribute(name, value)
Then you can access your variable using session.getAttribute(name);
Hope this helps,
JEB
 
Arun Boraiah
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is just an extension for Jason's explanation.
in jsp if you want class level variable to be declared then use <%! %> (declaration tag) to declare . Other variable i.e, declared
in scriplet tag will be availabe to service method. Even you can define user defined method level variable with in declaration tag
has explained below
<%!
public void testMethod(){// this method can be called by service method
int count;//method level variable have scope with in testMethod()
.....

}
%>
Hope this is usefull
-arun
[ January 25, 2002: Message edited by: arun boraiah ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic