499 Simple Servlet Demo


1.   Create a directory called "caradd"

2.   Create a directory in carradd called "WEB-INF"

3.   Create a directory in caradd called "classes". This is where you place your servlets code (i.e. the java files and classes). While strictly speaking you don't have to keep your java files here, it is nice to have them all in one place

4.   Create a servlet in 'classes' called CarAdd.java. Here is some sample code:

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;


public class CarAdd extends HttpServlet



     * Handle the HTTP GET method by building a simple web page.


    public void doGet (HttpServletRequest request,

                                 HttpServletResponse response)

          throws ServletException, IOException


          PrintWriter         out;

          String              title = "Car Add Placement Servlet";


          // set content type and other response header fields first



          // then write the data of the response

          out = response.getWriter();





          out.println("<H1>" + title + "</H1>");

          out.println("<P>This is output from the car add servlet.");






1.   Create a file called "web.xml" in the 'WEB-INF' directory. This binds a servlet name to the servlet class. Here is the "web.xml" file for this applet:


<?xml version="1.0" encoding="ISO-8859-1"?>


<!DOCTYPE web-app

    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"















1.   Start a Tomcat server

2.   Open http://<machine>:<port>/caradd/caradd in a web browser where machine is the name of the machine you are using and port is your first port number. If you don't have a port number use 8080.


Other Servlet Resources


CMPUT 391 Servlets Information



This is from a database course that is being offered currently that is also using servlets. It contains setup information for the UAlberta environment.


Servlets Tutorial



This is Sun's Java tutorial for servlets


Servlets API



The Java Servlet API documentation


Tomcat Documentation