1. Notes
1.1. Accessing Tomcat
Web apps will be stored in the following directory:
-
Ubuntu: /usr/share/tomcat5/webapps
-
Gentoo: /opt/tomcat5/webapps
Try testing that Tomcat is started by accessing these webpages. You should expect to see a page that says "Tomcat" as a result:
-
Ubuntu: http://localhost:8180/
-
Gentoo: http://localhost:8080/
1.2. File Structure of a Project
-
http://tomcat.apache.org/tomcat-5.5-doc/appdev/deployment.html
-
says to organize files as such
./*.html ./*.jsp (etc.) ./WEB-INF/web.xml ./WEB-INF/classes/ (e.g. ./WEB-INF/classes/com/bar/foo/FooBarCom.class) ./WEB-INF/lib/ (for .jar files, e.g. JDBC drivers)
1.3. Hello World (.java version)
http://www.keyboardsamurais.de/mt/archives/000053.html
We are going to make a web application that should be accessible by the following URL:
-
(or on Ubuntu: http://localhost:8180/HelloWorld/hello)
We create the initial structure for the project:
mkdir HelloWorld cd HelloWorld mkdir WEB-INF mkdir WEB-INF/classes mkdir WEB-INF/lib mkdir WEB-INF/src
Now we can create a new class named HelloServlet in the directory WEB-INF/src. Now we can copy and paste following code into this class.
cat > WEB-INF/src/HelloServlet.java << EOF
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
public class HelloServlet extends HttpServlet {
public void doGet (HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException
{
PrintWriter out = res.getWriter();
out.println("Hello, Brave new World!");
out.close();
}
}
EOF
Now compile the Java class:
javac WEB-INF/src/* -d WEB-INF/classes/
Unfortunately we are not yet finished. We still need to create the web.xml descriptor, which contains certain elements of configuration specific to our application and the server behaviour. So we create the file web.xml in the directory WEB-INF (Note: not in the directory WEB-INF/src !!!). For our simple application, the following parameters should be fine.
cat > WEB-INF/web.xml << EOF
<!DOCTYPE web-app PUBLIC
'-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN'
'http://java.sun.com/dtd/web-app_2_3.dtd'>
<web-app>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
EOF
What follows now is a little explanation of what we just copied and pasted into web.xml. The Doctype is an xml specific item that tells the xml parser where to look for the dtd consistency rules for our xml document. The dtd ensures that no wrong parameter combinations are entered. The main tag <web-app> contains all preferences for our servlet. The <servlet> tag basically contains the name (<servlet-name>) that will be used throughout the xml document to reference our servlet and its linked class (<servlet-class>) . The tag <servlet-mapping> is responsible for telling the server, to what document name in the URL our application should respond to. Note, that the correct order of these tags has to be retained to form a valid web.xml descriptor. If we followed all steps correctly, we should now be able to fire up Tomcat again.
Finally, we need to tell Tomcat where to find our Hello World project:
# for Ubuntu, as root: ln -s $(pwd) /usr/share/tomcat5/webapps/ ls -lh /usr/share/tomcat5/webapps/ # check to make sure the link was created /etc/init.d/tomcat5 restart
