tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mark Biciunas" <>
Subject setting up a root servlet / getting images to appear in Tomcat 4.1.24
Date Wed, 09 Jul 2003 19:19:48 GMT
This email formally presents what I have learned over the past couple days
about setting up a servlet to be accessed as root (ie:
instead of without loosing access to images and
other mime types.  It is based primarily on advice received from Bill Barker
and Stefan Radzom as well as the hints and suggestions of many others.  As
you are looking at this solution, please bear in mind that I am not an
expert in Tomcat configuration and there is likely a lot of things I have
missed.  I welcome any additional advice / corrections people have to offer.

Setting up a servlet to be accessed without a path (ie:
is fairly easy if you pay attention to a couple of extra steps.  First this
is to deploy your application in the webapps directory as usual (ie:
webapps/myservlet).  Next, update conf/servlet.xml so that you have a root
context that looks like:

    <Context path="" docBase="myservlet" debug="0"/>

This will tell tomcat to serve ALL incoming requests to your servlet,
including requests for images, etc.  If your servlet is not set up to handle
mime types, then your images, etc. will seem to disappear.  To make sure the
images, etc. are handled correctly, you need to map them out in your
WEB-INF/web.xml as follows:





What is happining here in the first two sections is that you have identified
the servlet class and mapping for your servlet.  This is more or less the
same as you would do for any servlet.  If you do nothing more than this,
your servlet will (should) work, but you will not see any images since
picture.gif would match to a url-pattern of "/" and get sent to your servlet
for processing.

In the second two sections, we are telling Tomcat that anything that matches
a pattern of *.gif or *.jpg should be sent to the default servlet.  Now any
requests that match *.gif or *.jpg will be handled correctly.  If you need
to support more mime types, simply create more servlet mappings.

Where did the default servlet come from?  It is already configured in
conf/web.xml.  Remember that conf/web.xml is automatically read before your
WEB-INF/web.xml  so it can do it's thing without you having to worry about
it at all.  The trick is to use url-patterns to send requests back to the
default servlet so that they don't end up in your servlet.

The above solution will work great as long as you don't have to many mime
types to deal with.  If you need to handle lots of diferent types of
requests, then Bill Barker presented the following alternate solution which
involves changing your servlet code to redirect requests:

     URL file = getServletContext().getResource(request.getPathInfo());
     if( file != null ) { // physical resource exists
       RequestDispatcher rd =
       rd.forward(request, response);
    // Your code here.

I have not tried this solution so I do not know to much about it.  It seems
staightforward enough though, so I would expect it to work great.

Mark Biciunas

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message