Directory listing on the Web server in a Java AppletTag(s): Javascript interaction


There is no way for an Applet to retrieve a directory contents without the help of server side process(servlet/CGI). But here a way to do it with some help from Javascript.

In a browser, loading a URL with no file specified will return a directory listing under the following conditions :

  1. The directory doesn't contain a default page like index.html, default.htm or default.html.
  2. The web server allows directory exploration.
For example, the URL http://www.rgagnon.com/images shows a directory listing of the files in the images directory.

The listing is actually a real HTML page build on the fly. Our applet will extract the links in this page and present them in a List. Links in a page are listed in the document property called links. This property can be easily transform into a String array by a Javascript function. Then the array is passed to a Java method. The Applet is very simple and can be customized to display more useful descriptions or filter some entries.

We have 3 frames, 2 visibles and 1 invisible. The invisible one will contains the directory listing. Frame visible #1 is for the Applet, by doubleclicking on a line in the List, the corresponding images will be displayed in frame visble #2.

Frames definitions
Note that the 2 visibles frames are initially loaded with a "blank.html" to allow the third frame (the invisible one) to be loaded with the directory content (here "../images").

browse.html

<HTML><HEAD>
<SCRIPT>
function reload_master() {
    window.master.location.href = "./selector.html";
}
</SCRIPT></HEAD>
<FRAMESET ROWS="35%,65%,*" onLoad="reload_master()">
<FRAME SRC="blank.html" NAME="master">
<FRAME SRC="blank.html" NAME="detail">
<FRAME SRC="../images" NAME="contents" NORESIZE>
</FRAMESET>
</HTML>

blank.html

<HTML<<HEAD><TITLE&glt;
</TITLE></HEAD><BODY>
</BODY></HTML>

When all pages are loaded, the selector.html page is loaded into the first visible frame. That page contains the Applet. During layout time, Javascript extracts links to the an Array. Via the BODY onLoad event handler, we trigger a Javascript function to transfer the Array to the Applet.

selector.html

<HTML><HEAD>
<SCRIPT>
var LinksLength =
    parent.contents.window.document.links.length
var AllTheLinksAsArray = new Array()
// start at the second link because
// we dont want the root directory
for (var i = 1; i < LinksLength ; i++) {
    s = parent.contents.window.document.links[i];
    AllTheLinksAsArray[i] = s;
    }

function putLinksIntoApplet() {
    AllTheLinksAsString = AllTheLinksAsArray.join("|");
    document.Selector.insertData(AllTheLinksAsString);
    }
</SCRIPT></HEAD>
<BODY onLoad ="putLinksIntoApplet()">
<P ALIGN="center">Doubleclick to view
<APPLET
   CODE=Selector.class
   HEIGHT=100
   WIDTH=400
   NAME=Selector>
<PARAM NAME="targetFrame" VALUE="detail">
</APPLET>
</BODY>
</HTML>

Selector.java

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
import java.net.*;

 public class Selector extends Applet
    implements ActionListener {
   java.awt.List l;
   boolean okToDisplay = false;
   String targetFrame = "";

   public void init() {
     setLayout(new BorderLayout());
     add(l = new java.awt.List(5), "Center");
     l.addActionListener(this);
     targetFrame = getParameter("targetFrame");
     }

   public void actionPerformed(ActionEvent ae) {
     if (okToDisplay) {
        try {
          URL urlToDisplay = new URL(ae.getActionCommand());
          getAppletContext().showDocument(urlToDisplay, targetFrame);
          }
        catch (Exception e) { e.printStackTrace(); }
        }
     }

   public void insertData(String arrayAsAString) {
   int i = 0;
   String s;
   StringTokenizer st =
      new StringTokenizer(arrayAsAString, "|");

   while(st.hasMoreTokens()){
     s = st.nextToken();
     l.add(s); // or l.addItem(s);
      System.out.println(s);
     }
   okToDisplay  = true;
   }
 }
You can try it here.

NOTE: This How-to is inspired by an article by Martin Webb on http://www.irt.org


blog comments powered by Disqus