Web.xml.MimeMapping

<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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值