|
What appears below are my personal notes I wish were part of my long-term memory but don't always seem to fit. I strive for accuracy and clarity and appreciate feedback. If applying any of this information anywhere, confirm for youself the correctness of your work as what you see below might very well be, albeit unintentionally, incorrect or misleading. These notes are here as an easy reference for myself.
Information worthy of a more formal presentation will appear elsewhere than this "Scratch" area. - ksb
Here's some notes on Apache Tomcat
Table of Contents | References |
I nearly always want to start with a simple 'Hello, World' example when learning a new development technology. I've barely gotten by so far in dealing with Servelet containers like Tomcat so when I wanted to dig into how to write a web app, I'm surprised I couldn't find a simple example elsewhere on the web. There is a 'Hello, World' with Tomcat but that expects and goes into details on cvs, ant, jsp which seemed like overkill to me. Below is what I derived from that Apache example, hopefully a clearer simpler one:
Create the follow dir structure:
HW HW/src HW/src/hw HW/web HW/web/WEB-INFHere's how:
mkdir -p HW/src/hw HW/web/WEB-INF
Now, create the following 3 files:
HW/index.html HW/src/hw/HW.java HW/web/WEB-INF/web.xmlThe top-level index.html file:
<html> <head> <title>Hello, 'Hello, World'</title> </head> <body> The servelet your looking for is <a href="./hw">here.</a> </body> </html>The Java code in HW.java
/* Apache Tomcat "hello, world" web application. */ package hw; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * The simplest possible servlet. */ public final class HW extends HttpServlet { /** * Respond to a GET request * * @param request The request * @param response Our response */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter writer = response.getWriter(); String greet = "<html>\n" + " <head>\n" + " <title>Hello, world.</title>\n" + " </head>\n" + " <body>\n" + "Hello, world.\n" + " </body>\n" + "</html>"; writer.println(greet); } }The web.xml file:
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <display-name>Hello, World Application</display-name> <description> This is a simple web application with a source code organization based on the recommendations of the Application Developer's Guide. </description> <servlet> <servlet-name>HelloServlet</servlet-name> <servlet-class>hw.HW</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloServlet</servlet-name> <url-pattern>/hw</url-pattern> </servlet-mapping> </web-app>
Finally, here is a simple build script to compile and assemble it all into a .war file:
#!/bin/sh CH=/path/to/apache/tomcat/catalina/home CP=$CH/lib/servlet-api.jar WD=work WAR=hw.war \rm -rf $WAR $WD if [ "$1" = "clean" ]; then exit fi mkdir -p $WD/WEB-INF/classes javac -d $WD/WEB-INF/classes -cp $CP ./src/hw/HW.java || exit cp ./web/WEB-INF/web.xml $WD/WEB-INF cp ./index.html $WD cd $WD jar -cf ../hw.war *
Now you should have a hw.war file which you can deploy into your $CATALINA_HOME/webapp dir. Then you can hit the /hw dir which presents a page (the above index.html file) pointing you to the url for the servlet.