Sorry maxpower1, but I disagree ... while it is valid to define your context in server.xml (which incidentally is under TOMCAT_HOME/conf), it is not necessary.
The layout of the directory structure (this is discussed in the Tomcat documentation -
is as such (for the default or ROOT context) :
classes must be put in TOMCAT_HOME/webapps/ROOT/WEB-INF/classes
In TOMCAT_HOME/conf/web.xml there is something called the "invoker" servlet which is a mapping tool, and allows you to access a servlet in WEB-INF/classes (as above) by hitting this URL pattern :
Code:
[URL unfurl="true"]http://localhost:8080/servlet/MyServlet[/URL]
If you have your own context, then the dir structure is like this :
TOMCAT_HOME/webapps/myContext
TOMCAT_HOME/webapps/myContext/WEB-INF
TOMCAT_HOME/webapps/myContext/WEB-INF/classes
and you put your classes in TOMCAT_HOME/webapps/myContext/WEB-INF/classes.
(as long as they are not in a package (if they are its a bit different - ie if your class was called com.acme.MyServlet, then it would reside in TOMCAT_HOME/webapps/myContext/WEB-INF/classes/com/acme).
------------------------
If this is all too much (contexts and so on), just stick to putting everything under the default context ROOT. So, under TOMCAT_HOME/webapps/ROOT/WEB-INF/classes, put this compiled class (obviously compile it first) -
Code:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class TestServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String param1 = request.getParameter("param1");
String param2 = request.getParameter("param2");
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("A test servlet !<br>Here is some HTTP header info ... <hr>");
Enumeration enum = request.getHeaderNames();
while (enum.hasMoreElements()) {
String ne = (String)enum.nextElement();
out.println(ne+" : " +request.getHeader(ne));
}
out.println("<hr>And here were some parameters passed on the URL : <br><b>" +param1 +" " +param2 +"</b>");
out.println("</body></html>");
}
}
You can hit this servlet by doing this URL :
Code:
[URL unfurl="true"]http://localhost:8080/servlet/TestServlet?param1=Hello¶m2=World[/URL]
Hope this gets you started ...