JSP variable scope issue

  • Permalink
  • submit to reddit
  • Email
  • Follow


I have a page of the form:

== Start ==

<%! int someVariable = 0; %>
<%= someVariable %>
<% someVaribale++; %>

== End ==
Now when I run the page the first time I get 0 then each subsequent
time a higher number... Why?  I should think the scope of someVariable
is local and to make the scope application or session I would need to
do something special. I even clear the cache on firefox and the bloody
thing still increments.

I'm using Netbeans  6.7.1 with Glassfish v3 if it makes a difference.
0
Reply ken473 (54) 12/9/2009 9:52:25 PM

See related articles to this posting


Ken wrote:
> I have a page of the form:
> 
> == Start ==
> 
> <%! int someVariable = 0; %>
> <%= someVariable %>
> <% someVaribale++; %>
> 
> == End ==
> Now when I run the page the first time I get 0 then each subsequent
> time a higher number... Why?  I should think the scope of someVariable
> is local and to make the scope application or session I would need to
> do something special. I even clear the cache on firefox and the bloody
> thing still increments.

"<%!" opens a declaration block, so 'someVariable' is an instance variable of 
the servlet.  Servlet containers like to reuse servlet instances whenever 
feasible and permitted.  So you have the same instance with the same member 
incrementing.

Don't use scriptlet in JSP.

-- 
Lew
0
Reply Lew 12/10/2009 12:13:48 AM

Ken wrote:
> I have a page of the form:
> 
> == Start ==
> 
> <%! int someVariable = 0; %>
> <%= someVariable %>
> <% someVaribale++; %>
> 
> == End ==
> Now when I run the page the first time I get 0 then each subsequent
> time a higher number... Why?  I should think the scope of someVariable
> is local and to make the scope application or session I would need to
> do something special. I even clear the cache on firefox and the bloody
> thing still increments.
> 
> I'm using Netbeans  6.7.1 with Glassfish v3 if it makes a difference.
"<%!" starts a class-level declaration. so someVariable is declared at 
the JSP servlet object level.  you could use <% int someVariable = 0; 
%>.  The better alternative is to not use scriptlets at all, and use 
some form of MVC architecture.



-- 
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
0
Reply Daniel 12/10/2009 12:18:10 AM
comp.lang.java.programmer 51973 articles. 38 followers. Post

2 Replies
259 Views

Similar Articles

[PageSpeed] 44


  • Permalink
  • submit to reddit
  • Email
  • Follow


Reply:

Similar Artilces:

Variable scope issue
I have a function foo () in a file called funcfoo.php called from my main program file main.php, which has an include for that file. Foo () contains a line: echo $bar; Now I have a (global) variable $bar which is set in main: GLOBAL $bar; $bar = 1; When I call foo () from main it doesn't echo the value 1. (How) can I make $bar visible to foo () without explicitly passing it as a parameter ? Is the only way putting the entire foo function body in the same file as main ? TIA Pjotr Pjotr Wedersteers wrote: > I have a function foo () in a file called funcfoo.php called from my...

How do I scope a variable if the variable name contains a variable?
I can do this: my @bar; #explicit name - fine but not this: my $foo = 'bar'; #fine my @{$foo}; #oops Perl complains, "Can't declare array dereference..." Does anybody know the syntax to scope a variable name that includes a variable? Thanks! In article <e4c916dd.0405181656.5d92e96e@posting.google.com>, David Filmer wrote: > I can do this: > > my @bar; #explicit name - fine > > but not this: > > my $foo = 'bar'; #fine > my @{$foo}; #oops The above is how you deference...

scope issue of variable in iterator
--------------050008030408040106030702 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit mkhan@mkhan:~$ irb irb(main):001:0> batman = 'robin' => "robin" irb(main):002:0> [ 'cat', 'dog', 'horse', 'chicken' ].each { |batman| puts batman } cat dog horse chicken => ["cat", "dog", "horse", "chicken"] irb(main):003:0> puts "Now you might think this would say 'robin', but it says: #{batman}" Now you might think this would say 'robin', but it sa...

BASH : variable scope issue
Hi there, Could someone help me to solve this problem ? When i run the code below, i would get the value of x initialized inside the 'while' loop. Obviously i can't get it because of the sub-shell run by the pipe command. Is there another way to get it ? I've tried to export the variable before the sub-shell ... but it doesn't work. Source code : x=TEST cat titi | \ while read a b do echo $a $b if [ "$a" != "VAL1" ] then echo "VAL1 matched" x=$a fi done echo $x File titi VAL1 LIGNE1 VAL2 LIGNE2 Thank you in advance... C...

Variable Scoping issues during evaluation
Strange behaviour with the code below (main) => NIL doesn't match array dimensions of #(0 0 0 0 0 0 0 0 0) . [Condition of type SIMPLE-ERROR] (eval-when (:compile-toplevel :load-toplevel :execute) (defparameter vars nil) (defparameter predictors nil) (defparameter dependent nil) (defparameter intercept 1) (defparameter db '(("STUDYID" "CASECNT" "AGE" "GENDER" "RACE3" "HYPER" "DIAB" "SMOKE" "NUMCIG" "YRSSMOKE" "FATINDEX" "FAMMI"...

Variable scope issue #2
This is seriously blowing my mind. $a = array('a', 'b', 'c'); for ($i=0; $i<=count($a); $i++) { echo 'In FOR: '.$a[0].' - '.$a[1].' - '.$a[2]."\n"; if ($a[$i] = 'a') { echo 'In IF: '.$a[0].' - '.$a[1].' - '.$a[2]."\n"; } } Ok, I expected the above to output: In For: a - b - c In IF: a - b - c In For: a - b - c In For: a - b - c But I get this: In For: a - b - c In IF: a - - In For: a - b - c In For: a - b - c For some reason $a[1] & $a[2] can be seen outside the IF but inside t...

design issues: variables at class scope
Hello, It seems to me that using too many variables at class scope in C++ (e.g. private data members) can be just as bad as having a C program with lots of global variables. This is especially true for large classes with lots of methods. The worse part is that even if conventions are used to assign names to class data members to so as to distinguish them from other variables, then it still becomes difficult to tell from the class definition which methods use which variables without looking at the particular implementation of functions. That is, when we look at a header file, it is fairl...

Class-level variables
Here's an obscure bit of Python semantics which is close to being a bug: >>> class t(object) : .... classvar = 1 .... .... def fn1(self) : .... print("fn1: classvar = %d" % (self.classvar,)) .... self.classvar = 2 .... print("fn1: classvar = %d" % (self.classvar,)) .... .... >>> t1 = t() >>> t2 = t() >>> t1.fn1() fn1: classvar = 1 fn1: classvar = 2 >>> t1.fn1() fn1: classvar = 2 fn1: classvar = 2 >>> t2.fn1() fn1: classvar = 1 fn1: classvar = 2 ...

File handle Variable scope issue
Hello All, I am somewhat new to perl and I am having some difficulty with a cgi script that I am making. This script is supposed to read a html file and replace certain sections of it with its own variables. Code similar to this works great. as long as $html is in the actual script. $variables = "Hello World"; $html = '<h1>'.$variables.'</h1>'; sub print_html { print "Content-type:text/html\n\n"; print <<End_of_Bottom; $html End_of_Bottom } This outputs <h1>Hello World</h1>. The issue I am having is getting the $variable...

Variable scoping
Hi All, Sorry if I'm just repeating other questions, but I can't find an answer for this particular problem: I have my main index.php file, which looks so: <?php include "includes/header.php"; #defines a global variable called $lang include "includes/functions.php"; # some required functions safe_include "main_page.html"; # user-defined function that tries to # include main_page.html.$lang or gives a warning message echo "Language available to index.php at line XYZ is -$lang-"; #-- to check empty value inclu...

store variables in a jsp page scope with the spring framework
I want to store some variables (e.g. number of rows per table) at page scope in Spring framework so that each page has its own group of variables. What is the proper way to do that with the spring 2.5.x framework? This has compile errors. What is the proper way to initialize pageContext? import javax.servlet.jsp.PageContext; @Override public ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { PageContext pageContext; Object o = pageContext.getAttribute("com.mycompany.pageId"); Description Resource...

get static variable from class into JSP tag scope
Hi All,I have a static variable in a class that I wish to share with some ofmy JSP code. Can I use the <jsp:useBean> (or any tag?) to get that mapinto the scope of my JSP tags?Thanks for any assistance,Rob:) Robert Mark Bram wrote:> Hi All,> > I have a static variable in a class that I wish to share with some of> my JSP code. Can I use the <jsp:useBean> (or any tag?) to get that map> into the scope of my JSP tags?<jsp:useBean> requires that the bean be in scope, i.e., entered into the context of the page, request, session or application, e.g., via<http://jav...

array not accessible outside a loop? variable scope issue?
I have following two files which I like to compare and write 1 or 0 in a new resulting file depending on whether $3 of sent.txt is equal to $1 of received.txt or not. I have written a small script which is not working pefectly. Maybe someone can give me an advice. ------------------sent.txt------------------- 11 5 468 12 5 477 13 1 130 14 1 84 15 1 90 16 1 44 17 1 97...

Having trouble understanding function scope and variable scope
Hello again, I'm having trouble understanding why the element types in the form I'm using aren't defined in my function. Below is the code (it's a nonsense page that I'm using just to get my understanding correct). In the function displayFormElements(), the form.type property isn't defined, but yet the form.length was because the for loop only goes for 3 iterations. Is there something wrong with how I'm passing the form to the function? Thanks for any help, Andy <html> <head> <script type="text/javascript"> function foo() { docum...

Export local variable to global variable scope?
Hello, I have a function, e.g. function foo() { include("bar.php"); } and the bar.php contain contents e.g. $global_v1 = "abc"; $global_v2 = "def"; I want to execute function foo(), but at the same time, let the variables declared in bar.php to have global scopem is it possible? Thanks. howa wrote: > Hello, > > I have a function, e.g. > > function foo() { > include("bar.php"); > } > > and the bar.php contain contents e.g. > > $global_v1 = "abc"; > $global_v2 = "def"; > > I w...

How to make variable have package scope but not global scope.
I am a beginner in package development in Mathematica. I need a package because I have several functions that call each other that depend on the same data. Instead copying and passing the data around, I want to have the functions all access the data directly. My package currently looks like: f[A_, x_] := Module[{}, b = A.x; Return[{b,A}]; ] g[A_, l_] := Module[{}, .... (*You get the picture*) ] So my question is, how can my variables have package scope, but not global scope? What is the best way to implement this? Thanks, Jeremy jeremito wrote: > I ...

Variable scope: how fortran deals with undeclared variables in subroutines?
Hi everyone, I'm trying to fix a program which have many subroutines and many problems with undeclared variables. I know the best way to do this is to declare them in every subroutines or pass by parameters, but the program is very complicated, and first I am trying to understand what happens with the variables, because I'm afraid to mess something if I make many changes. Sometimes a variable is used in a subroutine A, without being declared first, and it has a value. Where this value come from? - the last time a variable with the same name is declared out of the routine? (it doesn...

assign JavaScript variable to Java variable problem in JSP
If I assign Java variable a to javascript variable x, it is fine. <% int a = 10; %> var x = <%= a %>; alert(x); But if I do the other way around, then it has 500 error. any ideas?? <% int b; %> <% b %> = x; thanks! Matt wrote: > If I assign Java variable a to javascript variable x, it is > fine. > <% > int a = 10; > %> > var x = <%= a %>; > alert(x); > > But if I do the other way around, then it has 500 error. > any ideas?? > > <% > int b; > %> > <% b %> = x; With server-side ...

How to make variable have package scope but not global scope. #2
I am a beginner in package development in Mathematica. I need a package because I have several functions that call each other that depend on the same data. Instead copying and passing the data around, I want to have the functions all access the data directly. My package currently looks like: f[A_, x_] := Module[{}, b = A.x; Return[{b,A}]; ] g[A_, l_] := Module[{}, .... (*You get the picture*) ] So my question is, how can my variables have package scope, but not global scope? What is the best way to implement this? Thanks, Jeremy ...

assign JavaScript variable to Java variable problem in JSP
If I assign Java variable a to javascript variable x, it is fine. <% int a = 10; %> var x = <%= a %>; alert(x); But if I do the other way around, then it has 500 error. any ideas?? <% int b; %> <% b %> = x; thanks! "Matt" <jrefactors@hotmail.com> wrote in message news:ba8a039e.0409242152.b129c9c@posting.google.com... > If I assign Java variable a to javascript variable x, it is > fine. > <% > int a = 10; > %> > var x = <%= a %>; > alert(x); > > But if I do the other way around, then it has 500...

AGS issues issues issues
Using AGS3 on my apple IIc all I can reliably do is crash any tips? On Sunday, November 18, 2012 5:42:33 PM UTC-6, Kevin wrote: > Using AGS3 on my apple IIc all I can reliably do is crash any tips? I recommend tracking down the author and giving him a really good piece of = your mind about how little he's updated that project recently... oh wait (l= ooks in mirror) that would be me. ;-) Yes, actually I developed it with a Rev-0 //c so I know it can work. :-) T= ry toggling the "debug bootstrap" parameter. That tends to help a bit. I need to scrub through the c...

Scoping Issue
Hi there, My HTML calls two javascript functions: 1. paint a webpage with textboxes and drop down menus; 2. populate the webpage with the XML DOM values. The paint function, #1, just consists of two functions: drawTextBox and drawDropDown menu. Inside of these functions I just call document.write(input type=select OR type= text ...). I need to add onclick functionality to the drop-down menus so that when they're clicked, they access a particular URL that the XML DOM specifies. My problem is that I don't know how to add this onclick functionality. If I try to add...

variables and scope
Hi all, I would be asking my instructor this question be she is not available on the weekends... How do I make "n" available from anywhere inside the class? I need to keep the values in n from call to call. When calling the method "pt", "n" acts as if it was not declared above. I get -1 instead of what I wish for ... 99. Thanks in advance, Mike class Test n=100 def pt puts "hi there" n=-1 end end t = Test.new puts t.pt -- Posted via http://www.ruby-forum.com/. take note of the usage of @ for ...

A variables variables
how can I declare a variable with another variable name? for example I will use PHP: $a= "hello"; $a_hello="baybay"; print ${'a_'.$a) //output: baybay how can i do it with no Arrays using python thanks! On Aug 23, 7:25=A0pm, Gandalf <goldn...@gmail.com> wrote: > how can I declare a variable with another variable =A0name? > > for example =A0I will use PHP: > > $a=3D "hello"; > > $a_hello=3D"baybay"; > > print ${'a_'.$a) =A0//output: baybay > > how can i do it with no Arrays using =A0python...