Search This Blog

Friday, 18 March 2016

Reverse AJAX - what and how ?

I had only heard about the concept of reverse AJAX before. So when I started looking at servlets async behavior I was tempted to check out this concept.
AJAX is basically an asynchronous technique of communication used in browsers to allow sections of page to communicate with the server without a complete reload. Here the point to note is that all requests are of the type Client requesting some data from the server. What if the server wants to send some data to the client ? For instance server wants the client to know that a particular event of the client's interest has occurred. How does the server inform the client ?
This is where reverse AJAX comes into the picture. Reverse Ajax allows the server to push information to the client.  The point to remember is there is no way for the server to initiate communication with the client. The server has to use AJAX techniques to achieve reverse AJAX.
The simplest way to achieve reverse AJAX is polling. Consider that you have a web application where you need to ensure that the session times out after exactly 5 minutes of inactivity.
In the absence of reverse AJAX you would check every client request to see if this has occured after 5 minutes of inactivity. In that case redirect user to logout page.
Problem with this approach is that the user's page stays open and visible to others until somebody tries to perform an activity. With polling, this problem can be avoided.
Consider the below index.jsp page
  var pollAndCheck = function () {
   document.getElementById("sessionActve").innerHTML="Checking..." ;
    if (window.XMLHttpRequest) {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
    } else {  // code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    xmlhttp.onreadystatechange=function() {
      if (xmlhttp.readyState==4 && xmlhttp.status==200) {
  //check every 2.5 minutes
  window.setTimeout(pollAndCheck, 2.5*60*1000);

session = request.getSession(false);
if (session != null) {
session = request.getSession(true);
System.out.println("Session has been activated at " + session.getCreationTime());

    <h3>Hello, This is a sample page</h3>
    Is your session active ?? <div id="sessionActve"></div>  

The page is a simple welcome page.
  1. On server side it initializes the user session. 
  2. It also includes a client side code that checks if user session is active. 
  3. The code executes every 2.5 minutes. 
Next is the servlet that is responsible for providing this information to the browser.
public class SessionCheckServlet extends HttpServlet {

  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    System.out.println("Request received for a check at " + new Date());
    HttpSession session = req.getSession(false);
    PrintWriter writer = resp.getWriter();
    if (session == null) {
    } else {
I configured the session timeout and the servlet in my web.xml:
      <!-- This is in minutes -->


Now when I run the code:
Thus using polling the client can be informed by the server when the session timeouts. Important thing is how often does the client query ?
If the client queries too often, chances of detecting the even change sooner is higher. But the difference is the amount of requests that occur. Too many requests from several clients - this solution would struggle to scale.
There are two other techniques too which I want to try out. I shall look at them in coming posts.


  1. Another valuable information for this post.I like this kind of important information.It's very useful articles at the my studies and i can easily understand all given information.HTML was easy to observed.
    java training

  2. Nice..Its informative...Its very useful for me to understand..Keep on sharing..
    SAP Training in Chennai

  3. Programming is combination of intelligent and creative work. Programmers can do anything with code. The entire Programming tutorials that you mention here on this blog are awesome. Beginners Heap also provides latest tutorials of Programming from beginning to advance level. Be with us to learn programming in new and creative way.

  4. Thank you for sharing such a nice and interesting blog with us. Hope it might be much useful for us. keep on updating...!!
    seo company in india
    digital marketing company in india
    seo company in chennai
    digital marketing company in chennai

  5. I just keep visiting this blog from past two years. The posts here are unique and useful than other blog posts. Such a great info provided in this post. Keep providing useful post in this blog .I appreciate author for this valuable post. Culinary Schools in Dubai | International Centre for Culinary Arts

  6. Thanku so much for this blog . I get Knowledge from this blog .Thank u
    core java training in chennai
    | java training with job guarantee

  7. BlueHost is ultimately one of the best hosting company with plans for any hosting requirements.