views:

91

answers:

3

If i click a link ia m calling a servlet.

Is there any possibility to track the duplicate request thru filter or something so that i can avoid hitting the servlet?

+2  A: 

Typically you put some sort of "request identifier" in the link, e.g. a GUID. You either lazily keep track of the identifiers you've already seen (and ignore anything in that set), or keep track of the ones which you've generated but not seen (and ignore anything not in that set).

Jon Skeet
This could bite you if you're sharing links with people...
Steve Brewer
@Steve - this is typically a problem with form posts, where the GUID is included as a hidden input rather than as a parameter in the URL.
Nick Holt
@Steve: I'd say that's a problem inherent in the question. The OP needs to be careful what he does this with. There will always be some inherently "unshareable" links though.
Jon Skeet
A: 

Do you really want to do this for EVERY request? If you don't, use the first solution in Jon's answer and ignore things that don't have a guid.

You can do this in javascript pretty easy. After any link is clicked, prevent it from being clicked again: (using some YUI):

YAHOO.util.Event.onDOMReady(function(){
  function killIt(e){
    YAHOO.util.Event.stopEvent(e);
  }

  var anchors = document.getElementsByTagName('a');
  for(var i = 0; i < anchors.length; i++){
     YAHOO.util.Event.addListener(anchors[i], 'click', function(e){
       var el = YAHOO.util.Event.getTarget(e);
       YAHOO.util.Event.removeListener(el, 'click'); // kill existing handle
       YAHOO.util.Event.addListener(el, 'click', killIt); // kill any future clicks
     });
   }
});

you could also make then anchor disabled when you click on it. A little different. Straight forward to only implement this on anchors you want to single-click-only-enable.

Steve Brewer
A: 

There is absolutely no reason to use a huge Javascript framework just to dynamically disable a link.

<a href="/test" onmouseup="this.href='';" />

Done.

Civil Disobedient