0

I'm quiet new to this JSTL and JSP programming, and cannot figure out how this scenario should be solved. Thats why I hope any of one can inlight me and help me towards the right direction.

I have a button on a page. If the user click a second time on that button within 5 minutes from the last click, then I want to prompt the user with a confirm dialog, asking them if they are sure about this action.

JSP:

<c:choose>
<c:when test="${actionBean.warningDialog}">
    <!-- Show warning dialog -->
    <stripes:submit name="send" value="Send" style="height:20%;"
        onclick="return confirm('Last click is lesser than 5 minutes ago.\nAre you sure you want to click again?','Are you sure you want to click?')" />
</c:when>
<c:otherwise>
    <!-- Don't show any warnings -->
    <stripes:submit name="send" value="Send" style="height:20%;" />
</c:otherwise>

ActionBean:

public boolean isWarningDialog(){
ClickBean bean = getClickSettings();
return isClickExpired(bean.getLastClick(), new Date());
}

private boolean isBroadcastExpired(Date lastSentDate, Date currentDate) {
    this.differenceInMilliseconds = currentDate.getTime()-     lastSentDate.getTime();
    return differenceInMilliseconds < minimumBroadcastWarningTime;
}

@HandlesEvent("broadcast")
public Resolution send(){
    //...Send message and return ForwardResolution...
}

So my problem currently is that the isWarningDialog is calculated when the page is loaded, and NOT on click. The idea is that when the user click on the button, then the page should calculate if a warning should be prompted or not.

When the user click send, it will end with a ForwardResolution to the same page, which means that the following scenario will occur:

  1. User clicks the button, no dialog shown, and the user will be forwarded to the same page.
  2. Now, after forwarded, the isWarningDialog is calculated and LastClick will be lesser than 5 mins.
  3. The user waits 6 mins, until he click on the button 2nd time.
  4. The user should not get any warning dialog, but currently he would, be cause the time is calculated right after the previous click <-- This is the issue.
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
grmihel
  • 784
  • 3
  • 15
  • 40

1 Answers1

1

The solution is quite simple

  1. The first time the user clicks on the button, store the time in a session variable (request object can be made available in your backing bean with jsp:setProperty )

    long timestamp = System.currentTimeMillis();        
    request.getSession.setAttribute("firstClickTime", timestamp);
    
  2. The second time just check that firstClickTime is not null (means the button has already been clicked) and make sure that the time difference is less than 5 minutes

    long firstTimeClick = request.getSession().getAttribute("firstTimeClick");
    if( firstTimeClick!=0 ){ //if not 0, it's the second click
    
    //make sure that the user clicked within 5 minutes
    long currentTime = System.currentTimeMillis();
    if(  currentTime - firstTimeClick <= (5 * 60 * 1000) ) {//
     //initialize your warning dialog prompt 
    }
    //reset your session variable
    request.getSession().setAttribute("firstTimeClick", 0);
    
    } 
    

UPDATE: Alternatively, make your backing bean session scoped (@SessionScoped) and add an additional field and corresponding getter/setter methods for a timestamp field. This way you don't need a request object at all.

dsp_user
  • 2,061
  • 2
  • 16
  • 23
  • I see that I missed that in my question, but submit button has a @HandlesEvent("send") in my actionbean, which should be executed if the user click OK on the confirmbox. I assume your code example is placed in the actionbean? – grmihel Mar 09 '16 at 13:02
  • You should place this code in whatever handler defined for your submit event (so yes in your ActionBean). – dsp_user Mar 09 '16 at 13:07
  • It might be my lack of JSP knowledge, but this request.getSession where is it from? My action bean method with the @HandlesEvent("send") does not seem to have a reference to this variable? In your cde example, if that code is running in the actionbean, how do you initialize the confirm dialog? As far as I see, this is created in the frontend JSP, and not in the backend java bean. – grmihel Mar 09 '16 at 13:12
  • The request (and other implicit objects) object is accessible directly only within a jsp page (or servlet). You can use scriptlets to access it ( <% JAVA_CODE_HERE %>). Alternatively, you'll have to modify your bean class (see http://stackoverflow.com/questions/11984371/how-to-access-jsp-implicit-objects-like-request-response-in-java) to be able to Access it (recommended) – dsp_user Mar 09 '16 at 13:20
  • I think I got it :-) But how do init the confirm box from a backing bean? Currently I have placed it in the onclick event on my button. – grmihel Mar 09 '16 at 14:34
  • The onclick is fired before the page is submitted so you'll probably have to put the confirm dialog init code on page load. – dsp_user Mar 09 '16 at 14:50