I am asking a question in which servlet and jsp are included, but this question is about approach not about technical details of jsp, and servlets. it is about better approach for implementing AJAX in a particular scenario.
The scenario is:
step1:- I submit my request to a servlet from my jsp (normal request - non-ajax).
step2:- servlet takes my request, and creates a new thread (as the task is heavy and may consume time) which processes my task, and servlet returns back to the jsp.
NOTE:- thread will update a flag in database when task is complete.
step3:- on the jsp's onload event, a flag is checked, and if its false(false first time), then an ajax-request (asynchronous request) is sent to the servlet.
step4:- servlet will check for the flag in database(which is to be updated by the database and returns back to the jsp.
step5:- now the jsp checks the flag, if true then takes appropriate action else after 10 seconds jsp sends the same request (as in step3).
now my confusion is in step4 and step5.
request is again and again being sent to the server, so is it ok,
or the other side is ok, i.e:-
when ajax request is sent to the servlet, and servlet accepts the request (i.e. in step4), then if flag is false in database, then servlet sleeps for 10 seconds and after waking up again checks flag in database. and when flag becomes true, returns result.
Thanks in advance for any suggestion.