Tuesday, July 8, 2008

JSP Content Type - MIME Type

Some JSP pages are designed so they can deliver content using different content types (and character sets) depending on request time input. These pages may be organized as custom actions or scriptlets that determine the response content type and provide glue into other code actually generating the content of the response.

The initial content type for the response (including the character set) is determined as shown in the "Output Content Type" column in Table JSP.3-1. In all cases, the container must call response.setContentType() with the initial content type before processing the page.

The content type (and character set) can then be changed dynamically by calling setContentType() or setLocale() on the response object. The most recent call takes precedence. Changing the content type can be done up until the point where the response is committed. Data is sent to the response stream on buffer flushes for buffered pages, or on encountering the first content (beware of whitespace) on unbuffered pages. Whitespace is notoriously tricky for JSP Pages in JSP syntax, but much more manageable for JSP Documents in XML syntax.

Default JSP Type :


language CDATA "java"
extends %ClassName; #IMPLIED
contentType %Content; text/xml; UTF-8
import CDATA #IMPLIED
session %Bool; true
buffer CDATA 8kb
autoFlush %Bool; true
isThreadSafe %Bool; true
info CDATA #IMPLIED
errorPage %URL; #IMPLIED
isErrorPage %Bool; false
>


1. is "text/html" for JSP Pages in standard syntax, or "text/xml" for JSP Documents in XML syntax.

2. is "ISO-8859-1" for JSP Pages in standard syntax, or "UTF-8" for JSP Documents in XML syntax.

3. is "ISO-8859-1" for JSP Pages in standard syntax, or "UTF-8" or "UTF-16" for JSP Documents in XML syntax (depending on the type detected as per the rules in the XML specification). Note that, in the case of include directives, the default input encoding is derived from the initial page, not from any of the included pages.


How set the Content Type in JSP :

Using the setContentType() of the response we can change the MIME type and encoding format.

EX :
response.setContentType("text/html");

To check Pre-defined Content Type : click Here

No comments: