LiveConnect Requests - Java Script

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

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:

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:

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:

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

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 instance.Unlike last time, this code uses a Connection object to facilitate the request:

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:

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:

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:

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

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd Protection Status

Java Script Topics