Part 3: First Servlets
2. A Simple Servlet Generating Plain Text
Here is a simple servlet that just generates plain text.
The following section will show the more usual case where HTML is generated.
2.1 HelloWorld.java
You can also
download the source or
try it on-line.
package hall;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletRequest response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("Hello World");
}
}
2.2 Compiling and Installing the Servlet
Note that the specific details for installing servlets vary from Web server to Web server.
Please refer to your Web server documentation for definitive directions.
I'm using Sun's Java Web Server 2.0 for these examples, where servlets are expected to be in a directory called "servlets" in the JWS installation hierarchy.
However, I placed this servlet in a separate package to avoid conflicts with other servlets on this server; you'll want to do the same if you are using a Web server that is used by other people.
Thus, HelloWorld.java actually goes in a subdirectory called "hall" in the servlets directory.
If you've never used packages before, you'll probably find that the easiest way to compile things is to go to the directory above the one containing your servlets, and then do:
"javac directory\ServletClass.java" (Windows; note the backslash)
or "javac directory/ServletClass.java" (Unix; note the forward slash).
So, in this case, I pop up a DOS window (Windows), change directory to the "servlets" directory, and do "javac hall\HelloWorld.java".
Note that, on Windows, most JDK 1.1 versions require a backslash for the directory name with javac, even though they don't for "java".
This is fixed in JDK 1.2, but since many Web servers are configured to use JDK 1.1, many servlet authors stick with JDK 1.1 for portability.
For this simple example, you could do javac directly in the package-specific subdirectory ("hall" in this case), but that fails for later examples when the servlet class uses other classes in the same package.
Advanced package users will note that you could also set your CLASSPATH to the "servlets" directory, or keep the source code in a location distinct from the .class files, and use javac's "-d" option to install them.
2.3 Running the Servlet
With the Java Web Server, servlets are placed in the "servlets" directory and are invoked via http://host/servlet/ServletName.
Note that the directory is "servlets", plural, while the URL refers to "servlet", singular.
Since this example was placed in the "hall" package, it would be invoked via http://host/servlet/hall.HelloWorld.
NEXT
This tutorial is now available as a book: Core Servlets and JavaServer Pages by Marty Hall, published by Sun Microsystems Press.
Read all about it at CoreServlets.com
Server-Side Web Applications using Java Servlets versions 2.1/2.2 and JavaServer Pages (JSP) version 1.0: A Tutorial
© 1999-2000 Marty Hall.
All source code freely available for unrestricted use.
Created for work in the Research and Technology Development Center of the Johns Hopkins University Applied Physics Lab, for courses in the Johns Hopkins Part-Time MS Program in Computer Science, and for various industry seminars and on-site Java short courses.
Please note that this is a first draft of the tutorial, so please send corrections, comments, and suggestions to me at hall@apl.jhu.edu.
Reprinted with permission from the author. Click here to visit the original version
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|