Dave Cater

Java - Servlets

 
Home Page
Career outline
Java
Java references
Java - XML parsing using SAX
Java - XML parsing using DOM
Java - Servlets
Java - SOAP
Linux
Security
Perl
System management
Testing
Musical notes
Early interest focused on using Java to write applets to run in a client's browser. Java is becoming increasingly important as a language for server programming.

The Java 2 Standard Edition contains classes in the java.net package for creating Java servers along traditional networking programming lines. This involves creating sockets (transport endpoints) bound to TCP ports. Thus a Java server may listen on a well-defined port (such as port 80) and read a raw byte stream which contains HTTP requests, sending HTTP responses in an output byte stream. One could construct a complete Java based HTTP server for use on the WWW.

An alternative approach is to create a Java runtime environment which can be connected to an existing Web server, and allows Java components to be plugged into that environment. This hides the details of how HTTP requests and responses are handled, and allows Java components to be used by up-to-date industrial strength Web servers.

Java Servlets is a standard framework for these components. A simple Java Servlet API is defined by Sun Microsystems as a Java Enterprise standard.

These notes describe my efforts to develop and deploy Java servlets on Mandrake Linux.

  • I followed the Java servlets tutorial in PC Plus (September 2001). as an introduction. This involved downloading the binary distribution of the open source Java servlets engine known as Tomcat from the Apache Project site. The file jakarta-tomcat-3.2.3.zip was downloaded from http://jakarta.apache.org/tomcat and unpacked using the unzip command.

  • To read the documentation I used the following commands:
    	TOMCAT_HOME=`pwd`/jakarta-tomcat-3.2.3 export TOMCAT_HOME
    	cd $TOMCAT_HOME/doc
    	netscape `pwd`/index.html
    

  • I installed and started the software as described in the Tomcat User's Guide. I then accessed the Tomcat home page locally using netscape to access the URL http://localhost:8080. This gives more information on the servlets development environment than in the magazine article, as well as links to other servlet related Web sites.

  • Somewhile later I discovered that you MUST have JAVA_HOME set in order to do much useful. Tomcat will generate ClassNotFound errors, or similar, and these will be sent to the client process for most SOAP calls.

  • I then looked through the document Developing Applications With Tomcat to get a rough idea what was involved in creating a simple servlet from scratch. The document uses an IDE (Integrated Development Environment) called "ant", but I decided I wanted to create a servlet from scratch using just the Java SDK (and good old vi, of course).

  • I followed the article quite closely in creating a Java source file Hello.java. I then added each of the jar files in the Tomcat lib directory to my CLASSPATH environment variable and tried to compile the code using the command
    	javac Hello.java
    

  • I found that the class to extend is called HttpServlet (not HTTPServlet as in the article). Incidentally, I found the class names using the commands
    	cd $TOMCAT_HOME/lib
    	$JAVA_HOME/bin/jar tf servlet.jar
    
    I also had to include the line
    	import java.io.*;
    
    at the top of the file which the article didn't mention explicitly. I'm getting the hang of all this now ...

  • Instead of creating a new Context entry in server.xml, I found it is possible to create a new sub-directory (dave) in an existing directory (webapps). I copied the file Hello.class into $TOMCAT_HOME/webapps/dave/WEB-INF/classes. When Tomcat is restarted it automatically creates a Context for each sub-directory such as dave (see the Tomcat log files for startup information which logs these directories).

  • It is then possible to run the servlet via the following URL:
    	http://localhost:8080/dave/servlet/Hello
    
    Success! My first Java servlet.