Article:
  Remote Scripting with IFRAME
Subject:   A cleaner solution
Date:   2003-05-18 11:56:49
From:   anonymous2
Response to: A cleaner solution

Yes it's me again. I solved the problem with the blinking cursor and status bar, and can now exchange information with the web server without reloading the HTML page, and without any flicker.


This example uses a small javaapplet instead of the IFRAME.


The example contains 4 files, that enables a html page to exchange information with the web server without reloading the viewed HTML page (client.php).


The solution uses javascript, java and PHP. You will propably need to add some database too.
I've used PHP in these examples, but it should be easy to replace the PHP with ASP, JSP, or the like.


Check out this page for the other method, that does the same job:
http://www-106.ibm.com/developerworks/web/library/wa-exrel/?dwzone=web
It does not use java, but an IFRAME. It causes the status bar to flicker, and the cursor to turn into an hour-glass, every time receiver.php gets reloaded.


My solutions solves this annoyance, by loading the file from a java applet, by opening a socket back to the webserver.


Here is a short description of the files:
--client.php--
This file contains the webpage that needs to get information from the webserver without getting reloaded
This is propably the page the user sees when using your service.


--receiver.php--
This is the webpage that we load, to avoid loading client.php.
The user never sees this page.


--ClientApplet.java--
This is java program we use to load reciever.php behind the scenes.
The user never sees this applet.


--Client.java--
This file contains the methods that gets/reloads/refreshes reciever.php from the webserver.
The user never sees this applet.


Best Regards
Stefan Krabbe
dsl74896@vip.cybercity.dk


PS: The 4 files follows this line.
=- client.php -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
<html><head>
<SCRIPT LANGUAGE="JAVASCRIPT">
var heartbeat_counter = 0;
//////////////////////////////////////////////////////////////////
// Javascript:
// The browser can execute the function periodically or upon request
// by the user. The function uses the ClientApplet.getjavascript() to
// get the script contained in a new instance of the page reciever.php.
//////////////////////////////////////////////////////////////////
function query_receiver_php()
{
sv_chat = "";
// Query the java applet for the new javascript, that sets the variable sv_chat.
s = document.ClientApplet.getjavascript("&somevariable=somevalue&anothervariable=anothervalue");
// Evaluate the javascript returned by getchat(), fx 'var sv_chat = "Hello there";'
eval(s);
// Do something with sv_chat, fx:
if(sv_chat)
someForm.someTextArea.value += sv_chat;
}
</script>
</head>
<body>
<h2>This web page contains an applet that can query/refresh reciever.php</h2>
<!-- Insert a GUI here, that calls query_receiver_php() -->
<applet name="ClientApplet" width="0" height="0" code="ClientApplet.class">
<param name="host" value="www.somesite.somedomain" />
<param name="port" value="80" />

Your browser is not able to run Java applets. This is probably due to having Java disabled in your browser settings.


</applet>
</body></html>


=- receiver.php -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
<HTML><HEAD>
<?php
function get_chat() {
// Look up some info ... open a database or a text file.
// For now just return some static text.
return 'Hello there';
}
$sv_chat = get_chat();
?>
<SCRIPT LANGUAGE="JAVASCRIPT">
var sv_chat = "<?php print($sv_chat) ?>";
</SCRIPT>
</HEAD><BODY></BODY></HTML>


=- ClientApplet.java -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
import java.awt.*;
import java.lang.*;
import java.applet.*;
import java.io.*;
import java.net.*;
public class ClientApplet extends Applet
{
private Client client;


public void init() {
String host = getParameter("host");
int port = Integer.parseInt( getParameter("port") );
client = new Client(host, port);
}
public String getjavascript(String querystring) { return client.myprocess(querystring); }
}
=- Client.java -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Use the function Client.myprocess() to get the contents of the
// webpage reciever.php, and extract some javascript from it.
import java.lang.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class Client
{
private long fakeid;
private Socket socket;
private DataOutputStream toserver;
private DataInputStream fromserver;
private byte received_data[];
// Applet parameters
private String host;
private int port;
public Client(String arg_host, int arg_port) {
host = arg_host;
port = arg_port;
received_data = new byte[10000];
fakeid = 0;
}
private static final String extract_javascript(String html) {
String s_begin, s_end;
int i_begin, i_end;
s_begin = "<SCRIPT LANGUAGE=\"JAVASCRIPT\">";
s_end = "</SCRIPT>";
i_begin = html.indexOf(s_begin);
if(i_begin < 0)
return "";
i_begin += s_begin.length();
i_end = html.indexOf(s_end, i_begin);
if(i_end < 0)
return "";
return html.substring(i_begin, i_end);
}
public final String myprocess(String querystring) {
String received = "";
try {
// Use the querystring to pass information to receiver.php
String msg = "GET /receiver.php?fakeid" + String.valueOf(++fakeid) + querystring + " HTTP/1.1\r\n";
msg += "Host: " + host + "\r\n";
msg += "Connection: close\r\n";
//msg += "Cookie: PHPSESSID=" + somesessionid + "\r\n"; // Set this if you need to pass session information
msg += "\r\n";
socket = new Socket( host, port );
fromserver = new DataInputStream( socket.getInputStream() );
toserver = new DataOutputStream( socket.getOutputStream() );
toserver.writeBytes(msg);
int n = socket.getSoTimeout();
boolean bad = (socket.isClosed() || !socket.isConnected() || socket.isInputShutdown() || socket.isOutputShutdown());
int numreads = 0;
for(received = ""; !bad; ) {
int bytes_received = 0;
bytes_received = fromserver.read(received_data, 0, 9990);
if(bytes_received <= 0)
break;
received += new String(received_data, 0, bytes_received);
bad = (socket.isClosed() || !socket.isConnected() || socket.isInputShutdown() || socket.isOutputShutdown());
++numreads;
}
received = extract_javascript(received);
fromserver.close();
toserver.close();
socket.close();
} catch( IOException ioerr ) {
System.out.println("Error: " + ioerr.toString());
return "";
}
return received;
}
}