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

## Dynamic Content Type Settings

For situations where the appropriate content type for the HTTP response is not known until runtime, you can set it dynamically in the JSP page. The standard `javax.servlet.ServletResponse` interface specifies the following method for this purpose:

    public void setContentType(java.lang.String contenttype)

The implicit `response` object of a JSP page is a `javax.servlet.http.HttpServletResponse` instance, where the `HttpServletResponse` interface extends the `ServletResponse` interface.

The `setContentType()` method input, like the `contentType` setting in a `page` directive, can include a MIME type only, or both a character set and a MIME type. For example:

    response.setContentType("text/html; charset=UTF-8");

or:

    response.setContentType("text/html");

As with a `page` directive, the default MIME type is `text/html` and the default character encoding is `ISO-8859-1`.

This method has no effect on interpreting the text of the JSP page during translation. If a particular character set is required during translation, that must be specified in a `page` directive, as described in "Content Type Settings in the page Directive".

Be aware of the following important usage notes:

- The JSP page cannot be unbuffered if you are using the `setContentType()` method. (It is buffered by default; do not set `buffer="none"` in a `page` directive.)

- The `setContentType()` call must appear early in the page, before any output to the browser or any `jsp:include` command (which flushes the JSP buffer to the browser).

- In servlet 2.2 environments, the `response` object has a `setLocale()` method that sets a default character set based on the specified locale, overriding any previous character set. For example, the following method call results in a character set of `Shift_JIS`:

      response.setLocale(new Locale("ja", "JP"));

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