----- Original Message -----
From: "[Tom Hunter]" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
> Does anyone how or where Tomcat finds a servlet?
This question requires a rather elaborate answer, but I am trying to jolt
down some points which I know of.
Let us suppose that the servlet container has references of a few objects
which are subclasses of HttpServlet(how the container instantiated these
classes is given in the later part of the mail). Each of these object
represents one servlet. (I am assuming that there are no multiple instances
of a servlet class to keep things simple, although they might exist under
certain situations). The servlet container keeps the mapping of the
servlets[1] to the instances of the servlet classes in the memory. This
mapping table is created after parsing the web.xml.
When a HTTP request comes to the servlet engine, it does the following
- Assigns a thread to process this request
- Assigns/Creates two Objects. One object encapsulates the request details
and the other Object encapsulates the sending of the response back to the
client.
- The servlet container parses the request to find the resource being
requested.
- The container tries to find out which servlet this request is referring to
by applying the rules for servlet mapping [1]
- If a servlet is found to be matching the requested resource, the service
method of that servlet is invoked by the container.
- If no servlet is found matching, the container sends back an appropriate
Http error code to the client (404 in this case)
As for how the servlet instances created by the servlet container...
The container parses the web.xml file to find out the name of class which is
intended to be a servlet. The path to which this servlet maps is also
specified in the web.xml (optional).
The containers create an instance of a custom classloader for each context
to load the classes . This classloader search for the classfile with the
name specified in the web.xml in the following manner (may vary a little bit
from container to container).
i) The class file is searched on the classpath by delegating the
responsibility to the system classloader.
ii) If not found by the system classloader, the servlet container tries to
find the class file in jar files which are there in the lib directory of the
container generally referred to as "Container library files" [2].
iii) If not found, then the container tries to find the files in the
WEB-INF/classes directory. [3]
iv) If not found till now, then the container tries to find the file in all
the jars present in the WEB-INF/lib directory [3]
v)The container generally instantiates an instance of the class using the
method Class.newInstance(). The service method is invoked on this instance
when a request for a resource is recieved which maps to this servlet.
If you want to look at the how classloaders and classloading is managed in
Tomcat, you can look at the document available for tomcat 4.0 from the link
http://jakarta.apache.org/cvsweb/index.cgi/~checkout~/jakarta-tomcat-4.0/cat
alina/docs/dev/classloaders.html?rev=1.6&content-type=text/html
I hope all this helps to an extent.
Regds,
Gokul
[1] Section 11.1 of the servlet specification 2.3 (PFD) available from
http://java.sun.com/aboutJava/communityprocess/first/jsr053/index.html
[2] Section 9.6.1 of the servlet specification 2.3 (PFD)
[3] The directory structure is defined in section 9.4 of the the servlet
specification 2.3 (PFD)
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html