ajax vs sync - How to Detect Ajax Request using Java Servlet?

How to Detect Ajax Request using Java Servlet?

Posted on

ajax vs sync - How to Detect Ajax Request using Java Servlet?

These days Builders the world over makes use of JavaScript frameworks like jQuery UI, ExtJs, Dojo to develop Wealthy Web Functions (RIA). This JavaScript framework makes use of Asynchronous calls (Ajax Request) to get knowledge from server in Json format.

On server aspect we’ve loads of frameworks like spring, Struts which might deal with Asynchronous and synchronous request.

In response to a Asynchronous request, Controller must ship response in Json format. For synchronous request, controller can delegate response to a View.

Following HTML doc makes use of jQuery.publish() methodology to ship a Asynchronous Ajax request to Servlet.

A Controller which must take care of each sort of request can use request.getHeader() methodology to detect the Request sort. Developer can use x-requested-with header parameter to get the request sort. In case of Ajax Request request.getHeader('x-requested-with') will return XMLHttpRequest as string else return null.

Output will look much like as follows.

detecting async request using java - How to Detect Ajax Request using Java Servlet?

Supply techzoo.org