Lab 3:  Servlet Programming

A.) Basic servlet setup:

  1. You installed Tomcat servlet engine during Lab1.

    Optional: You can change the port on which Tomcat is listening for incoming http connections by modifying the "port" property for "Connector" item in server.xml.  In example below port is changed to 9999.

    <!-- Define a non-SSL Coyote HTTP/1.1 Connector on port 8080 -->
    <Connector className="org.apache.coyote.tomcat4.CoyoteConnector"
    port="9999" minProcessors="5" maxProcessors="75"
    enableLookups="true" redirectPort="8443"
    acceptCount="100" debug="0" connectionTimeout="20000"
    useURIValidationHack="false" disableUploadTimeout="true" />

    You also need to change the control port here:

    <Server port="8005" shutdown="SHUTDOWN" debug="0">

     

  2. The CLASSPATH environment value passed to the JVM that executes your servlets can be modified in a number of ways (see Class Loader Info).  One way to change it is to make the Oracle JDBC library available in your ~/tomcat/common/lib/  .  This should be already done.
    1. Tomcat's 'shared' folder (~/tomcat/shared/lib) is for java libraries that need to be accessible to all of your web applications. Add the oracle jdbc jar into this folder with the command:
    ln -s /opt/java/jdbc/oracle12.jar oracle12.jar

     

  3. Copy this simple servlet example:

      cp -r /stage/classes/current/51024-1/labs/lab3/hello ~/tomcat/webapps/

  4. Modify the servlet in: ~/tomcat/webapps/hello/WEB-INF/classes/HelloWorldExample.java  so that it prints your name in the header line. Recompile the servlet.
     
  5. Start your http server with:  ~/tomcat/bin/startup.sh (If you need to stop it first:  ~/tomcat/bin/shutdown.sh).  You'll see something like:

      Using CATALINA_BASE:   /home/skuehn/tomcat
      Using CATALINA_HOME:   /home/skuehn/tomcat
      Using CATALINA_TMPDIR: /home/skuehn/tomcat/temp
      Using JAVA_HOME:       /opt/java/jdk1.3.1_11

       

  6. In a browser go to: (use the port number your http server is configured to work with.  The default port is 8080)

        http://localhost:port/

    to check that you started Tomcat.  Then go to:

        http://localhost:8080/hello/servlet/HelloWorldExample

    The servlet should display your name.


B.) Simple Browser/Servlet communication

  1. Take a look here for a simple example of doGet method usage.  You can look at the servlet code here: /stage/classes/current/51024-1/labs/lab3/SimpleServlet.java.  To see the html code use "View/Page Source" menu of your browser.
  2. Try to make this example work in your environment. (you need to copy files, deploy the servlet, restart tomcat, etc)
  3. You can also take a look at various examples from the Goodwill book. Another good source of information are, of course, the set of examples that come with Tomcat.
C.) Integrating Servlets with JDBC

Once you have the JDBC driver classes in the CLASSPATH built for your servlet engine ,accessing an Oracle database should work fine.

  1. We modified the previous example to log all data entered by the user into a database table. Go here to run the servlet.  The code is here: /stage/classes/current/51024-1/labs/lab3/ServletJDBC.java
  2. You have to modify the servlet to work with your own table or at least to create a new table in the database.
     
  3. After going through all these examples you can start working on Milestone1


D.) To use Tomcat's Application Manager:


ta-51024@cs.uchicago.edu