HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML

Just when you thought it was safe to start learning Java, along comes yet another new technology from the those clever boffins at SunSoft, to confuse and dazzle the developer. Fortunately, it's not as complicated as it sounds. Jeeves is the name of the new Java Server API that defines a mechanism and framework for creating connection-oriented server applications. This article will take a look at what Jeeves is aiming to accomplish and why it is a much better solution to the technologies being offered today. We will conclude with a look at the time honored tradition of the Hello World' program, complete with all the source code and supporting files needed to create this masterpiece.

A servlet is simply the program that runs in response to a connection or request from the client. The popularity of Java can be attributed to the Internet, and, therefore, aiming Jeeves initially at the Web would be an excellent way of promoting the power of servlets. The alpha release of Jeeves has a complete Web server implemented using servlets, which is an excellent example of the power of Jeeves.

A main server loads up and binds itself to a port, in this instance the HTTP port of 80. When a connection is accepted, the server checks to see if a servlet is available to service the request. If there is one, the connection is passed to the servlet for processing. This way, the server can maintain a pool of servlets and ensure that there are always enough resources to service any further connections.

The real beauty of this mechanism is that you can build servlets that handle very specific requests. For example, you can have one servlet to service file requests, one for CGI requests, one for Image maps, etc. You can find a servlet for all of these in the current alpha release of Jeeves. The main advantage of this technique is that it results in very small, dedicated programs that do not need to drag extra functionality around with it, just in case it's used.

Power to the Server
Nearly all of today's Web processing is performed by CGI scripts written primarily in C. From front-ending search engines to providing a simple hit count for the number of visits to a site, CGI pops up everywhere. However, just because something is as popular as this doesn't mean it doesn't have its limitations. Ask any Web administrator about the extra load processing a CGI program puts on the server. For every request, a new program instance is loaded and run and then shutdown. This takes time and resources.

Servlets, on the other hand, do not have this overhead. They are loaded once and run many times, making them very efficient and very fast. Since the loading and initialization stage has been removed, the user can experience a much speedier reply.

Servlets from the developers/administrators point of view also have a major benefit. Although CGI is cross-platform to an extent, it still has some porting issues. Since servlets are implemented using Java, the developers no longer need to concern themselves with the target platform. Another benefit that comes part and parcel with this is the security of servlets, as this is an intrinsic part of Java.

`Hello World Servlet'
When learning a new language or technology, we generally want to get something working as quickly as possible, and build on successes. Developers are a funny lot on the whole; we prefer to modify and adapt existing code instead of completely rewriting. How many times have you cut and pasted the framework of an applet instead of re-doing it over and over again? So with that, we present a complete servlet implementation that will perform the very important task of printing Hello World' when accessed. But just to make this version really useful(!), we will keep a running count of the number of times the servlet has been accessed (see Listing 1).

Just as an applet is based upon the java.applet.Applet class, a servlet is based upon the java.servlet.Servlet class. As stated before, a servlet is first loaded into memory when the first reference to it occurs, and as a result of this the init() function is called. In this instance, we initialize our variable NoTimes to 0.

The next method we override is the service(...) function. This method is called for in every client connection that is made to a servlet. This is why servlets are so efficient, instead of a complete program reloading and initializing itself. It's just a method that is re-run.

This method has two parameters that are passed into it, one for any parameters passed to the servlet and the other one for formatting a response to the client. In this example we are not picking up any parameters, just preparing an output. Notice how we set up the header information for the output stream; this is simply preparing the output response to the text/html' protocol and setting the status flag to a good state.

Just like many of the output mechanisms in Java, servlets utilize the stream's functionality. We retrieve a reference to the output stream by calling the _Res.getOutputStream() method and using that to create a PrintStream object. This then gives us the flexibility to use the println(...) method to create our on-the-fly HTML file.

To run this servlet we need some way of calling it from an HTML file. This is done by using the following HTML tags:

<servlet CODE="mypackage.HelloWorldServlet"
P_1="VAL_1" ... P_N="VAL_N">

And that's it.

We demonstrated how easy it was to construct a servlet, using the Hello World' example. As shown, there aren't a great many lines associated with it and the complexity of handling the actual client connection is taken care of for you within Jeeves.

As we have seen, Jeeves has taken the next ecological steps in the World Wide Web. The applet took up the gauntlet at the client side of the Web, and now the servlet has picked up the gauntlet at the server side. And it shows no sign of dropping it.

About the Author
Alan Williamson currently runs his own Software Consultancy company (N-ARY Limited) based in the United Kingdom. Having previously gained his honours degree at the University of Paisley, Scotland, he went on to work for major corporate organizations including BT and BP Exploration. N-ARY's is involved in the Internet and Web publishing, Alan, along with one of his colleagues, is currently co-authoring a book on Java and databases which is scheduled for release soon by a major publisher.


Listing 1:

import java.io.*;
import java.util.*;
import java.servlet.*;

public class HelloWorldServlet extends Servlet
 int NoTimes;

 public void init()
NoTimes = 0;

public void service ( ServletRequest _Req, ServletResponse _Res )

  	_Res.setStatus( SC_OK );
	_Res.setContentType( "text/html" );

PrintStream	Output = new PrintStream( _Res.getOutputStream() );

Output.println( "<HTML><HEAD><TITLE>Hello World</TITLE><HEAD>" );
Output.println( "<BODY>" );
Output.println( "<H1>HELLO WORLD!!!!</H1>" );
Output.println( "<BR>You are number " + NoTimes + " to visit" );
Output.println( "</BODY></HTML>" );


All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.