Servlet Tutorial - Some Examples of Java Servlets


Ok, so what - in slightly more detail - is a Java Servlet? A Java Servlet is a Java class that subclasses from class HttpServlet and usually overrides the doGet (or doPost) method. These methods will be (later) invoked automatically when an appropriate web request is made, and each method produces a stream of dynamic html (or other) output which will be returned to the web browser.

To recap what we said in the main JSP and Servlets document, every context directory has a partially fixed layout as shown below:

Context Diagram

In particular, inside each servlet context directory there should be a special directory called WEB-INF, often containing two subdirectories called lib and classes and an optional mapping file called web.xml. Any Java classes you need for your web application (whether servlet classes, support classes or java beans) can either be placed in WEB-INF/classes as individual precompiled .class files, or can be packaged up into .jar archive files and then be placed into WEB-INF/lib.

Before a servlet's doGet()/doPost() method can be called, several things need to have been done:

   mktomcat6 ~/example-context
   cd ~/example-context

A Simple Example Servlet -

Let's start with a very simple servlet class.

          import javax.servlet.*;
          import javax.servlet.http.*;

          public class Simple extends HttpServlet {
             int accesses = 0;

             public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws IOException, ServletException
                PrintWriter out = response.getWriter();

                out.print("Number of times this servlet has been accessed:" + accesses);

      CONTEXT =       ../webapps/ROOT
      CLSDIR  =       $(CONTEXT)/WEB-INF/classes
      CLASSES =       Simple.class

      all:    $(CLASSES)

              /bin/rm -f $(CLASSES) */*.class

      install:        $(CLASSES)
              install -m600 $(CLASSES) $(CLSDIR)


                <!-- The invoker servlet, handling /servlet/* URLs -->


                <!-- The Simple servlet, as URL /s -->


JDBC Servlets Examples - and

See our separate JDBC with DoC Supported Databases document for a single worked example (Films and directors) shown in multiple different forms - including a pair of Servlet versions, one connecting to Postgres and the other to Microsoft SQL Server. Specifically, here's the source code of and

Let's try to get them both working in Personal Tomcat, it's simple. Proceed as follows:

wget                          and

      cp /vol/www/csg-res/static/jdbc/Servlet*.java .

      CONTEXT =       ../webapps/ROOT
      LIBDIR  =       $(CONTEXT)/WEB-INF/lib
      CLSDIR  =       $(CONTEXT)/WEB-INF/classes
      JARS    =       servlets.jar
      CLASSES =       Simple.class Servlet_Postgres.class Servlet_MSSQL.class

      all:    $(JARS) $(CLASSES)

              /bin/rm -f $(JARS) $(CLASSES) $(CLSDIR)/* $(LIBDIR)/*

      install:        $(JARS) # $(CLASSES)
              install -m600 $(JARS) $(LIBDIR)
              #install -m600 $(CLASSES) $(CLSDIR)

      servlets.jar:      $(CLASSES)
              jar cf servlets.jar $(CLASSES)




     http://localhost:59999/servlet/Servlet_Postgres                                         and

guides/java/servlets/servlet (last edited 2010-05-25 19:12:06 by dcw)