Wrox Home  
Search P2P Archive for: Go

  Return to Index  

apache_tomcat thread: web xml problem with tomcat


Message #1 by "gary bushek" <viper_19901@y...> on Thu, 19 Dec 2002 03:50:02
question 1:  does anyone know why i cant run a servlet without a web.xml 
file. 

question 2:  when i use a web.xml file should i have to use a <servlet-
mapping> tag, several examples i have seen dont use it but my servlets 
dont work without it.

any help is appreciated,
  gary
Message #2 by "Ben Galbraith" <junk@g...> on Fri, 20 Dec 2002 06:00:15
Hi Gary!

I'm not sure which version of Tomcat you're using, but I'll assume it's
version 4.1.x.  Both of your questions seem related to the same issue: you
are unable to route requests to a servlet without setting up a web.xml file
(called a deployment descriptor).

Previous versions of Tomcat did allow for routing requests to servlets
without a web.xml file.  The syntax was:

  http://host[:port]/[webapp]/servlet/[your.servlet.ClassName]

However, at some point, this feature was removed, as it no longer functions
in Tomcat 4.1.  I'm not in the mood right now to go through all the
versions and see where it disappeared :-) -- but I'm sure plenty of folks
know when and why.  Perhaps its removal was security-related.

This is probably for the best, however, as this feature was *not* a
standard Servlet 2.3 feature, and could lead to confusion if you migrated
to another servlet container.  The Servlet 2.3 spec makes no provision for
URL-servlet mapping that isn't defined in web.xml.  It does however permit
containers to add their own implicit mapping features, such as the one
above, but as I stated, such mappings would indeed be non-standard.

In any event, you will indeed need to setup a servlet mapping.  The
simplest web.xml file to accomplish this purpose would be:

<!DOCTYPE web-app PUBLIC 
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">
<web-app>
  <servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>my.package.MyServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>*.myservlet</url-pattern>
  </servlet-mapping>
</web-app>

This would route all requests ending in ".myservlet" to a servlet deploying
in either the WEB-INF/lib or WEB-INF/classes directory called
my.package.MyServlet.

Hope this helps!

Ben

  Return to Index