tags:

views:

342

answers:

4

When i click a link im calling a servlet. When i click that link multiple time the servlet throws an error (error details not important) Though there are other work around for this fix (Like disable the link once clicked, etc) I am curious is there any way to control this thru request/response Object.

+2  A: 

the error is relevant, having multiple calls to a servlet acting different then one means you have thread safety issues probably due to the way you implemented the servlet

Peter
The error is im creating some large the Object with some logic. This wont be a problem in multi-user environment. But it is causing this problem when parallel request for the same user.
Madhu
+1  A: 

The details of the servlet's error are potentially interesting. The servlet APIs in general should not be throwing errors, my guess is that this is an application error of some kind.

The general principle I try to apply is:

1). We construct the UI to make it difficlut for the user to inadvertantly submit the same request twice (eg. debit my account £100, really don't want to send two such requests. This is where some nift javascript can help.

2). We construct the application to defend against inadvertant double requests, for example by including some kind of identifier on the requests that allow is to spot duplicates.

We do not assume that the UI is perfect, our business application layer has final responsibility for preventing double actions.

djna
Isn't there any builtin method already available when we update HttpResponseObject this problem gets solved
Madhu
When the user hits submit the browser is sending a request object, the servlet is costructing the response object.The browser does "know" that the second click has any relation to the first, hence the two requests have no relationship - unless you code something in Java Script. The servlet receiving the request (which could even be running on a different cluster member) can no nothing about teh previous reuqest (HTTP is stateless) unless you code something.Frameworks such as Strust do have features to address the issue.
djna
Make sense. But still im googleing to find a solution for this problem
Madhu
Do share what you find. I'd like to be wrong :-)
djna
I wouldn't recommend using javascript as a failsafe for not charging a customers creditcard twice...
Tommy
Umm, thats why we have the real defence in item 2 in the answer. The java script is just to be friendly.
djna
One solution is to update the session with some flag. This flag is reset when the particular job is over. So when any request comes check the flag and ignore if this flag is on otherwise do the process.This is a nasty workaround. I wouldn't prefer and i force myself to use this if i didn't find any honest solution
Madhu
A: 

Set a flag in the servlet session scope when entering the servlet and reset it when leaving. If the flag is set when entering, then silently ignore.

You will need error handling in your servlet so a ServletException does not leave the flag set.

Thorbjørn Ravn Andersen
Conceptually, yes, but can be tricky when dealing with clustered instances. May be easier to use a db to track recent requests, ie. police duplicate requests in the business logic rather than at servlet level.
djna
A: 

The error is really, really relevant.

You could have thread safety issues but you can also have a "race-condition", that is, the result of the process depends on the execution order, one of them could give you an error.

(race condition : http://en.wikipedia.org/wiki/Race%5Fcondition)

Herme