Accessing a counter variable in a servlet
5 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Sam_Mueller
Posted On:   Thursday, August 22, 2002 07:32 PM

hi guys I'm encountering a big problem with a countervariable in my servlet. I would be very happy, if anyone can help me. Scenario 1: I have a normal http servlet with a static countervariable. the variable is raised in the dopost method and also printed out in the dopost method. Everything works fine. Scenario 2: I have the same servlet, but with an additional method called MessageReceived. This method is invoked by a MessageHandler and when the message is received it raises the countervariable. if i print out the value in the MessageReceived method in the tomcat window it shows the correct value. but when i want to access the same variable through my dopost method and p   More>>

hi guys


I'm encountering a big problem with a countervariable in my servlet. I would be very happy, if anyone can help me.


Scenario 1:

I have a normal http servlet with a static countervariable. the variable is raised in the dopost method and also printed out in the dopost method. Everything works fine.


Scenario 2:

I have the same servlet, but with an additional method called MessageReceived. This method is invoked by a MessageHandler and when the message is received it raises the countervariable. if i print out the value in the MessageReceived method in the tomcat window it shows the correct value. but when i want to access the same variable through my dopost method and print it out in the browser, it doesn't work.
I've tested it - the same static variable is counted different by the two methods.


Can please anyone explain me why and if there is a solution to access the correct value from the dopost method.





I hope I explained my problem clearly. Thank you in advance for your help




greets,

sam

   <<Less

Re: Accessing a counter variable in a servlet

Posted By:   Christopher_Koenigsberg  
Posted On:   Friday, August 23, 2002 09:10 AM

Is your message handler loading in a different JVM, or at least a different class loader, from the one Tomcat is loading the servlet into? If so, then each JVM or class loader has its own instance of the class, including the static variable.

Re: Accessing a counter variable in a servlet

Posted By:   Sam_Mueller  
Posted On:   Friday, August 23, 2002 04:27 AM

First of all thank you very much for your quick help.


I tried everything, but it didn't help. The static variable is still counted twice. I' ll put the important lines of the code at the end my postig. perhaps someone has an idea, that would help me:


import.....


public class mservlet extends HttpServlet implements ServiceListener,SingleThreadModel
{


....


static int counter = 0;


....


public void messageReceived(ServiceEvent evt) throws Exception
{


....


counter++; // The counter is incremented

System.out.println(counter); // and prints out the right value


....


}


....


public void doPost(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException

{


....


out.println("Counter:" + counter); // 0 is printed out


....


}




as i said before the MessageReceived method is called before the dopost method is called. it's acting as a kind of a service. and when the dopost method is called, not the variable that was raised before is accessed, but a new instance of it. is there a possibility to access the raised variable in my dopost method?



thanks in advance for your help



greets


sam

Re: Accessing a counter variable in a servlet

Posted By:   mellon_sun  
Posted On:   Thursday, August 22, 2002 10:07 PM

Normally, the system makes a single instance of your servlet and then creates
a new thread for each user request, with multiple simultaneous threads running
if a new request comes in while a previous request is still executing. This
means that your doGet and doPost methods must be careful to synchronize
access to fields and other shared data, since multiple threads may be trying to
access the data simultaneously.


If you want to prevent this multithreaded access, you can have your servlet implement the SingleThreadModel
interface.


If you implement this interface, the system guarantees that there is never
more than one request thread accessing a single instance of your servlet. It
does so either by queuing up all the requests and passing them one at a time
to a single servlet instance, or by creating a pool of multiple instances, each of
which handles one request at a time. This means that you don¡¯t have to worry
about simultaneous access to regular fields (instance variables) of the servlet.
You do, however, still have to synchronize access to class variables (static
fields) or shared data stored outside the servlet.


Synchronous access to your servlets can significantly hurt performance
(latency) if your servlet is accessed extremely frequently. So think twice
before using the SingleThreadModel approach.

Re: Accessing a counter variable in a servlet

Posted By:   Anonymous  
Posted On:   Thursday, August 22, 2002 09:27 PM

Have you tried with volatile modifier?

Re: Accessing a counter variable in a servlet

Posted By:   michael_dean  
Posted On:   Thursday, August 22, 2002 09:22 PM

Sounds like you may have discovered the concept of variable shadowing. Is there an intermediate class between HttpServlet and your servlet (i.e. one that contains the code for the MessageReceived method) or are you extending HttpServlet directly?



The only case I know of that allows the "same" variable to have two different values depending on which method accesses the variable is the case where a variable is "shadowed" by another variable that's defined in a subclass (similar to overriding a method). If this is the case, the methods defined in the superclass will access the superclass's variable and the methods defined in the subclass will access the subclass's variable. In other words, there is no such thing as overriding a variable. If this is the case, take out the declaration of the variable in the subclass (and just do an assignment, if necessary, in init() or something).



If this is not the case, then you are accessing different variables (perhaps static variables in two different classes).



One other word of warning... Since servlets are multi-threaded, make sure you synchronize access to the shared data as appropriate. I'm sure you've thought of this, but reminders never hurt. :)

About | Sitemap | Contact