How to Detect Ajax Request using Java Servlet?

Nowadays Developers across the world uses JavaScript frameworks like jQuery UI, ExtJs, Dojo to develop Rich Internet Applications (RIA). This JavaScript framework uses Asynchronous calls (Ajax Request) to get data from server in Json format.

On server side we have plenty of frameworks like spring, Struts which can handle Asynchronous and synchronous request.

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

Following HTML document uses method to send a Asynchronous Ajax request to Servlet.

A Controller which need to deal with both type of request can use request.getHeader() method to detect the Request type. Developer can use x-requested-with header parameter to get the request type. In case of Ajax Request request.getHeader('x-requested-with') will return XMLHttpRequest as string else return null.

Output will look similar to as follows.

About the Author

has written 69 posts.

I Love Coding, Analyzing, Designing and doing it the right way. I am obsessed about learn anything new daily and sharing what I have learn each Day. This Blog is journey of my Zeal, going through step by step to the mountain of desire to become prefect. Google Wanna Hire me ?

Copyright © 2020 All rights reserved.
Proudly powered by WordPress. Developed by 7Tech Solutions