Yes, that is how it is supposed to work.
The syntax <%!...%> creates a class variable for the servlet created from
the JSP. It is the same as if you typed the following:
public class MyCustomServlet
{
int var = 0;
public void _jspService(HttpServletRequest request, HttpServletResponse
response)
throws IOException, ServletException
{
var++;
out.println( var );
}
}
Keep in mind that the class is instantiated at the whim of the application
server. You can't assume that each invocation will create a new instance of
the class. For efficiency the application server will keep the original
instance around in memory and simply re-invoke the _jspService method as it
needs to.
You also can't assume that the application server won't destroy the instance
of the class and subsequently create a new instance as it is needed. The
server may also keep multiple instances of the servlet class around in
memory at the same time.
The best advice is to only declare constants using the <%! %> syntax. Avoid
creating mutable class variables using this technique. It is very difficult
to get right.
As a postscript, the syntax <%! var++ %> is incorrect and as far as I know
won't compile. The <%! %> scriplet tag is for creating class declarations
and the syntax of the following is incorrect:
public class MyCustomServlet
{
int var = 0;
var++;
public void _jspService(...
}
I hope this helps.
-----Original Message-----
From: Liza J Alenchery [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, May 31, 2000 1:33 PM
To: [EMAIL PROTECTED]
Subject: Problem with variables.
I have declared a variable in my JSP page as
<%! int var = 0 ;
%>
and somewhere else, I do
<% var ++ ; %>
and also
<%! var++; %>
The problem is that when each time I come to the page in the browser,
the previous values are held in the integer var. It does not get
reset to 0. What is happening here?
thanks in advance
liza
__________________________________________________
Do You Yahoo!?
Send instant messages & get email alerts with Yahoo! Messenger.
http://im.yahoo.com/
===========================================================================
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