JavaEJBA new home for Java -- servlets

A new home for Java — servlets

February 16, 1998
A new home for Java — servlets

by Simon St. Laurent

You’ve spent weeks and months learning Java, poring over its classes and learning the rules of the virtual machine. After much hard effort, you’ve produced an applet that looks polished and provides users with a reasonably friendly interface for some complex business processes. Demonstrations have been successful, and everyone seems pleased. Then the phone rings; it’s a user who’s taken Java’s cross-platform capabilities seriously and visited the site from a different platform. It works, mostly, but it looks really strange, almost unfamiliar…

As promising as applet development is, developers’ frequent lack of control over the final appearance and performance of their products is often frustrating. The limitations on applet capabilities and the changes between Java versions make it difficult to apply the full strength of Java’s incredibly powerful programming structures, restricting the real-world use of Java fairly severely. As applet development becomes a specialized field for multimedia toys and carefully-built applications, Sun has taken some dramatic steps to insure that Java remains ubiquitous — and useful — by creating a new set of APIs that allow developers to use Java as a server-side development tool.

Servlets — Java in the server environment

Servlets run on the server much like applets run on the client, sharing the same kinds of sandbox security rules, with the same kinds of error checking and garbage collection. Servlets have a lot more power, however, because they run in a much more controlled environment. Administrators can give servlets the permission they need to get real work done, without having to worry about frantic calls from users trying to decipher strange warnings in dialog boxes. Servlets can run like CGI programs, answering HTTP requests, or they may run independently of a Web server, taking requests from servlets and other programs directly over the network. Sun’s Java Servlet API provides developers with a simple set of tools for creating powerful server-side Java applications that run on a wide variety of platforms, Web servers and Java Virtual Machines.

The Java Servlet API first appeared with the Sun Java Web Server, a full-functioned Web server that runs entirely in Java. Version 1.1 of the Java Web Server extended that API with new tools for maintaining sessions (with or without cookies), working with HTML templates, and compiling HTML pages into servlets. The servlet API will make its official debut as part of Java Development Kit 1.2, though as an “extension,” not as a part that all virtual machines are required to provide as core functionality.

Servlet extensions are available for a wide variety of platforms, even without the Java Web Server. New Atlanta Communications offers ServletExec for a variety of Macintosh and Windows servers. O’Reilly Web Site Pro 2.0 provides servlet support, and IBM offers ServletExpress 1.0 for Netscape, Lotus, Microsoft and Apache servers on many platforms. Servlet support is gradually becoming a part of the standard Web server feature set.

Servlets and the Web — the new CGI

Servlets may well be the best tool to arrive on the server side of Web development since Perl and its many CGI libraries. They offer a possible replacement for CGI applications and much more. With servlets, except for the interface libraries, all of Java is available. Applet developers will find most of servlet development extremely familiar, requiring only a change in focus and class library. Java Database Connectivity (JDBC) remains a key tool for connecting to databases, Remote Method Invocation (RMI) is available for distributing applications, and the entire toolbox of Java utility classes is available.

Sun provides a basic set of tools in the

javax.servlet.http package
. (If you’re using classes in

sun.servlet.http
, watch out — these are now obsolete.) At its simplest, Java servlet development is a matter of creating appropriate code to handle the doGet and doPost methods, writing output to the response’s output stream. To demonstrate the point, I offer the code for a simple servlet.

While this code only provides the basic Hello World, in a package that probably seems a bit more intimidating than it’s worth, it also provides a framework for a much more complex set of interactions using the

HttpServletRequest
and

HttpServletResponse
objects. Java Servlets provide a rich set of tools for receiving data from and sending data to Web browsers, including a powerful Session class for maintaining state between user visits.

Developers who want to create pages that only use a little bit of Java — not full-fledged programs — are also in luck. Sun provides a

PageCompileServlet
that takes hybrid HTML/Java pages and compiles them into servlets. It does this transparently; the server will run files that end with .jhtml through the

PageCompileServlet
the first time they are requested and store the compiled version for future use. Java statements can be included within <java> tags, or marked with backquotes (when code is inserted into tags.) Developers who have worked with Netscape’s Server-Side JavaScript (formerly LiveWire) environment should find this familiar.

Beyond the Web: servlets on their own

Although most developers will probably use servlets (at least at first) in place of the traditional CGI mechanisms, servlets aren’t tied to the HTTP protocol. Servlets that extend the

GenericServlet
class intead of

HttpServlet
can define a service method more appropriate to a different task, receiving and processing information that isn’t just for Web s. Servlets can even be containerized as Beans, using that toolset for communication with other Beans and other applications. Servlets can even be chained together, with one servlet calling another further down the line, making it easy to create specialist servlets that perform one task well for other servlets, avoiding unnecessary code duplication.

Servlet chaining opens up new horizons for servers. Applications that need translation, connections to a particular data set, or format conversions can use a servlet to handle the intermediate tasks, avoiding the clutter of enormous applications. An XML application, for instance, might use a servlet to validate XML, or even convert it to an HTML equivalent, before passing the data back to the user.

A new world of Java

Servlets provide a standard set of tools for building server-side Java applications. While they’re only one part of the rapidly growing Java toolbox, they are one of the most promising — and give Java developers easy access to Java’s power without requiring too much additional training time. The API and implementation are simple, providing a welcome relief from the complexity of client-side interfaces, the difficulties of multiple operating systems, nonstandard virtual machines and users frustrated by waiting for their applets to download.

Links on this article:

Related Link:

Check out the developer.com Discussions area for more servlet talk.

Simon St. Laurent is an experienced Web developer who has been fiddling with hypertext since 1989 and working with the WWW since 1994. He has worked for a number of multimedia and Web design firms on projects for clients from small start-up businesses to Fortune 500 companies. He currently lives in Greensboro, NC. His first book, Dynamic HTML: A Primer, was published by MIS: Press in August, and he is currently hard at work on his next two books: XML: A Primer and Cookies.


Get the Free Newsletter!
Subscribe to Developer Insider for top news, trends & analysis
This email address is invalid.
Get the Free Newsletter!
Subscribe to Developer Insider for top news, trends & analysis
This email address is invalid.

Latest Posts

Related Stories