Charles Chen wrote:

> Contact:   Tel: 2726  -  New Media Systems, 1st Floor South, Queens Walk
> [...]
> Bo Xu,
>
> I can see during the weekend you have learnt a lot, even changed your [...]
> style to *...*.
>
> I don't think anyone will be lucky to get the CD you promised, because I know by
> the fact that servlet alone (meaning without persistency) CAN NOT provide a true
> persistent counter. This is because the obvious reason that web server can be
> shut down and your servlet can be reloaded. Your counter variable (no matter if
> it is an instance variable or class variable) only has the maximum life span
> between the time the class is being loaded into memory, to the time the instance
> is garbage collected.
>
> However, if you are opt for the persistent solution, the safest way is to update
> the counter directly from the persistent media (ideally a database of some
> sort). That is, every time you need to increase the counter, increase it from
> the persistent media (with proper locking of course). This will GUARANTEE that
> the counter reflects the true value. This solution is designed for those who
> does not trust (or maybe just not sure) that a servlet has one and only one
> inctance.
>
> Another more elegent solution is to implement a counter JavaBean and load it
> into memory when the web server is started. Your servlet will simply access this
> bean instance to read/update the counter. This way you don't even need to worry
> about anything that you are not sure about servlet. There are many ways to
> ensure that the JavaBean instance is unique during the life span of the web
> server, and obviously you still need to implement the bean as a persistent
> object.
>
> Cheers,
>
> Charles
> [...]

Thanks for your email :-)   and after I sent a email to reply Milt's email
yesterday,
I also guess perhaps the safest way is:
   #   persisting counter everytime JUST after it has been  updated.
   #   and I guess it is the ONLY safe way to load it in *static/non-static
        initialization block* in MyServlet class.

the following is my updated code, if it is wrong, please correct me :-)

public class MyServlet extends HttpServlet
{
private static Object   sync        =new Object();
private static int          counter   =0;
   {  // or use static initialization block --> static  {
   synchronized(sync){
   counter=...;   //load counter from persistent state
   sync.notifyAll();
   }
}

...

    //service/doGet/doPost {
      ...
      synchronized(sync){
      counter++;
      ... //save counter into persistent state
      sync.notifyAll();
      }

...
}


Thanks in advance!    and I will continue to try my best to reduce
the number of *...* in my email :-) :-)


Bo
Nov.06, 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

Reply via email to