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

Declarations

 
sri rallapalli
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
I have some doubt abt the declarations. I know that if we declare a variable like <% int x = 3 %>, then it will be in the service method, and local to it, and if we declare a variable as <%! int x = 3 %> then it will be in the class level, here the scope is upto the class, is it possible to access the same variable through out the application? what does class scope means here.

Thanks in advance,
Srini.
 
Arvind Giri
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what does class scope means here.


You can access that variable in jspInit and jspDestroy also. While former was accessible inside service method only.



Is it possible to access the same variable through out the application


Technically Yes! Since there is only one instance of a servlet so this variable will be one for whole application. But it would require a lot of extra stuff and also using context parameter is far better option.

please correct me if I am wrong?
 
Kishore Balla
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you are in SCWCD forum.. I assume that you are SCJP

And a class level variable (non-static/instance variable) can be accessed by all the members of the class (So, in servlet, it can be accessed in init(), service(), doxxx(), destroy() and other user defined methods)
 
sri rallapalli
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi arvind,
U said, technically it is accessible throughout the appllication. so now my question is, if we declare another variable with the same name in another JSP, then how can we access the variables differently.
Sri.
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can we access the variables differently


probaby you will need to do -

some_getter_method_that_returns_instnace_of_the_current_request
_thread's_servlet( ) . variablename !!

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic