I have searched high and low for an answer/tutorial on this with no joy. It must be too trivial a question.
I’ve installed Tomcat 7.0.27 and am messing around with the stock examples. The examples are stored in webapps/examples and server.xml says the docBase is webapps. The HTML index of the examples is accessed via http://localhost:8080/examples/servlets/. So far, so good.
On this page, hover over the “Execute” link for the “Hello World” servlet and the path expands to: http://localhost:8080/examples/servlets/servlet/HelloWorldExample, which executes successfully of course. But servlet is supposed to expand to WEB-INF/classes. So I would expect the on-disk path to the class to be webapps/examples/servlets/WEB-INF/classes/HelloWorldExample.class, which it is not. The actual path is webapps/examples/WEB-INF/classes/HelloWorldExample.class. The only thing that’s in the servlets directory is HTML and image files. Question #1: What’s up with that? How did examples/servlets/servlet get re-routed to examples/WEB-INF/classes?
Question #2: I drop a compiled class Foo.class into WEB-INF/classes and try to access it both through http://localhost:8080/examples/servlets/servlet/Foo and through http://localhost:8080/examples/servlet/Foo. Both produce 404 not found. Restarted Tomcat (shouldn’t have to, but what the hay) which didn’t help. What gives?
Doesn’t work like that. Servlets != CGI. There is no expansion.
Servlets are specifically mapped in the web.xml to their URL. Their package name, or placement within WEB-INF as a .class, in a .jar, or located some other place on the class path have no bearing on what URL they are mapped to.
Addena, in Servlet 3.0, they can be annotated instead of mapped in web.xml. That doesn’t mean necessarily that compiled classes can be “just dropped in”. That would be a container feature, I can’t say whether Tomcat would allow that or not.