You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Tim Willis <ti...@coderite.com> on 2000/04/14 18:23:43 UTC

Interpretation

This is my first time working with Tomcat and Servlets, and I've run into a question that might require a lengthy answer, but I haven't yet found a doc online that has given me a sufficient answer.   Can someone please either direct me to a good document, or post a simple answer to the following question?

How does apache know, when given this URL
http://www.....com/examples/servlet/HelloWorldExample

..to execute this class file?
D:\apache\jakarta-tomcat\webapps\examples\WEB-INF\classes\HelloWorldExample.class

Which, if any .conf file is this information found in?

Thanks,
=================================
Tim Willis
IS Technician
Healthcare Solutions Group
Affiliated Computer Services, Inc.
tim@coderite.com
=================================

Re: Interpretation

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
Juan Alvarez Ferrando wrote:

> You specify that at the web.xml file you have to put into
> /path/to/tomcat/webapps/your_app/WEB-INF
> Find a detailed description about the format at the Servlet 2.2
> specification you can get from http://developer.java.sun.com
>
> Juan Alvarez Ferrando
>

Juan's answer is correct (and the advice to read the spec is right on), but what
actually happens is a little more interesting, and deserves further explanation.

The question is -- how does the servlet container know what to do with a URL like this:

    http://localhost:8080/examples/servlet/HelloWorldExample

assuming you've just installed Tomcat?

* First, Tomcat sees that "/examples" matches the context path
  of the examples web application, so it starts there.

* Within the web.xml file for this app (but see more info below),
  it looks for a servlet mapping that matches the next part of the
  request.  In this case, it matches "/servlet", and executes a
  servlet built into Tomcat called the invoker.

* The invoker servlet sees "/HelloWorldExample" as the value
  returned by getPathInfo().  It strips off the slash at the beginning,
  and assumes the rest of the path is the name of a Java class
  containing your servlet.  That class is loaded, and your servlet
  is called for you.

"But wait a minute!", you say.  "There is no mapping for /servlet in the
examples/WEB-INF/web.xml file!".

That's true.  What happens is that Tomcat loads a set of default mappings from a file
named "conf/web.xml" under the Tomcat home directory.  In this file, you will see the
<servlet> definition for the invoker servlet (plus the JSP page compiler servlet and a
bunch of other stuff), plus a <servlet-mapping> entry that matches pattern "/servlet/*"
to this servlet.  Pretty cute, huh?

Craig McClanahan

PS:  Exercize for the reader:  some servlet engines map the path "/servlets" to this
kind of feature, rather than "/servlet".  Can I make Tomcat do that?  Sure, just add
the following entry in web.xml:

    <servlet-mapping>
        <servlet-name>invoker</servlet-mapping>
        <url-pattern>/servlets/*</url-pattern>
    </servlet-mapping>

and you can use the plural version as well.




Re: Interpretation

Posted by Juan Alvarez Ferrando <ja...@oviedo.syseca.es>.
You specify that at the web.xml file you have to put into
/path/to/tomcat/webapps/your_app/WEB-INF
Find a detailed description about the format at the Servlet 2.2
specification you can get from http://developer.java.sun.com

Juan Alvarez Ferrando

> Tim Willis wrote:
> 
> This is my first time working with Tomcat and Servlets, and I've run
> into a question that might require a lengthy answer, but I haven't yet
> found a doc online that has given me a sufficient answer.   Can
> someone please either direct me to a good document, or post a simple
> answer to the following question?
> 
> How does apache know, when given this URL
> http://www.....com/examples/servlet/HelloWorldExample
> 
> ..to execute this class file?
> D:\apache\jakarta-tomcat\webapps\examples\WEB-INF\classes\HelloWorldExample.class
> 
> Which, if any .conf file is this information found in?
> 
> Thanks,
> =================================
> Tim Willis
> IS Technician
> Healthcare Solutions Group
> Affiliated Computer Services, Inc.
> tim@coderite.com
> =================================