Metawerx Java Hosting Small Logo

<mime-mapping>

This tag defines a mapping between a file extension and a MIME type.

The server-default web.xml file in <jakarta>/conf/web.xml contains a list of all the built-in mappings. This tag can be used to extend that list.

There are two required subelements as follows:

  • <extension> - file extension (no leading dot), examples: xls, pdf
  • <mime-type> - a mime type, eg: application/vnd.ms-excel or text/html

Enabling file downloads via the application/octet-stream MIME type

While most files will download correctly from your site, if you have created a file with an unknown extension, the browser may attempt to display the file using the text/plain MIME type, which will show in the browser as text.

To avoid this behaviour, a mime-mapping can be set for your file to force the browser to download the file. This appropriate MIME type is application/octet-stream. This is a generic type for binary data streams.

Here is an example for an imaginary file type of pqz:

<!-- Send PQZ files as application/octet-stream so the user is asked to download the file --> 
<mime-mapping> 
    <extension>pqz</extension>
    <mime-type>application/octet-stream</mime-type> 
</mime-mapping>

Enabling download of Microsoft Excel spreadsheets

When providing Microsoft Excel spreadsheets for download from your application, some browsers display the file as garbage on the browser unless an appropriate mime-mapping is set.

The desired action, is to prompt for the file to be saved, or to open it in Excel. Newer versions of Internet Explorer may also display the file directly in the browser using the embedded Excel Active-X control.

Adding the following mime-mapping element to web.xml sets the correct mime-type, allowing the browser to interpret the file correctly.

<!-- Set Excel mime-mapping so spreadsheets open properly instead of being sent as an octet/stream -->
<!-- If this is not done, the user may be prompted to save the file, or it may open as garbage text in the browser --> 
<mime-mapping> 
    <extension>xls</extension> 
    <mime-type>application/vnd.ms-excel</mime-type> 
</mime-mapping>

The MIME type used here is vendor-specific (in this case Microsoft), so it has a "vnd." prefix.

Enabling a Favourites Icon

In Internet Explorer, when the URL you are visiting is in your Favourites List, an icon appears next to the URL in the Address bar and next to the website name in the Favourites menu. To see an example, add this page to your Favourites, then refresh. Look at the address bar of the browser, the metawerx logo is displayed. In FireFox the icon is always displayed.

http://www.metawerx.net/images/screenshots/favourites_icon.png

This icon is called the Favourites Icon, and can be customised by creating a 16x16 or 32x32 Windows Icon file, with an extension of .ico, and uploading it to the root folder of your website as "favicon.ico". You can download other site's icons by going to http://website/favicon.ico. For example: http://wiki.metawerx.net/favicon.ico.

Note that you may need special graphics software to create or edit an icon file, as most graphics software does not support this format. One such piece of software is IrfanView. However - see below for how to use a PNG file instead.

The Favourites Icon has a file type of image/x-icon. This is not defined by default in the Tomcat configuration.

Add the following mime-mapping element to web.xml sets the correct mime-type, allowing the browser to interpret the file correctly.

<!-- Set Favourites Icon MIME-Type --> 
<mime-mapping>
    <extension>ico</extension>
    <mime-type>image/x-icon</mime-type>
</mime-mapping>

On modern browsers, a newer method for adding a favourites icon has been added. This method now takes preference over the default favicon.ico file. Therefore, for best results on your web pages, include the following line in your HTML as well. For compatibility with older browsers however, we recommend using the favicon.ico method.

<link rel="icon" type="image/ico" href="/favicon.ico"/>
  • Using a link in this way also allows different icons for different pages of your site
  • You can also use a PNG image by using image/png above instead of image/ico.
  • href can point to any valid image location, as long as the image is the correct type (ico if you specified image/ico, png if you specified image/png etc..) and size (16x16 or 32x32)

Child Element(s)


More Information

navigation
metawerx specific
search
Share
tools
help

referring pages
Valid XHTML 1.0 Transitional

Share