Not so Simple Pages

This is an area that I find needs exploring. Here you will find my first steps on the path to Java.

I suppose it all starts with the famous line "Hello World."
In pure Java that would look something like this:

class HelloWorld {
public static void main(String args[]){
System.out.print("Hello World.");

}
}

The file that contains this piece of code should be named : 'HelloWorld.java'
and after compiling (using javac) you would find 'HelloWorld.class' in the same directory.

Running it would go something like 'java HelloWorld' and the result is a new line printed with  the famous words 'Hello World.'
This all, of course, on the command line of your terminal or DOS window.
That's all a bit useless if you want to produce those famous words on a web page.
The way the above piece of code is written (with a 'main' body) makes it excecutable. What we want is make it runnable through a browser.

Okay, lets change tack.
What do we need to put in a 'html' file to make this one work.
I've created a file called, you guessed it, helloworld.html and its content looks like this:

<HTML>
<HEAD>
<TITLE> Simple page </TITLE>
</HEAD>
<BODY>
The famous words we are looking for:

<APPLET CODE="HelloWorld.class" WIDTH=150 HEIGHT=25>
</APPLET>

</BODY>
</HTML>

We need to make some changes to the original pure java because we are now implementing it as an Applet.

import java.applet.Applet; 
import java.awt.*;
public class HelloWorld extends Applet {
public void init() {
setBackground(Color.black);
}
public void paint(Graphics g) {
g.drawString("Hello world!", 50, 25);
}
}

Try this link and see the end result:

helloworld.html


You might find that this is, in most browsers, no longer working!!


One of the problems with Java is that the client computer need to have the JRE installed.
Also, the Java application runs on the client computer. This can be an obstacle.
My latest move would be to introduce AJAX. Have the applications running on the server and only show the result on the client computer.
Here is an example of my first steps toward using the AJAX mechanism.

<html>
<title>AJAX Server-Side Testing Script </title>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
</head>

<body onload="ajaxFunctions()">
<script src="ajax.js">
</script>
 
Server Time:
<div id="time2"></div>
<br>
The image should change every so often.
<br>
<img id="RandomImg" alt="">
</body>
</html>
      

Most of the tricks are hidden in ajax.js which looks like this:

function ajaxFunctions(){
    timeFunction();
    randomImage();
}


function timeFunction(){  
  var xmlHttp = testBrowser();
  var url="/cgi-bin/ajax/...";
  url = url+"? &sid="+Math.random();

  xmlHttp.onreadystatechange=function(){
    if(xmlHttp.readyState==4){
      document.getElementById("time2").innerHTML=xmlHttp.responseText;
    }
  }
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);

  t=setTimeout('timeFunction()', 1000);
}


function randomImage(){
  var now = new Date();
  var myimage = document.getElementById("RandomImg");

  myimage.setAttribute("src", "/cgi-bin/ajax/random_image1.pl?nocache="+now.getTime());
  setTimeout('getpic()', 7000);
}

function testBrowser()
{
  var xmlHttp;

try{ // Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e){ // Internet Explorer
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){
alert("Your browser does not support AJAX!");
return false;
}
}
}
return xmlHttp;
}

To see this all in action click on the following link AJAX

Some links that might become of some interest.

  • The first steps to live data from my Weather Station
  • What's wrong with this clock :-))
  • Some javascript also producing a clock

  • exit.gif (1095 bytes)


    Last modified:Friday, 15-Sep-2017 20:05:17 NZST