Hi, Wai Mun

Create a class named, say, HitCounter, with a method Increment(String):

        public class HitCounter
        {
            Hashtable pageCounts = new Hashtable();

            public void Increment(String s) {
                int[] count = (int[])pageCounts.get(s);
                if (count == null) {
                    count = new int[1];
                    count[0] = 0;
                    pageCounts.put(s,count);
                }
                count[0]++;
            }

            // method to access counts here
        }

Note that since Hashtable instances are thread-safe, your counter variables are
automatically protected from concurrent access.  You also need to define a
method to access the counts.

You can then embed this class in any jsp or servlet that needs to count accesses
to URIs it handles.  I believe a more general way to handle this is via
server-side includes, but exactly how that is done is server-dependent.

Hope this helps.

Regards,
Noel Lecaros

Wai Mun wrote:

> Yes! how do i do that? how do i synchronize access to the counter variable?
> do u have a snippet to show?
>
> thank you very much!
> ----- Original Message -----
> From: "Noel E. Lecaros" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Sunday, May 06, 2001 12:18
> Subject: Re: file lock
>
> > Hi, Wai Mun
> >
> > Do you mean that every page access generates one file access?  Why don't
> you put
> > the counter in the servlet context and synchronize access to the counter
> > variable?
> >
> > Regards,
> > Noel Lecaros
> >
> > Wai Mun wrote:
> >
> > > if i need to create a counter for a web page, how do i ensure that there
> > > will be no other process that try to access the couter file at the same
> time
> > > for updating the value in the file? i.e. how do i lock the file for
> > > exclusive writing?
> > >
> > > but if i'm able to lock the file, then how can i effectively update the
> > > counter, say there are ten people accessing the web page at the same
> time?
> > >
> > > pls advice, thank a lot!
> > >
> > >
> ___________________________________________________________________________
> > > 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

___________________________________________________________________________
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