David Wallace Croft
croft@alumni.caltech.edu
1997-11-12
While CGI programs can be written in Java, Java servlets add additional functionality including security, dynamic loading from remote servers, and the ability to add functionality to your web server.
Unlike CGI programs, servlets are loaded once and then stay resident. Future service calls to the servlet are faster since the servlet is already in memory.
Servlets can maintain state information between service calls.
The Servlet API is supported by most major web servers.
They are often compared to "applets", which are client-side Java programs that are loaded and run with within the framework of a web browser.
Instead of subclassing the java.applet.Applet class, you write your servlets by subclassing a class that implements the javax.servlet.Servlet interface.
Servlets are called "faceless" in that do not have a GUI component. Note that the Applet class extends the java.awt.Panel class which necessitates an interactive interface.
The corresponding life-cycle methods are
Note that this differs slightly from the Applet life-cycle which includes init ( ), start ( ), stop ( ), and destroy ( ).
It subclasses GenericServlet, a ready-made simple implementation of the Servlet interface. Note that I have included the init ( ) and destroy ( ) method calls to remind you that they can be overloaded.
import java.io.*;
import javax.servlet.*;
public class HelloServlet extends GenericServlet {
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
public void init ( ServletConfig config ) {
//////////////////////////////////////////////////////////////////////
super.init ( config );
}
public void service (
ServletRequest req, ServletResponse res )
throws ServletException, IOException {
//////////////////////////////////////////////////////////////////////
PrintStream out = new PrintStream ( res.getOutputStream ( ) );
out.println ( "Hello, World!" );
}
public void destroy ( ) {
//////////////////////////////////////////////////////////////////////
super.destroy ( );
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
}
This servlet extends javax.servlet.http.HttpServlet. The HttpServlet class "understands" HTTP requests coming from web browsers and provides additional functionality.
In this example, the output is a dynamically generated graphics image file (GIF).
public class RandomGIFServlet extends HttpServlet {
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
/*********************************************************************
* Services a single request from a client.
*********************************************************************/
public void doGet (
HttpServletRequest req,
HttpServletResponse res )
throws ServletException, IOException {
//////////////////////////////////////////////////////////////////////
short w = 16;
short h = 16;
int [ ] values = new int [ w * h ];
int i = 0;
for ( short x = 0; x < w; x++ ) {
for ( short y = 0; y < h; y++ ) {
values [ i++ ] = ( int ) ( Math.random ( ) * 0xFFFFFF );
}
}
try {
GIFEncoder gifEncoder = new GIFEncoder ( w, h, values );
res.setContentType ( "image/gif" );
ServletOutputStream out = res.getOutputStream ( );
gifEncoder.Write ( out );
} catch ( AWTException e ) { }
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
}
<FORM ACTION="postservlet" METHOD=POST> <INPUT TYPE=TEXT NAME="name" SIZE=40 VALUE="Enter your name here."><BR> <INPUT TYPE=TEXT NAME="mail" SIZE=40 VALUE="Your e-mail address." ><BR> <TEXTAREA ROWS=2 COLS=60 NAME="note">Send me a note here!</TEXTAREA><BR> <INPUT TYPE="SUBMIT" VALUE="Send"> </FORM>
public void doPost (
HttpServletRequest req, HttpServletResponse res )
throws ServletException, IOException {
//////////////////////////////////////////////////////////////////////
res.setContentType ( "text/html" );
ServletOutputStream out = res.getOutputStream ( );
try {
out.println ( "<html><head><title>" + getTitle ( )
+ "</title></head>" );
out.println ( "<body><center><H1>" + getTitle ( ) + "</H1>" );
String name = req.getParameter ( "name" );
String mail = req.getParameter ( "mail" );
String note = req.getParameter ( "note" );
out.println ( "Name: " + name + "<BR>" );
out.println ( "Mail: " + mail + "<BR>" );
out.println ( "Note: " + note + "<BR>" );
} catch ( Throwable t ) {
out.println ( "<P></CENTER><PRE>" );
t.printStackTrace ( new PrintStream ( out ) );
out.println ( "</PRE><CENTER><P>" );
}
out.println ( "</center></body></html>" );
}
It is free for non-commercial use and commercial licenses are inexpensive.
It runs on all platforms that support the JDK 1.1 but the least buggy versions run on Solaris, NT, and Win 95.
You can install it anywhere that you have command-line access, either remotely via telnet or on your desktop computer.
You do not need "root" access to install this program as you can hang it off of any port you choose. To hang it off of the default HTTP port 80, you will need root access. Otherwise, you will need to address your web pages using a URL of the form "http://www.domain.edu:8080/" where the port number is specifically included.
ANSER Java Agents and Extraneous (AJAX): a collection of servlet demos.
http://www.alumni.caltech.edu/~croft/research/java/servlet/
Copyleft 1997
David Wallace Croft
Posted 1997-11-12