For the developer that dabbles in Java programming, servlets can be used to turn the Java Web Server into their own personal Web application server.
One of the slickest pieces of software to come from
JavaSoft recently is the Java Web Server (formerly known as Jeeves).
While the Java Web Server can be used for serving up Web sites
just like any other Web server (using SSL, CGI scripts, authentication
and more), its ability to use Java servlets is what makes it stand
out from other Web servers. For the developer that dabbles in
Java programming, servlets can be used to turn the Java Web Server
into their own personal Web processor.
Servlets, (server-side applets), are very similar
to applets in that they can both be included in Web pages and
they both run on any Java-enabled platform-but servlets have no
graphical front end. Servlets can be used to insert, sort and
delete data from a database, create roaming Web agent servlets,
interactive HTML editors or perform just about any Web-related
function. Not only can you use servlets with the Java Web Server, but you can also use
them with Netscape FastTrack and Enterprise servers,
the JigSaw Web server,
and the Acme Serve Web server.
As the development and acceptance of Java continues to escalate,
more and more Web servers will conform to the Java Server API.
Later we'll discuss what other companies are doing with
this same concept.
To create servlets you'll need to download the Servlet
Development Kit from the JavaSoft site
and you'll also need to already have the Java Development Kit
1.1 installed. You can also get the JDK 1.1. from JavaSoft's site,
but keep in mind that applets produced with the JDK 1.1 are not
yet supported by the release versions of Netscape Navigator or
Microsoft Internet Explorer (they should be supported in the next
release of both browsers).
The Java Web Server was created using the Java Server
API. The Java Server API is a "cross-platform, extensible
framework for the creation of network-centric server solutions."
The Java Web Server is one of the first applications that Sun
developed using the Java Server API. It is administered (locally
or remotely) through any Java-capable browser. While the Java
Web Server is already a very full-featured Web server, it ships
with several Core servlets that further enhance its functionality.
The Core servlets include:
File Servlet - provides the standard document
serving capabilities of the Java Server, including caching and
parsing for SSIs
Invoker Servlet - invokes other servlets which
are explicitly requested by name
Server Side Include Servlet - any file with a
.shtml extension is parsed and written out to the client, and
the embedded servlet is loaded
Admin Servlet - provides administration of the
Java Web Server through a browser front end
CGI Servlet - acts as a gateway for the CGI 1.1
interface; any standard CGI 1.1 program will run
Imagemap Servlet - enables the use of standard
NCSA server-side imagemaps
Among the servlets that are included, but not documented,
are:
BBoardServlet - a bulletin board servlet
ChatServlet - a real time chat servlet
DBDataEncoder - encodes data into a database
MailServlet - take the input from a form and
emails it
RedirectServlet - a servlet to redirect the client
SimpleFormServlet - a servlet that takes form
input and redirects it into an HTML page
Sun has seen fit to include not only the .class files,
but the source for all the above servlets (also included are several
well-documented servlets, including Snoop, Counter, Hello
World, Finger and Certificate Authority servlets). By looking
at the code for the SimpleFormServlet, I was able to come up with
a better FormServlet of my own.
/** * Note that to compile servlets, the path containing the Java
Servlet library files must be set in the CLASSPATH. */
public void sendResponse(HttpServletResponse sresp, Hashtable
Htab) throws IOException { Enumeration keys; String key; String value; OutputStream out; String title = "WD Form Servlet"; String bgcolor = (String) Htab.get("bgcolor"); String text = (String) Htab.get("text"); String name = (String) Htab.get("name"); String city = (String) Htab.get("city");
sresp.setContentType("text/html"); out = sresp.getOutputStream(); HtmlPage page = new HtmlPage(title); page.add("<BODY BGCOLOR=" + bgcolor + " "
+ "TEXT=" + text + ">"); page.add(title, "H1"); page.add("<P>Thanks for the info <B>"
+ name + "</B> from " + city + "!<BR>"); page.add("We'll be getting back to you soon!"); page.write(out); out.flush(); } }
First we set the HttpServletResponse
that is to be returned as text/html, the standard Web page mime-type:
HttpServletResponse.setContentType("text/html");
Then we assign the values of the data from the form
to a hashtable, and assign the values from the hashtable to strings
for later reference:
String bgcolor = (String) Htab.get("bgcolor");
String text = (String) Htab.get("text");
By making the "bgcolor" and "text"
fields hidden fields in the HTML form, we can use them to set
the background and text color of the response page. When we send
the reply page, we can use the viewer's name and city to make
the page more specific to them:
page.add("<P>Thanks for the info <B>"
+ name + "</B> from " + city + "!<BR>");
<insert wdformservlet.jpg & htmlcode.jpg (only
use htmlcode.jpg if you want to put them together, Matt)> Caption
-
Using a standard HTML form, the servlet is called
to return a Web page and send the form data to an email address.
The really slick thing about servlets is that you
can do virtually anything with them. This applet could have been
written to take the information from the form and email it to
a the person that filled out the form. This is what that servlet's
source would look like:
// Here we're getting the background and text colors from the
form's hidden fields and writing // the page accordingly. We also get the rest of the information
and assign each field's value to // a string. Then (below) we check to see if the name and email
fields contain data. If they don't, then we // know something's up and send an appropriate response to
the client (see 'else' below).
if ((name.length() > 0) && (email.length() >
0)) { try { sendmail = new SmtpClient("mail.yoursite.com"); sendmail.from("wd@mail.yoursite.com"); sendmail.to(email); ps = sendmail.startMessage(); succeed = true; } catch (Exception e) { page.add("Can't get to a server outside. We'll try your
own mail server."); }
// If we can't get to a mail server on the Internet, then the
user is probably behind a firewall, // so the next best thing is to use a mail server from your
own site. We can use InetAddress to // find the user's local host and host name.
if (!succeed) { try { sendmail = new SmtpClient(InetAddress.getLocalHost().getHostName()); sendmail.from(email); sendmail.to(email); ps = sendmail.startMessage(); } catch (Exception e) { page.add("Captain, there appears to be a problem!"); return; } } try { ps.println("From: " + email); ps.println("To: " + email); ps.println("Subject: WD Form Servlet is the heat!"); ps.print("\r\n"); ps.println("Hello " + name + ","); ps.println("This is your info:"); ps.println(streetaddress); ps.println(city); ps.println(state); ps.println(zip); ps.println(); ps.println("We thought you might be interested in the
Java Web Server."); ps.println(); ps.println("-WD."); // Here we take the strings with the hashtable values and print
them in the email. // Then we send the email and close the connection. ps.flush(); ps.close(); sendmail.closeServer(); page.add(name + ", check your email for a message from
the WD team."); } catch (Exception e) { page.add("More problems, Captain no email for you!"); return; } } else page.add("We need your name AND your email address"); page.write(out); out.flush(); } }
Instead of sending the info to the person that filled
out the form, we could have sent it to the system administrator.
Or the servlet could have written the data from the form to a
database, which could later be edited or deleted from the database
using yet another servlet. Servlets can also write HTML to the
page, including applets (or other servlets), Shockwave animations,
midi files, etc.
The Java Web Server is basically proof of the pudding
for Sun proof that their vision can be a reality. Since Java
is platform-independent, applets, applications and servlets-modules,
so to speak-can be little dynamic pieces of code that turn an
otherwise obsolete piece of hardware into a machine capable of
running chat servers, video conferencing systems, text chat servers,
file servers, Web servers-all "modules" or servlets.
Other companies are also using the same type of model to structure
their own client/server operating system. Skunk Technologies
recently released their Entao Open Operating System, which is
a 100% Java-written network operating system. Entao is a complete
solution for businesses with legacy systems that use proprietary
IT technologies. The main "Base" server serves as a
foundation for all the different "modules" that are
each designed for a particular purpose, and will run on any platform
that can run the Java Virtual Machine. The Base Server is built
to support CORBA, JDBC, Java Beans, ActiveX, SSL, and SQL, which
is a good indication of its extendibility. New modules can be
created or added to those that come with the Entao Open Operating
System. The basic suite of Entao's Java-based applications includes:
Entao Contact Manager - a vCard compliant information
manager that tracks user's appointments and availability
Entao Database Controller - enables users to
connect to SQL and ODBC compliant databases (also supports JDBC)
Entao Email - a POP3, MIME, SMIME and IMAP4 compliant
email client
Entao Message Center - a threaded discussion
group that supports SSL, digital signatures and security log-in
capability
Entao Text Chat - a simple solution for creating
virtual communities on the Internet or intranet.
Entao Media Module - enables streaming video
to Internet and intranet users
Entao Visual Chat - takes text chat a step further
with the use of avatars, sound and interaction
Entao Web Server - an HTTP 1.0 compliant Web
Server that complies to the JavaSoft Servlet API and is remotely
administered
Entao Workspace - a customizable desktop interface
Entao is currently working to create many other modules
including Commerce, real-time Media servers, Storefront and Ad
servers. Third party companies may also be coming up with their
own plug-in modules in much the same way as third party VBX and
OCX vendors did for Microsoft Visual Basic and Visual C++. All
the Java naysayers that say that they haven't seen any solid Java
applications only need take a look at the Java Web Server or the
Entao Operating System for proof.
If you have any Java tips, techniques or hacks that
you'd like to share, we'd be happy to hear from you. Write to
me at sclark@webdeveloper.com.
We'll let you share it with the whole wide world, make you famous,
and give you a Web Developer® goody bag. Until next time keep
burning the midnight oil.