Back to the Web Developer's Journal Main Page
internet.com
side nav bar

Utilizing Netscape's 'servlet' API is an easy way to use server-side Java for processing forms and driving mission critical applications. Both experienced and inexperienced Java developers will appreciate the helper-methods as well as access to the lower level methods in Java. Java code should make your Web-based applications easier to maintain and manage than C, Perl, or Tcl scripts. It also provides an easier interface to your back-end legacy systems.
HOW DID THEY DO THAT???

Find out in:
Amazing HTML



Site Map

Jobs at webdeveloper.com


Check out our Web-based
Discussion Groups:

Check out and join our email-based Mailing Lists for Web developers.


Discussion Groups Book Reviews Software Reviews Download Web Tools

How to implement server-side Java using Netscape's implementation of Java within the Enterprise Server.

Netscape-flavored Servlets

by Russell Castagnaro

It happens everyday. Someone, in a corporation somewhere, is attempting to convince skeptics of the benefits of using Java. The conversation goes well. Finally the group crowds around a monitor to see a 'really interesting Java applet.' The URL is loaded. What happens? An ugly gray box appears where the applet should be. Just another victim of an 'overactive firewall' or a 'non-Java-compatible browser.' The company decides to wait a few months until the technology has 'matured.'
September 28, 1997

Beating the System



It's true, until more corporations reconfigure firewalls and/or upgrade browsers, Internet users residing behind a firewall will not be able to use those applets that you or your team have spent months slaving over. Fear not. There is a middle ground between the bleeding-edge (applets) and the 'dull' blade (CGI). By implementing server-side Java which serves dynamically created HTML pages, you can improve performance, offer more functionality, and update your systems while providing functionality to the most basic of clients, a Web browser.

Netscape's Architecture

Netscape has created Java packages that are included with its servers. The HTML documentation is installed with the server, although finding it can be quite a chore. Netscape has made it easy to handle almost anything you may need to do within your servlet without much knowledge of the rest of the API.

The netscape.server.applet.ServerApplet class provides most of the functionality for handling requests. The netscape.server.applet.HttpApplet provides many helper and HTTP-specific methods.

When a request is made of a Netscape servlet by either directly linking to the URL or by making the URL the action of a form, the run method of the class is called. Your applet must subclass either ServerApplet or HttpApplet and implement the run method in order to be invoked by the Netscape server.

Your First 'Netscape Servlet'

This servlet returns the contents of the '/Inetpub/wwwroot/message.txt' file.

The run() method is called by the Netscape server when a request comes in for this URL. The HttpApplet class, in handling the http-request for you, insulates you from it, providing methods that can access its contents. Similarly, you are not required to know anything about http-responses. To create the servlet's response merely call the getOutputStream() method and print the contents of your response directly to that PrintStream. Provided a getFile() method which returns the contents of a file as a String.

You do have to import the netscape package.
import netscape.server.applet.HttpApplet;
import java.util.*;
import java.net.*;
import java.io.*;

public class TestApplet extends HttpApplet {


    public void run() throws Exception {

 	PrintStream out;

String message;
        // load the message into a file
        message = getFile("message.txt",true);
        
// serve the message
 	// Open output stream to client.
        try {
            out = getOutputStream();
        } catch (IOException e) {
            // send a message to the console
            // std out doesn't go to the serve log as best I can tell
            System.out.println("Cannot open output stream. \n");
            return;
        }

        // Initiate HTTP response to client.
        try {
            this.returnNormalResponse("text/html");
        } catch (IOException e) {
            return;
        }

        // If response initiated successfully, serve the string.
        out.print(message);
   
    }


    protected String getFile(String _fileName, boolean _fromRoot)
		throws Exception {
        
        String input;
        String fini = new String(""); // Closing tag for HTML
        StringBuffer outStr = new StringBuffer();
        File file;
        boolean fin = false;
        DataInputStream in;
       String fileName;
        if (_fromRoot) {
            fileName = DOC_ROOT + _fileName;
        } else {
            fileName = _fileName.toString();
        }

        // Open login HTML file.
        try {
            f = new File(fileName);
        }
        catch (NullPointerException e) {
            return ("Cannot create file pointer for file...\n");
        }


        // Define input stream of HTML file.
        try {
            in = new DataInputStream(new FileInputStream(file));
        }
        catch (FileNotFoundException e) {
            throw new Exception("File not found.");
        }

        // Read the contents of the file into the StringBuffer.
        try {
            // Loop until closing HTML tag read.
            while  (! fin) {
                input  = in.readLine();
                if (input.indexOf(fini) > 0 ) {
                    fin = true;
                }
                outStr.append("\n" + input);
            }
        } catch (Exception e) {
            fin = true;
        }

        // Close stream.
        try {
            in.close();
        }
        catch (IOException e) {
        }

        return outStr.toString();
    }
    

}

Processing Form Data

One of the most important functions that a servlet will perform is processing data that is entered by a user on a remote browser. Netscape has implemented a method, getFormData() that loads the form's name/ value pairs into a Hashtable. The data is accessible by using the name of the form field as the key. FormData is a class variable that any method can access. For instance to access the "foo" value from the form, you would call:

getFormValues();
String val (String) formData.get("foo");

It may seem somewhat unwieldy to cast the object returned by get() to a String all the time, however it is necessary, since a Hashtable stores Objects, not Strings. If you prefer, you can call the toString() method instead.
public void getFormValues(){
        
        try {
	        formData = getFormData();
    	} catch (IOException err) {
    	    serve("Form data could not be interpreted. <BR>"+err);
    	}
    }

Limitations

There are some inherent limitations in Netscape's implementation of server-side Java. Any API that links to a native library will cause the server to crash. For example, attempting to use the JDBC-ODBC bridge, OrbixWeb, or other non-pure Java API's, yields very nasty errors in Solaris or NT. Netscape was unable/ unwilling to recommend a work-around at the time of writing. This can be extremely frustrating.

The 2.0 and 2.01 release of Netscape's Enterprise Web server uses Netscape's implementation of Java. Many of the core Java classes were re-created by Netscape developers. There are some bugs present that are not in Sun's JDK. With the release of the 3.0 version, these should be fixed.

Work-arounds

While Netscape was not particularly helpful with linking to external libraries, there are ways to program around the problem. For database connectivity, Weblogic's T3, a pure Java JDBC client. T3 and other comparable products (XDB's JetConnect and Symantec's DB anywhere) provide a daemon that runs on your server listening for requests. The native calls are made by the daemon, thus saving you from an embarrassing server crash. For other business objects that are not pure Java, creating your own socket server daemon is always an option.

Closing

Utilizing Netscape's 'servlet' API is an easy way to use server-side Java for processing forms and driving mission critical applications. Both experienced and inexperienced Java developers will appreciate the helper-methods as well as access to the lower level methods in Java. Java code should make your Web-based applications easier to maintain and manage than C, Perl, or Tcl scripts. It also provides an easier interface to your back-end legacy systems.


Russell Castagnaro is the Senior Technical Architect for DataHouse Inc. in Honolulu, HI. Previously, he was the Senior Technical Architect in the Internet Practice for Claremont Technology Group. He has been the chief architect for four large, successful Java projects in the last two years, with many more to come.


Back to the Web Developer's Journal
Contact WDJ   •    Suits!   •    Propheads!   •    Ponytails!
Discuss   •    Subscribe   •    Search

Be a Commerce Partner