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 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(" | | |