facebook
Are you an Employer?
List of Topics

LiveConnect Requests


LiveConnect Requests

Netscape Navigator introduced a concept called LiveConnect,a capability that enables JavaScript to interact with and use Java classes. To work, the user must have a Java Runtime Environment (JRE) installed, and Java must be enabled in the browser.Almost all modern browsers (with Internet Explorer being the major exception) support LiveConnect, which provides access to all the HTTP-related libraries that Java offers.

Performing a GET request

If you know how to perform a GET request using Java,it’s very easy to convert the request into a LiveConnect script.The first step is to create a new instance of java.net.URL:

function httpGet(sURL) {
var oURL = new java.net.URL(sURL);
//...
}

Note that when you use LiveConnect,you must furnish the complete name of the class,including the package,to instantiate a Java object.After the URL is created,you open up an input stream and create a reader to get the data back.The preferred way to do this is to create an InputStreamReader and then a BufferedReader based on it:

function httpGet(sURL) {
varoURL = new java.net.URL(sURL);
var oStream = oURL.openStream();
var oReader = new java.io.BufferedReader(new
java.io.InputStreamReader(oStream));
//...
}

With the buffered reader created, all that’s left to do is read the data back from the server. A buffered reader gets data line-by-line, so you create a variable to build up the response into the full text. This response text variable (named sResponseText) must start out as an empty string, not null, so that string concatenation can be used to build the result:

function httpGet(sURL) {
var oURL = new java.net.URL(sURL);
var oStream = oURL.openStream();
var oReader = new java.io.BufferedReader(new
java.io.InputStreamReader(oStream));
var sResponseText = “”;
var sLine = oReader.readLine();
while (sLine != null) {
sResponseText += sLine + “
”;
sLine = oReader.readLine();
}
//...
}

Because the buffered reader returns lines, each line must be appended with a new line character to ensure that it remains in the same form, as it should. The last steps are to close the reader and return the response text:

function httpGet(sURL) {
var oURL = new java.net.URL(sURL);
var oStream = oURL.openStream();
var oReader = new java.io.BufferedReader(new
java.io.InputStreamReader(oStream));
var ResponseText = “”;
var sLine = oReader.readLine();
while (sLine != null) {
sResponseText += sLine + “
”;
sLine = oReader.readLine();
}
oReader.close();
return  sResponseText;
}

Now this function can be used to send a GET request using the same addURLParam() function defined earlier:

var sURL = “http://www.somwhere.com/page.php”;
sURL = addURLParam(sURL, “name”, “Nicholas”);
sURL = addURLParam(sURL,“book”, “Professional JavaScript”);
var sData = httpGet(sURL);

Unfortunately,you don’t get all the same information,such as status, when using LiveConnect.Also, this function creates a synchronous call without the option of creating an asynchronous one.But the advantage is that this works in Netscape Navigator 4.x and most versions of Opera, as well as any other browser that supports LiveConnect.

Performing a POST request

As discussed earlier, POST requests are slightly different from GET requests in their format and behavior.However, it’s possible to send a POST request just as easily as a GET request using LiveConnect. To start,you provide a URL and a parameters string .Then,you create another java.net.URL instance.Unlike last time, this code uses a Connection object to facilitate the request:

function httpPost(sURL, sParams)  {
var oURL = new java.net.URL(sURL);
var oConnection = oURL.openConnection();
//...
}

Next,you must determine the settings on the connection. Because a POST request is considered bi-directional, the connection must be set up to accept input and output by using the setDoInput() and setDoOutput() methods.Additionally, the connection shouldn’t use any cached data, so setUseCaches() is given an argument of false.Just as with the XML HTTP request object, you must set the “Content-Type” header to the ppropriate value using the setRequestProperty() method:

function httpPost(sURL, sParams)  {
var oURL = new java.net.URL(sURL);
var oConnection = oURL.openConnection();
oConnection.setDoInput(true);
oConnection.setDoOutput(true);
oConnection.setUseCaches(false);
oConnection.setRequestProperty(“Content-Type”,“application/x-www-form-urlencoded”);
//...
}

After the connection has been set up, it’s possible to get an output stream for the request.It’s on the output stream that you place the parameter string using the writeBytes() method.After that,a call to flush() sends the data along,and the stream can be closed:

function httpPost(sURL, sParams)  {
var oURL = new java.net.URL(sURL);
var oConnection = oURL.openConnection();
oConnection.setDoInput(true);
oConnection.setDoOutput(true);
oConnection.setUseCaches(false);
oConnection.setRequestProperty(“Content-Type”,“application/x-www-form-urlencoded”);
var oOutput = new  java.io.DataOutputStream(oConnection.getOutputStream());
oOutput.writeBytes(sParams);
oOutput.flush();
oOutput.close();
//...
}

The next part of the function gets the input stream for the connection and reads the data in, line-by-line, similar to the httpGet() function. Then, the input stream is closed, and the response text returned as the function value:

function httpPost(sURL, sParams) {
var oURL = new  java.net.URL(sURL);
var oConnection =  oURL.openConnection();
oConnection.setDoInput(true);
oConnection.setDoOutput(true);
oConnection.setUseCaches(false);
oConnection.setRequestProperty(“Content-Type”,“application/x-www-form-urlencoded”);
var oOutput = new
  java.io.DataOutputStream(oConnection.getOutputStream());
oOutput.writeBytes(sParams);
oOutput.flush();
oOutput.close();
var sLine = “”, sResponseText =  “”;
var oInput = new
  java.io.DataInputStream(oConnection.getInputStream());
sLine = oInput.readLine();
while (sLine != null){
sResponseText += sLine + “
”;
sLine = oInput.readLine();
}
oInput.close();
return sResponseText;
}

Using this function,you can submit a POST request like the following:

var sParams = “”;
sParams = addPostParam(sParams, “name”, “Nicholas”);
sParams = addPostParam(sParams, “book”, “Professional JavaScript”);
var sData =
  httpPost(“http://www.somewere.com/reflectpost.php”,sParams);