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 the example below, the port is changed to 9999 -- please, do not use 9999 :).

    <!-- Define a non-SSL Coyote HTTP/1.1 Connector on port 9999 -->
    <Connector port="9999" protocol="HTTP/1.1"
    maxThreads="150" connectionTimeout="20000"
    redirectPort="8443" />

    The comments in your "server.xml" file are very helpful. Please, read them!

    You also need to change the control port here:

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

     

  2. (Nothing to do!) 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 $CATALINA_HOME/lib/ .  This should be already done.

     

  3. Copy this simple servlet example:

      cp -r /stage/classes/current/51024-1/pub/hello $CATALINA_HOME/webapps/

  4. Modify the servlet in: $CATALINA_HOME/webapps/hello/WEB-INF/classes/HelloWorldExample.java  so that it prints your name in the body of the webpage. Now take a look at the web.xml file. It defines your servlet and provides a target url for the webserver so that it knows to resolve a specific url to hit the servlet. What is that URL? Recompile the servlet.
     
  5. Start your http server with:  $CATALINA_HOME/bin/startup.sh (If you need to stop it first:  $CATALINA_HOME/bin/shutdown.sh).  You'll see something like:

      Using CATALINA_BASE:   /home/alkhafaj/tomcat
      Using CATALINA_HOME:   /home/alkhafaj/tomcat
      Using CATALINA_TMPDIR: /home/alkhafaj/tomcat/temp
      Using JAVA_HOME:       /opt/java2/jdk1.5.0_06

       

  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/HelloWorldExample

    The servlet should display your name.


B) Simple Browser/Servlet communication

  1. Copy this simple servlet example:

      cp -r /stage/classes/current/51024-1/pub/simple-get $CATALINA_HOME/webapps/

  2. There should be an html file in the simple-get directory and two java files in the class directory located in the directory structure just like the Hello World example. Take a close look at the web.xml file to understand what has been deployed. Try to make the servlet SimpleServlet.java work with the simple.html file 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 is, 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. Documentation on classpaths for servlets can be found here.

Before you go further, make sure you have read the class loader information above! You should check the following directories and look for the ojdbc*.jar file.

  • $CATALINA_HOME/lib
  • $CATALINA_HOME/lib/*.jar
  • If you don't have the odbc*.jar file in any of the directories above, you will have to put it under one of the following directories.

  • /WEB-INF/classes of your web application
  • /WEB-INF/lib/*.jar of your web application
  • Remember that you have limited network drive capacity so you should NOT directly copy it. Instead, try creating a symbolic link using ln -s. See the man page of ln for details. The jdbc driver classes are located at:

    /stage/classes/current/51024-1/ojdbc

    1. We will modify the previous example to log all data entered by the user into a database table. There should be a java source file: $CATALINA_HOME/webapps/simple_get/WEB-INF/classes/ServletJDBC.java which is part of the directory you just copied. You have to modify the servlet to work with your own table or at least to create a new table in the database and then recompile the servlet code. Then, you must create a simpleJDBC.html file (copy and modify simple.html) and set it to work with the SimpleJDBC servlet. Finally, you must modify the web.xml file so the target for the simpleJDBC.html get will resolve to the SimpleJDBC servlet.
       
      Try to use the servlet a few times. Notice the log numbers. Kill your browser, bring up a new one and use the servlet a few more times. What seems to be happening? Now shut down tomcat, bring it back up again and try the servlet again a few times. What can you tell about servlet persistence?
       
      Now take a closer look at the ServletJDBC.java code. It has init() and destroy() functions. Can you tell what is persisting through the life of the servlet? If a servlet is going to make calls to a database for multiple clients, is it a good idea to have the servlet maintain a constant connection to the database? Why or Why not?

       
    2. After going through all these examples you can start working on Milestone1


    D) To use Tomcat's Application Manager: