Oracle JavaServer Pages Developer's Guide and Reference Release 8.1.7 Part Number A83726-01 |
Library | Service | Contents | Index

## Content Type Settings in the page Directive

You can use the `page` directive `contentType` parameter to set the MIME type and to optionally set the character encoding for a JSP page. The MIME type applies to the HTTP response at runtime. The character encoding, if set, applies to both the page text during translation and the HTTP response at runtime.

Use the following syntax for the `page` directive:

    <%@ page ... contentType="TYPE; charset=character_set" ... %>

or, to set the MIME type while using the default character set:

    <%@ page ... contentType="TYPE" ... %>

`TYPE` is an IANA (Internet Assigned Numbers Authority) MIME type and `character_set` is an IANA character set. (When specifying a character set, the space after the semi-colon is optional.)

For example:

    <%@ page language="java" contentType="text/html; charset=UTF-8" %>

or:

    <%@ page language="java" contentType="text/html" %>

The default MIME type is `text/html`. The IANA maintains a registry of MIME types at the following site:

    ftp://venera.isi.edu/in-notes/iana/assignments/media-types/media-types

The default character encoding is `ISO-8859-1` (also known as Latin-1). The IANA maintains a registry of character encodings at the following site (use the indicated "preferred MIME name" if one is listed):

    ftp://venera.isi.edu/in-notes/iana/assignments/character-sets

(There is no JSP requirement to use an IANA character set as long as you use a character set that Java and the Web browser support, but the IANA site lists the most common character sets. Using the preferred MIME names they document is recommended.)

The parameters of a `page` directive are static. If a page discovers during execution that a different setting is necessary for the response, it can do one of the following:

- Use the servlet response object API to set the content type during execution, as described in "Dynamic Content Type Settings".

- Forward the request to another JSP page or to a servlet.

  Notes: The `page` directive that sets `contentType` should appear as early as possible in the JSP page. A JSP page written in a character set other than `ISO-8859-1` must set the appropriate character set in a `page` directive. It cannot be set dynamically because the page has to be aware of the setting during translation. Dynamic settings are for runtime only. The JSP 1.1 specification assumes that a JSP page is written in the same character set that it will use to deliver its content. This document, for simplicity, assumes the typical case that the page text, request parameters, and response parameters all use the same encoding (although other scenarios are technically possible). Request parameter encoding is controlled by the browser, although Netscape browsers and Internet Explorer follow the setting you specify for the response parameters.

| Copyright © 1996-2000, Oracle Corporation. All Rights Reserved. |
Library | Service | Contents | Index