Ismael,

Beware of static variables in servlets.

Use application level variables...initialized from the init(). Put this in
any servlet, just as long as its used at least once.

    public void init(ServletConfig config) throws ServletException {
        super.init(config);

       Long counter = (Long)getServletContext().getAttribute("counter");


         if (counter == null) {

             counter = new Long(0);
             getServletContext().setAttribute("counter",counter);

         }


    }


Then throughout your web application in jsp's or servlets you can increment
your counter.

static variables are only for one servlet class, so if you have more than
one servlet or jsp, it won't work.

Regards,

the elephantwalker


-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]]On Behalf Of Ismael
Sent: Wednesday, July 25, 2001 2:33 AM
To: Orion-Interest
Subject: Re: Very Strange problem with a static varible on a jsp


Sorry, But I do not understand what you mean.
What I want is to have only a counter for all the requests.


At 13:35 24/07/2001 -0500, you wrote:
>Instead of static I would use a member attribute, since for servlets
>only one instance exists for a given class.
>
>Ismael wrote:
>
> >  I am making some loading tests, in order to test the loading tool I
> > have written some special jsp to deal verify the number of times a jsp
> > has been called.
> > My jsp is :
> >
> > <%@page contentType="text/html"%>
> > <html>
> > <head><title>JSP Page</title></head>
> > <body>
> > <%!
> > static int client=0;
> > static String attribute_name="client";
> > static Object lock=new Object();
> > %>
> > <%-- This JSP will be used with 4 other jsp to test the correctness of
> > the stress tool
> > --%>
> > <%
> > synchronized (application) {
> > session.setAttribute(attribute_name,new Integer(++client));
> > System.out.println("Entering client "+client);
> >
> >
> > }
> > %>
> > </body>
> > </html>:
> >
> >
> > It is very simple but the ouput I get is
> > Entering client  1
> > Entering client  2
> > Entering client  3
> > Entering client  4
> > Entering client  1
> > Entering client  1
> > Entering client  1
> > Entering client  1
> > Entering client  1
> > Entering client  5
> > Entering client  2
> > Entering client  3
> > Entering client  4
> > Entering client  5
> > Entering client  6
> > Entering client  7
> > Entering client  8
> > Entering client  9
> > Entering client  10
> > Entering client  11
> > Entering client  12
> > Entering client  13
> > Entering client  14
> > Entering client  15
> > Entering client  16
> > ..
> > That is, my static variable it is being ignored. I am using Orion
> > 1.4.5 , Windows 2000 and J2SE 1.3.0_02.
> >
> > Anybody know why this could be happening ?
> >
> > Regards,
> > Ismael
> >
> >




Reply via email to