Java Servlet Examples

The examples on this page are from various sources. They have been collected and implemented by Jeff Schmitt.

servletrun
This script starts the servletrunner server. It is called from each of the servlet examples below to make sure that the server is running. After starting the server, it redirects the URL to the server for processing.
view source

servletstop
This script stops the servletrunner server. It is necessary to stop the server each time you copy a new .class file to the servlet directory, or the server will continue to use the old .class file. The server will be automatically restarted by servletrun.
view source

Guestbook 6 -- by Jeff Schmitt
This servlet is a re-write of guestbook3 which has been converted to the servlet technology.
view source

Project: This program already has a separate button for viewing the database, but the submit button should just print the one person's submitted data and not the whole database.

Servlets have a major advantage over CGI programs in that loading the JDBC driver and connecting to the database server can be done in the servlet's init method, then each subsequent service transaction can be handled using this existing connection. The way it is written now, the JDBC driver is loaded and a database server connection is established for every transaction which is extremely inefficient and time consuming. Your project is to also implement this change.

Name:
Affiliation:
E-mail:

Guestbook 6 Oracle -- by Jeff Schmitt
This servlet is a re-write of guestbook6 which has been converted use Oracle instead of MySQL
view source

Name:
Affiliation:
E-mail:

Hello World Servlet
This servlet displays the famous first line of any programmer, "Hello World".
view source

SimpleServlet
This servlet, as the name implies, is another simple example of a servlet sending some output.
view source

Date Servlet
This servlet displays the current date and time local to timezone of the server running the servlet.
view source

Counter Servlet
This servlet demonstrates a servlet that uses Cookies to store the number of times a user has visited your servlet.
view source

Snoop Servlet
This servlet displays some header information from your request as well as any form data. This servlet is useful as a diagnostic tool to see what your browser is sending to your servlet.
view source

Finger Servlet
This servlet acts as a simple gateway for the finger protocol. It demonstrates how a servlet can make its own socket connection to the outside world:
view source

Username:
Hostname:

Upper Case Filter Servlet
This servlet changes all text to upper case
view source

RedirectServlet
This servlet will redirect your browser to a specific location. It demonstrates the ability for a servlet to send redirect headers. Enter a URL in the field below and the servlet will take you there.
view source

Location:

Link Checker Servlet
This servlet checks to see that all links in a hierarchy on a local host (below the start URL) are valid. This servlet also demonstrates the use of sockets and URL connections:
view source

Full URL to start from:
Recursively search all levels Only to this level

Bulletin Board Servlet
This is a bulletin board that is maintained by the server. Entries are parsed as HTML, so you can post anything from plain text to applets. The entries are saved to a file, so the board will survive server shutdowns and the like.
view source

Enter message:

National Flag Servlet
This servlet from Chapter 17 of Java 1.1 Developer's Handbook (Sybex, 1997) illustrates the technique of generating an image.

Special technique: Use this servlet (or one like it) to include an image in a page that uses a form with the POST method to communicate with a servlet. This should help to insure that your server is running when the form is submitted.
view source

Country:

Session Servlet
This is a simple example of an HTTP Servlet that uses the HttpSession class. Note that in order to guarantee that session response headers are set correctly, the session must be retrieved before any output is sent to the client.
view source

Session Demo
This is a simple example of an HTTP Servlet that uses the HttpSession class. Javasoft Servlet Examples website.
view source

Duke's Bookstore Demo
a collection of servlets to implement a bookstore complete with database access and a shopping cart. This demo is under construction by Jeff Schmitt. The .jsp pages are being converted to servlets and the database is being converted to Oracle.

You may want to view the

You may want to consult the original version of this code which came in the file (DONT CLICK ON THIS!)bookstore.jar which included JSP pages and was coded for MSQL database.

<SERVLET> Tag Sample
This page demonstrates the use of the <SERVLET> tag and runs the SnoopServlet in a table. This is not working at present.
view source