A useful post and I hope I can add to it.
I have three hello world servlets sitting in different directories.
HelloWorldExamplebb.class sitting in
/public_html/WEB-INF/classes/
HelloWorldExample.class sitting in
/public_html/WEB-INF/classes/helloworld/
and test.class sitting in
/public_html/WEB-INF/classes/com/test/
My web.xml file which runs all three is
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<servlet>
<servlet-name>newServlet</servlet-name>
<servlet-class>helloworld.HelloWorldExample</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>newServlet</servlet-name>
<url-pattern>/servlet/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>com.test.test</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>/servlet/*</url-pattern>
</servlet-mapping>
</web-app>
The
key in setting up the web.xml file is to declare your java class with the appropriate
package.
You
don't have to worry about packages if you are just placing your java class in the /public_html/WEB-INF/classes directory see code below
no package declared
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* $Id: HelloWorldExample.java 267129 2004-03-18 16:40:35Z jfarcand $
*
*/
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* The simplest possible servlet.
*
* @author James Duncan Davidson
*/
public class HelloWorldExamplebb extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
//ResourceBundle rb =
// ResourceBundle.getBundle("LocalStrings",request.getLocale());
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
String title = "Hello World top level dir"; //rb.getString("helloworld.title");
out.println("<title>" + title + "</title>");
out.println("</head>");
out.println("<body bgcolor=\"white\">");
// note that all links are created to be relative. this
// ensures that we can move the web application that this
// servlet belongs to to a different place in the url
// tree and not have any harmful side effects.
// XXX
// making these absolute till we work out the
// addition of a PathInfo issue
out.println("<a href=\"../helloworld.html\">");
out.println("<img src=\"../images/code.gif\" height=24 " +
"width=24 align=right border=0 alt=\"view code\"></a>");
out.println("<a href=\"../index.html\">");
out.println("<img src=\"../images/return.gif\" height=24 " +
"width=24 align=right border=0 alt=\"return\"></a>");
out.println("<h1>" + title + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
When I place my Hello World test in the /public_html/WEB-INF/classes/helloworld/ directory, I
must declare HelloWorldExample.java which compiles to HelloWorldExample.class to be a part of package helloworld;
note the package and how web.xml refrences it
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* $Id: HelloWorldExample.java 267129 2004-03-18 16:40:35Z jfarcand $
*
*/
package helloworld;
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* The simplest possible servlet.
*
* @author James Duncan Davidson
*/
public class HelloWorldExample extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
//ResourceBundle rb =
// ResourceBundle.getBundle("LocalStrings",request.getLocale());
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
String title = "Hello World mod nested deep"; //rb.getString("helloworld.title");
out.println("<title>" + title + "</title>");
out.println("</head>");
out.println("<body bgcolor=\"white\">");
// note that all links are created to be relative. this
// ensures that we can move the web application that this
// servlet belongs to to a different place in the url
// tree and not have any harmful side effects.
// XXX
// making these absolute till we work out the
// addition of a PathInfo issue
out.println("<a href=\"../helloworld.html\">");
out.println("<img src=\"../images/code.gif\" height=24 " +
"width=24 align=right border=0 alt=\"view code\"></a>");
out.println("<a href=\"../index.html\">");
out.println("<img src=\"../images/return.gif\" height=24 " +
"width=24 align=right border=0 alt=\"return\"></a>");
out.println("<h1>" + title + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
Similarly test.java which sits in /public_html/WEB-INF/classes/com/test/ must declare in test.java that it is part of package com.test; (Thanks to Hatcc3 for providing this example) Again
note the package and how web.xml references it.
package com.test;
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
public class test extends HttpServlet {
public void doGet (HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException
{
PrintWriter out = res.getWriter();
out.println("Hello, world!");
out.close();
}
}
Then in the web.xml
see above file use the dot notation relative to /public_html/WEB-INF/classes to access
the servlet functionality you desire.
This should provide enough of a detailed example to allow you to experiment and add new packages to you servlet class tree.
Happy hacking

PS A
http://www.caucho.com/resin-3.0/config/webapp.xtp#servlet-mapping useful link on web.xml files.