Servlet Tutorial - Some Examples of Java Servlets

Introduction

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 - Simple.java

Let's start with a very simple servlet class.

          import java.io.*;
          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
             {
                response.setContentType("text/html");
                PrintWriter out = response.getWriter();

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

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

      all:    $(CLASSES)

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

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

      Simple.class:   Simple.java
              javac Simple.java

                <!-- The invoker servlet, handling /servlet/* URLs -->
                <servlet>
                    <servlet-name>invoker</servlet-name>
                    <servlet-class>
                      org.apache.catalina.servlets.InvokerServlet
                    </servlet-class>
                    <init-param>
                        <param-name>debug</param-name>
                        <param-value>0</param-value>
                    </init-param>
                    <load-on-startup>2</load-on-startup>
                </servlet>

                <servlet-mapping>
                    <servlet-name>invoker</servlet-name>
                    <url-pattern>/servlet/*</url-pattern>
                </servlet-mapping>

                <!-- The Simple servlet, as URL /s -->
                <servlet>
                  <servlet-name>simple</servlet-name>
                  <servlet-class>Simple</servlet-class>
                  <load-on-startup>5</load-on-startup>
                </servlet>

                <servlet-mapping>
                  <servlet-name>simple</servlet-name>
                  <url-pattern>/s</url-pattern>
                </servlet-mapping>

JDBC Servlets Examples - Servlet_Postgres.java and Servlet_MSSQL.java

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 Servlet_Postgres.java and Servlet_MSSQL.java.

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

wget http://www.doc.ic.ac.uk/csg-res/static/jdbc/Servlet_Postgres.java                          and
wget http://www.doc.ic.ac.uk/csg-res/static/jdbc/java/jdbc/Servlet_MSSQL.java

      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)

      clean:
              /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)

      Simple.class:   Simple.java
              javac Simple.java

      Servlet_Postgres.class:   Servlet_Postgres.java
              javac Servlet_Postgres.java

      Servlet_MSSQL.class:   Servlet_MSSQL.java
              javac Servlet_MSSQL.java

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

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