File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes HTTP requests and threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "HTTP requests and threads" Watch "HTTP requests and threads" New topic

HTTP requests and threads

Steve Snodgrass

Joined: Jun 12, 2001
Posts: 27
I just came up with the following solution to a problem we are experiencing and was wondering if anyone can see any holes in it.
What we have is a jsp page containing the useBean tag with session scope. We post all of the form data in our application to this jsp page which in turns calls a method on the class referenced by the useBean tag (called requestProcessor for future reference) to process the requests. As a result, if an anxious user clicks the submit button many times (I know this can be prevented with javascript but for the moment lets assume it can't) then several requests are sent to requestProcessors processRequest method.. There is no point in processing any request except for the last one since the responses will never make it back to the browser anyway. In order to only process the last request I did the following:
1) Declare a variable global to the requestProcessor which all threads will have access to (called globalCount)
2) Declare a varable local to the processRequest method which each thread will have its own copy of. (called localCount)
3) Place all of processRequest in a synchronized(this) block expect for two lines which are:
localCount = globalCount;
4) In the synchronized section, if the localCount equals the globalCount, we know that this is the last request and should be processed, otherwise we ignore it.
Does this sound like a workable solution? Any other ideas as to how to avoid processing these requests whose responses are essentially dead?
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
"Declare a variable global to the requestProcessor which all threads will have access to (called globalCount)"
It seems to me that if you have created a new requestProcessor object for the session, you don't need to be so complicated, since only requests from that user will see that session.
How do you intend to "ignore" a request?
I think JavaScript is your best bet.
Steve Snodgrass

Joined: Jun 12, 2001
Posts: 27
Javascript is certainly our first line of defense. However, we have learned to never rely on Javascript. Every Javascript routine I have ever seen which prevents multiple submits breaks when the user hits the STOP button. Trapping the STOP button is not something we want to get involved in.
Anyway, when i say ignore the request I mean that we will not inspect its parameters and take action as a result of their contents. Some actions involve fairly significant database queries which can take quite a while (several seconds). No point in doing such a query any more often than absolutely necessary.
Thanks for your input!
I agree. Here's the link:
subject: HTTP requests and threads
jQuery in Action, 3rd edition