Cross Domain Json (jsonp) Request

From the ever knowledgeable Wikipedia:

JSONP or “JSON with padding” is a communication technique used in JavaScript. It provides a method to request data from a server in a different domain, something prohibited by typical web browsers because of the same origin policy.

Recently, I had the requirement, whereby I needed to send several calls, via javascript, to a server. The catch was, that I needed the server to return the results of my calls, in the order in which the calls were made. Making the calls synchronous achieved this. Heres how:

(With inspiration from  this AJAX guy)

function getJson(arrayOfIds){

       jsonRequest = createSyncCORSRequest(“get”, host + “/info/get/”+arrayOfIds.join);

     if (jsonRequest) {
          jsonRequest.onloadend = function() {
               if(jsonRequest .status==200){
                    var jsonResponse = $j.parseJSON(jsonRequest.responseText);
                         functionWhichProcessesJsonResponse(jsonResponse );

              }
          };
          jsonRequest.send();
     }
}
function createSyncCORSRequest(method, url) {
     var xhr = null;
     if (typeof XDomainRequest != “undefined”) {
          xhr = new XDomainRequest();
          xhr.open(method, url,false);//false parameter makes it synchronous
          xhr.setRequestHeader(‘accept’, “application/json”);
     } else {
          xhr = new XMLHttpRequest();
          xhr.open(method, url,false);//false parameter makes it synchronous
          xhr.setRequestHeader(‘accept’, “application/json”);
     }
     return xhr;
}

cross-domain-json

Nelson Vivas, impersonating json leaping from one domain to the next.

Java method to return NULL as an empty String.

What if the String you are expecting comes back as null? Would it be the end of the world for you? Not if you have good exception handling. This method is also useful.

It will return an empty String “” of the String is null. If the String is not null, it will return the trimmed version of the Java String.

 private String getNullAsEmptyString(Object o){

        if( o instanceof String){
            if( o == null){
                return “”;
            }
            return  o.toString().trim();
        }

        return null;
    }

return-null-as-empty-string

Nwankwo Kanu, never known to deal with null Strings.

Setting Java Opts In Apache Tomcat

Setting Java Opts in the bin/catalina.(bat/sh) file can be used by your webserver to find files such as your applications property files.

 

Below is an example of increasing the memory size to hopefully avoid java out of memory errors. Also below, the location of property files is specified.

For windows the catalina.bat file should have the following line straight after the top comment section
set JAVA_OPTS=%JAVA_OPTS% -Xms512m -Xmx512m -XX:MaxPermSize=256m -Dcom.yourdomain.yourappname_1.properties=C:\workspaces\ws_4\application-one-name\properties\dev\prop.properties -Dcom.yourdomain.yourappname_2.properties=C:\workspaces\ws_4\application-two-name\properties\dev\prop.properties
For linux the catalina.bat file should have the following line straight after the top comment section
JAVA_OPTS=”$JAVA_OPTS -Xms512m -Xmx512m -XX:MaxPermSize=256mDcom.yourdomain.yourappname_1.properties=C:\workspaces\ws_4\application-one-name\properties\dev\prop.properties -Dcom.yourdomain.yourappname_2.properties=C:\workspaces\ws_4\application-two-name\properties\dev\prop.propertiess”

Note the quotes in the linux version as well as the absence of the word set.

apache-tomcat-environment

Lee Dixon, smug in the knowledge that his applications could find their properties files.