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 51894 articles. 38 followers. Post

2 Replies
247 Views

Similar Articles

[PageSpeed] 15


  • 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...

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...

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...

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...

scoping issue
okay so the code in question looks like: namespace util { template<typename C> inline void mmapw(C *& ptr, size_t length, int prot, ...

Variable Scope ?
Hi there, I'm not sure if this is a C question or a compiler specific question. However since the issue seems to be occuring on all compilers I try it on I think it may be a language question. Anyways to the point... I have 2 seperate sources main.c and module.c main is our main application and module.c compiles to a dynamically linked library main has a circular buffer defined in the global scope, this is intended to be a sort of scratchpad for communication between main and module. The problem I am having occurs at runtime. When I run the application, the library cannot find the ci...

Variable scoping
I've been away from this list for a while, so I don't know what the current thinking is behind the scoping rules for block-local variables, and whether they're likely to be changed. But I've just been bitten by this. Have a look at the following code, and see if you can see what's wrong with it just by inspection. It accepts TCP connections on a socket, and starts a server in a thread for each one. It's a nice pattern for TCP servers. ----------------------------------------------------------------------- require 'socket' module MyModule def run puts &qu...

Variable scope
I'm new to XSLT and I've been trying to figure out a solution for the last couple of days. The XML & XSLT stylesheet are below. Basically I want to declare some language (English, French, etc.) variables at the beginning of the stylesheet, however I keep getting variable scope errors. The language strings are specific to this template. There aren't many since most of the data is coming from an application through XML. Below is a simplified example. I'm trying to accomodate multiple languages without having to maintain multiple stylesheets. Any ideas on how to accomp...