tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <>
Subject Re: programmatically setting MIME mappings for static-only site
Date Mon, 22 Apr 2019 13:43:04 GMT
Hash: SHA256


On 4/19/19 19:05, Konstantin Kolinko wrote:
> пт, 19 апр. 2019 г. в 19:29, Garret Wilson
> <>:>
>> I'm wanting to embed Tomcat to only serve static files (for the
>> moment). That is, no JSP, etc. I also want to have the welcome
>> files completely customizable.
>> So instead of calling `tomcat.addWebapp()`, I go the completely 
>> programmable route and call `tomcat.addContext()`, 
>> `context.createWrapper()`, etc. This makes my bypass the 
>> `DefaultWebXmlListener`, which would have called 
>> `initWebappDefaults(Context ctx)` to set up the welcome files and
>> such myself.
>> But `initWebappDefaults()` also sets up the default MIME
>> mappings. And `Tomcat.DEFAULT_MIME_MAPPINGS` is private.
> Also note that the value is not synch'ed with the default list in 
> conf/web.xml. E.g  the following entries from the top of the
> default list are missing
> <mime-mapping> <extension>123</extension> 
> <mime-type>application/vnd.lotus-1-2-3</mime-type> </mime-mapping>

> <mime-mapping> <extension>7z</extension> 
> <mime-type>application/x-7z-compressed</mime-type> </mime-mapping>
> (and many others)
> Some years ago the list in web.xml was synch'ed to the similar file
> in Apache HTTPD, but the list used by embedded Tomcat was not
> updated.
> Previous discussion:
>> So the situation seems to be that Tomcat forces me to choose
>> between creating a full-fledged JSP server, or setting up all the
>> MIME types with some list of my own. Maybe it would be good for
>> me to have my own list eventually, but for now this seems like an
>> artificial choice forced upon me.
>> Part of the problem seems to be that the (ancient?) code has the
>> MIME mappings as a string array!! Heaven knows we don't want to
>> expose that. It should really be turned into a read-only map and
>> then exposed so we can use it.
> An array is a bad API, but generally it is faster to create an
> array. All the time used to create a map goes to waste if it is
> only accessed sequentially, like it is done here.
> The only place where the values are used is
> for (int i = 0; i < DEFAULT_MIME_MAPPINGS.length;) { 
> ctx.addMimeMapping(DEFAULT_MIME_MAPPINGS[i++], 
> There are some people who ask for options to make Tomcat to start
> up faster (e.g. in a "serverless" environment when you start a
> process on demand and pay for execution time). Not being careful
> here may negatively affect the startup time. The current 
> 'DEFAULT_MIME_MAPPINGS' field is a static one and is always
> created when the class is loaded, regardless of whether it will be
> used or not.
>> Then of course I see the comment:
>>> TODO: would a properties resource be better ? Or just parsing
>> /etc/mime.types ?
>> To answer part of that question, we can't just parse
>> `/etc/mime.types` because the embedded server might not even have
>> an `/etc/mime.types` file. This should definitely be put into a
>> properties resource, I would think.
> For reference, 
>> So I've probably answered my own question; this is an old TODO
>> that needs to be done, I suppose?

Maybe it makes sense to do the following:

1. Create a separate, simple file. No XML, etc.

2. Create a new method for loading the default mime types. It can be
called if/when necessary but is not always called.

3. Hook something into the build process for re-generating the
mime-mappings for the default conf/web.xml file so everything is
always in sync.

- -chris
Comment: Using GnuPG with Thunderbird -


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

View raw message