next up previous contents
Next: Using Velocity Up: First Java Servlet Previous: First Java Servlet

The 'hello world' servlet from Tomcat

Tomcat comes with a simple servlet example. You can find this example in $TOMCAT_HOME/doc/appdev/sample
   [rmorrien@escay sample]$ cd \$TOMCAT\_HOME
   [rmorrien@escay tomcat]$ cd \$TOMCAT\_HOME/doc/appdev/sample
   [rmorrien@escay sample]$ ls -al
   total 32
   drwxr-xr-x    5 1225     1225         4096 May 11 19:51 .
   drwxr-xr-x    3 1225     1225         4096 May 11 19:51 ..
   -rw-r--r--    1 1225     1225          583 May 11 19:51 build.bat
   -rwxr-xr-x    1 1225     1225          482 May 11 19:51 build.sh
   -rw-r--r--    1 1225     1225         2276 May 11 19:51 build.xml
   drwxr-xr-x    2 1225     1225         4096 May 11 19:51 etc
   drwxr-xr-x    2 1225     1225         4096 May 11 19:51 src
   drwxr-xr-x    3 1225     1225         4096 May 11 19:51 web
   [rmorrien@escay sample]$
Copy this directory to a sub directory of your home directory (not a root home directory!) e.g copy the files to /home/yourname/javaprogram/.

Go to the directory en try to build the sample servlet program with the build.sh file. Note: Make sure the build.sh file is executable (chmod +x build.sh)!

   [marloes@escay jsp]$ ./build.sh
   Searching for build.xml ...
   Buildfile: /home/marloes/jsp/build.xml

   prepare:

   BUILD FAILED

   /home/marloes/jsp/build.xml:12: Directory /usr/tomcat/weba
   pps/myapp creation was not succesful for an unknown reason

   Total time: 5 seconds
   [marloes@escay jsp]$

The build file tries to install the helloworld application in $TOMCAT_HOME/webapps/myapp/, you don't have write access to that directory. Create the directory as root and give write access to the users group.

   [root@escay /root]# cd $TOMCAT_HOME/webapps
   [root@escay webapps]# mkdir myapp
   [root@escay webapps]# chown marloes myapp
   [root@escay webapps]# chgrp users myapp
   [root@escay webapps]# ls -al
   total 1316
   drwxr-xr-x   10 1225     1225         4096 Jun  7 14:32 .
   drwxrwxr-x   10 root     root         4096 Jun  4 15:13 ..
   drwxrwxr-x    6 root     root         4096 May 23 19:25 ROOT
   -rw-r--r--    1 1225     1225      1069459 May 11 19:52 ROOT.war
   drwxrwxr-x    5 root     root         4096 May 23 19:24 admin
   -rw-r--r--    1 1225     1225         7041 May 11 19:52 admin.war
   drwxrwxr-x    7 root     root         4096 May 23 19:24 examples
   -rw-r--r--    1 1225     1225       122214 May 11 19:52 examples.war
   drwxrwxr-x    5 rmorrien rmorrien     4096 May 23 20:26 forumdemo
   drwxrwxr-x    2 marloes  users        4096 Jun  7 14:32 myapp
   drwxrwxr-x    8 root     root         4096 May 23 19:25 test
   -rw-r--r--    1 1225     1225        89567 May 11 19:52 test.war
   [root@escay webapps]# exit
   exit
The rights are set to user marloes in group users. Now try to build:
   [marloes@escay sample]$ ./build.sh
   Searching for build.xml ...
   Buildfile: /home/marloes/sample/build.xml
   
   prepare:
        [copy] Copying 3 files to /usr/tomcat/webapps/myapp
       [mkdir] Created dir: /usr/tomcat/webapps/myapp/WEB-INF
        [copy] Copying 1 files to /usr/tomcat/webapps/myapp/WEB-INF
       [mkdir] Created dir: /usr/tomcat/webapps/myapp/WEB-INF/classes
       [mkdir] Created dir: /usr/tomcat/webapps/myapp/javadoc
   
   compile:
       [javac] Compiling 1 source file to /usr/tomcat/webapps/myapp/WEB-INF/classes
   
   BUILD SUCCESSFUL
   
   Total time: 14 seconds
   [marloes@escay sample]$
Everthing was build succesful. Now you can start Tomcat (as root..). Go to the /usr/tomcat directory. Start Tomcat with ./bin/startup.sh. Always start tomcat from the TOMCAT_HOME directory!!

Open your browser and enter the URL:

   http://127.0.0.1:8080/myapp/
You should see:
   Sample "Hello, World" Application

   This is the home page for a sample application used to 
   illustrate the source directory organization of a web
   application utilizing the principles outlined in the 
   Application Developer's Guide.
You can see two links, one to a servlet and one to a JSP page. The link to the servlet wordks directly, but the link to the JSP page looks to 'hang'the first time you visit it. The first time someone visits a JSP page, the JSP page has to be re-build to a servlet by Tomcat. This costs time. That's one of the reasons we will be using Velocity.


next up previous contents
Next: Using Velocity Up: First Java Servlet Previous: First Java Servlet
Copyright © 2001, R.M. Morriën, H.P.A.M. Snoek, M.A.C. Bakker