Thanks Penner and others.....
Since Servlet is running always ,I hope even if you remove your class file
, Servlet will work.....
Thanks again...
Rajesh
Penner Matt <[EMAIL PROTECTED]> on 02/12/2000 07:34:19
Please respond to "A mailing list for discussion about Sun Microsystem's
Java Servlet API Technology."
<[EMAIL PROTECTED]>
To: [EMAIL PROTECTED]
cc: (bcc: Rajesh K J/India/IBM)
Subject: Answer for: Re: Servlet retains variable for each instance..???
OK, let's get back to the original question here and give Rajesh an answer.
This problem shows one of the main differences, improvements, over standard
CGI. Servlets are constantly running from the time that the JVM loads
them.
They don't get reinterpreted and ran every time they get called.
This means that the init method, and any class variables, gets loaded when
the
JVM loads the servlet. After that they are never initialized again until
the
JVM reloads the servlet.
What's happening is that you are initializing UserName to null. You see
that
on your output the first time. Then you set it to somevalue. You see this
too.
Now what happens the next time you load your browser with the servlet page?
You only ask the JVM for the data for the servlet that is already running.
The
servlet itself has remained running ever since you first started it. So
your
UserName variable still holds somevalue. If you need it reinitialized you
need
to do that in a statement or restart your JVM (which some ISPs wont let you
do).
The easiest way to see how all this works is to put a class variable (int)
and
set it to 0. Then when you call the servlet have it increment it and print
it
out. You'll see that everytime you load the page, or even do a refresh,
the
number will increase.
Hope this explains your question. If not or you are a little hazy on some
things let me know.
Matt Penner
Penner Consulting
P.S. Here is an easy serlvet that you can load up to get a good idea of
what's
happening. Insert whatever standard code that I don't have. I can't
remember
it all off the top of my head. :)
public class testServlet extends HttpServlet{
// Class variable. Get's initialized by JVM.
private int classInt = 0;
public void init()....{
...}
public void doGet(...){
// Local method variable. Get's initialized by method call.
private int methodInt = 0;
PrintWriter out=res.getWriter();
out.println("classInt Value:" + classInt + "<BR>");
out.println("methodInt Value:" + classInt + "<BR>");
out.println("<BR>Increment both values by 1.<BR>");
classInt++;
methodInt++;
out.println("classInt Value:" + classInt + "<BR>");
out.println("methodInt Value:" + classInt + "<BR>");
out.println("<BR>Increment both values by 1 again.<BR>");
classInt++;
methodInt++;
out.println("classInt Value:" + classInt + "<BR>");
out.println("methodInt Value:" + classInt + "<BR>");
} // End of doGet method.
} // End of testServlet class.
Rajesh Kumar wrote:
> Hello all,
> Please go through this code to understand the problem
>
> public class testServlet extends HttpServlet{
>
> private String UserName=null;
>
> public void init()....{
> ...}
> public void doPost(..)..{
> {
> PrintWriter out=res.getWriter();
> out.println("First:"+UserName);
> ..
> ..
> UserName="somevalue";
> out.println("Second:"+UserName);
> }
> }
>
> When I run this Servlet ,output->First: null Second:somevalue
> Run this Servlet in another browser(second time), I got the output as ,
> First:somevalue Second:somevalue.
> How the old value is retained.Each time the servlet is called,it is
> initiated and so the First value should be null.
> Kindly let me know the reason for this behaviour.
>
> This I avoided by declaring the UserName inside the doPost method.
>
> Thanks,
> Rajesh
Quoting "Christopher K. St. John" <[EMAIL PROTECTED]>:
> Ying Sun wrote:
> >
> > If I change private to public, will it act the same?
> >
>
> You're really on the wrong track here.
>
> In this particular case, you need to understand exactly
> what "public" and "private" mean before you can understand
> why your question doesn't really make sense. You can find
> the answer in Sun's Java Tutorial, linked off the Java
> homepage at:
>
> <URL:http://java.sun.com>
>
> A good general Java book, and a good book on Servlets,
> would answer 95% of everybody's questions. For some
> resources available at absolutely no cost whatsoever,
> follow the "Resources" link at the bottom of this page.
>
> It's not that people don't want to answer your questions,
> it's just that the topic is quite complex, and there is
> a lot of free documentation already written than can explain
> much better than a couple of short emails possibly could.
>
>
> -cks
>
>
___________________________________________________________________________
> 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
>
___________________________________________________________________________
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
___________________________________________________________________________
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