Definitely yes...
Public is not concerned with instantiation of the servlet and initialization
of the member variables. It tells only the accessibility to other classes.
>From: Ying Sun <[EMAIL PROTECTED]>
>Reply-To: "A mailing list for discussion about Sun Microsystem's Java
> Servlet API Technology." <[EMAIL PROTECTED]>
>To: [EMAIL PROTECTED]
>Subject: Re: Servlet retains variable for each instance..???
>Date: Fri, 1 Dec 2000 14:32:04 -0500
>
>If I change private to public,will it act same?
>Thanks,
>chris
>
>Bo Xu wrote:
>
> > 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
> >
> > [...]
> >
> > Hi :-) I am not sure, but from Servlet spec2.2 and several emails from
> > Servelt-List:
> >
> > # UserName is a private instance field in your Servlet class, so the
>line
> > *private String UserName=null;* will only work when Servlet
>container makes
> >
> > a new instance of your Servlet class.
> >
> > # if we have n Servlet definitions, then normally, we will have n
>instances of
> > our
> > Servlet class. Servlet container will use these instances to make
>threads
> > for
> > client accessing, and after this client accessing has been
>responsed,
> > normally
> > this instance will not be *destroyed* -> it can be used in next
>time. so
> > now your
> > UserName is also there, and still refrences to a String whose value
>is
> > "somevalue"
> >
> > # Servlet container will make a new thread from one of these instances
>for
> > every
> > client accessing, if it happens that the two threads for your two
>client
> > accessings
> > are from the same instance, then perhaps we will get the result
>mentioned in
> > your
> > email. hope my guessing is right :-)
> >
> > Bo
> > Dec.01, 2000
> >
> >
>___________________________________________________________________________
> > 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
_____________________________________________________________________________________
Get more from the Web. FREE MSN Explorer download : http://explorer.msn.com
___________________________________________________________________________
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