This is by design, definitely.  When your JSP page is translated to a
servlet, declarations are not local to methods, they're out in the body of
the class.

In other words, your code is analogous to this servlet (pseudocode):

public class StringServlet
{
  // This is your declaration, executed only once.
  String str = "";

  public void doGet(request, response)
  {
    for (int i=1; i < 10; i++) { str = i +"<br>"+str; }

    out.println("Before <B>JSP</B> Output <P>");
    out.println(str);
    out.println("<P> After <B>JSP</B> Output");
  }
}

All you need to do is change your declaration to a scriptlet.  In other
words:

   <%! String str=""; %>

becomes:
   <% String str=""; %>

Michael Prescott

-----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, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to