How to Batch Request Multiple DWR Servlets? - Comprehensive Guide

DWR (Direct Web Remoting) is an Ajax library which lets you easily call Java code from JavaScript, and to call JavaScript from Java. The library is designed to enable batch requests to be made from the client. With batch requests, multiple requests are queued up and sent in the same http request, and the responses are returned in the same http response.

Steps for Batch Requesting Multiple DWR Servlets

  1. Define the services you want to call inside your "batch.js" file.
var batch = {
    myFirstService: {
        method: function (completion) {
            ... code goes here ...
        }
    },

    mySecondService: {
        method: function (completion) {
            ... code goes here ...
        }
    }
};
  1. Initialize your remote objects and set the pollInterval.
// The pollInterval is used to coalesce requests.
// This will wait N milliseconds to see if any other requests are
// made before calling the server
dwr.engine.beginBatch({ pollInterval: 5000 });

// Set your remote object root
dwr.engine.setErrorHandler(errorHandler);

// Initialize any remote objects
dwr.engine.setOrdered(true);
dwr.engine.useLoadingMessage('Loading...');
dwr.engine.setRetries: 3;
dwr.engine.setTimeouts(3000);
dwr.engine.setTextHtmlHandler(defaultTextHtmlHandler);

// Add services to the batch
batch.myFirstService.method(completion);
batch.mySecondService.method(completion);
  1. Complete the batch request.
// Complete the batch
dwr.engine.endBatch();
  1. Process the response.
// Callback for the batch request
function completion(response) {
    // Process the response
    ... code goes here ...
}

FAQ

What is the purpose of batch requests?

Batch requests are used to make multiple requests with the same HTTP request. This helps to reduce the number of round trips between the client and the server, which leads to faster processing of requests.

What is DWR?

DWR (Direct Web Remoting) is an Ajax library which lets you easily call Java code from JavaScript, and to call JavaScript from Java. It is used to reduce the amount of time needed to send and receive data to and from the server, making your web application responsive and faster.

What is the pollInterval parameter?

The pollInterval parameter is used to coalesce requests. This will wait N milliseconds to see if any other requests are made before calling the server. This helps to reduce the amount of round trips between the client and the server.

What is the setRetries parameter?

The setRetries parameter is used to set the number of retries to attempt when making a call to the server. If the request times out, it will attempt the request X times before giving up. This helps to ensure that your requests are not lost when the network has temporary hiccups.

What is the setTextHtmlHandler parameter?

The setTextHtmlHandler parameter is used to set the default handler for handling text/html responses. This helps to ensure that your responses are properly pumped back to the UI, with no unexpected errors.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Lxadm.com.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.