Reverse AJAX using AsyncServlet support in Servlet 3.0

Introduction

If you already know why and when to use reverse ajax, you can skip this section. But for those novice developers don’t know these answers I prefer go through this section.

Nowadays reverse ajax is used every now and then. Its very much needed in any type of website. Whether is any social network website or some serious business, it is needed to decrease the network traffic and also for better user interaction. Its because of the reverse ajax you gets the instant notification update in facebook(isn’t that fascinating?). But as a web developer I like the way reverse ajax is used in StackOverflow. They always keeps you updated with any new up votes to your answer or any new comments you have got for your answer or question. You can also take example of any chat service provided. Think, how you get to know that the person with whom you are chatting is right now typing something or how you gets the line typed by that person in your chat box. You are not pressing any button to get this responses. You get to know this whenever the other person is performing something and server knows that event is occurred and then it broadcasts that event to you without your request. Simple ajax request-response performed on client’s request to perform some action on server side so that the page doesn’t gets reloaded. Reverse ajax is any response you get from server without client’s request. That response is sent by the server whenever a specific event occurs on the server side. And it keeps on sending such response on every such event occurs on the server side. I think this is enough for you to know why and when reverse ajax is needed in a web application.

Reverse AJAX technologies

There are many ways you can implement reverse ajax. For example apache comet, atmosphere etc. You can use them also. Both of them provide good support with Spring Framework as well. But they require good amount of configuration and including those libraries in your application is some what overhead to your application size. And if you can do the same thing by just playing with the much familiar servlet API itself then why to include other libraries. AsyncServlet is introduced in the Servlet 3.0 version. And I found it very easy to implement and useful. If satisfies our need of reverse ajax very efficiently. We will also use quartz scheduler to perform job of sending the pending responses to the client. We will also use Queue and Map data structure. Queue will hold the pending responses to be sent. And the Map will hold a key(type of your choice as per need) and an ArrayList consisting watchers(waiting for some response from server). You will come to know about usage of these in coming sections one by one. Now just sit back, relax and go through next few sections of real time example.

Chat Box Example

In this example we will try to create a group chat box that can have any number of users chatting in a group created.

Web.xml

Advertisements

About japantrivedi

I'm a geek. Computer engineer and addicted to GWT coding.
This entry was posted in Servlet, Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s