My guess is that the servlet into which your jsp page is parsed is still
cached in memory on your web server.
When you use the <%! ... %> directive (at least with JRun 2.3.3), if you
look at the servlet generated, the variable is declared (implicitly, i.e.
there are no access modifiers before the variable, hence it is public) as a
public instance variable. Hence, the first time the servlet is loaded, that
public member variable is initialized (in this case to str="";).
The next time you request that jsp page, though, that servlet is called
again, but not instantiated into memory if you do it in rapid succession, so
str (an instance variable) is not initialized to "". Hence, 'str' is
concatenated with the values in your for loop and you have 10 more lines 9 -
1.
Mike
-----Original Message-----
From: A mailing list about Java Server Pages specification and reference
[mailto:[EMAIL PROTECTED]]On Behalf Of Alastair GULLAND
Sent: Thursday, March 16, 2000 12:36 PM
To: [EMAIL PROTECTED]
Subject: Re: Something strange
I tried your code out using a different JSP engine WebLogic Server 4.5.1 and
got similar results. I also tried,
<HTML>
<HEAD>
<%! int myNumber=0; %>
</HEAD>
<BODY>
<%
myNumber = myNumber + 1;
%>
<P>Refresh count = <%=myNumber%></P>
</BODY>
</HTML>
and this had a similar effect. I guess this is by design (?) but it's
certainly something to be aware of.
Regards,
Alastair
-----Original Message-----
From: Dennis Huang [mailto:[EMAIL PROTECTED]]
Sent: 15 March 2000 05:30
To: [EMAIL PROTECTED]
Subject: Something strange
Hi:
I have a piece of code look like this:
<%! String str=""; %>
<% for (int i=1; i < 10; i++) { str = i +"<br>"+str; } %>
Before <B>JSP</B> Output <P>
<%= str %>
<P> After <B>JSP</B> Output
This works. The strange thing is each time I click on the Refresh, <%=str
%> outputs more and more repeated values(from 9 to 1). It seems <%! String
str=""; %> does not work from the second time I click on the Refresh button.
Do you know why? I am using Tomcat and IE5.
Thanks,
Regards,
Dennis Huang
___________________________________________________
DATACOM SYSTEMS
34 Waterloo, North Ryde | * 0061-2-9023 5214
Sydney, NSW 1670 | * 0061-2-9023 5300
Australia | * [EMAIL PROTECTED]
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets