12.2 The contentType and pageEncoding Attributes

The contentType attribute sets the Content-Type response header, indicating the MIME type of the document being sent to the client. For more information on MIME types, see Table 7.1 (Common MIME Types) in Section 7.2 (Understanding HTTP 1.1 Response Headers).

Use of the contentType attribute takes one of the following two forms.

 <%@ page contentType="  MIME-Type  " %> <%@ page contentType="  MIME-Type  ; charset=  Character-Set  " %> 

For example, the directive

 <%@ page contentType="application/vnd.ms-excel" %> 

has the same basic effect as the scriptlet

 <% response.setContentType("application/vnd.ms-excel"); %> 

The first difference between the two forms is that response.setContentType uses explicit Java code (an approach some developers try to avoid), whereas the page directive uses only JSP syntax. The second difference is that directives are parsed specially; they don't directly become _jspService code at the location at which they appear. This means that response.setContentType can be invoked conditionally whereas the page directive cannot be. Setting the content type conditionally is useful when the same content can be displayed in different formsfor an example, see the next section (Conditionally Generating Excel Spreadsheets).

Unlike regular servlets, for which the default MIME type is text/plain , the default for JSP pages is text/html (with a default character set of ISO-8859-1 ). Thus, JSP pages that output HTML in a Latin character set need not use contentType at all. If you want to change both the content type and the character set, you can do the following.

 <%@ page contentType="  someMimeType  ; charset=  someCharacterSet  " %> 

However, if you only want to change the character set, it is simpler to use the pageEncoding attribute. For example, Japanese JSP pages might use the following.

 <%@ page pageEncoding="Shift_JIS" %> 

Generating Excel Spreadsheets

Listing 12.2 shows a JSP page that uses the contentType attribute and tab-separated data to generate Excel output. Note that the page directive and comment are at the bottom so that the carriage returns at the ends of the lines don't show up in the Excel document. (Note: JSP does not ignore white spaceJSP usually generates HTML in which most white space is ignored by the browser, but JSP itself maintains the white space and sends it to the client.) Figure 12-4 shows the result in Internet Explorer on a system that has Microsoft Office installed.

Listing 12.2 Excel.jsp
 First   Last    Email Address Marty   Hall    hall@coreservlets.com Larry   Brown   brown@coreservlets.com Steve   Balmer  balmer@ibm.com Scott   McNealy mcnealy@microsoft.com <%@ page contentType="application/vnd.ms-excel" %> <%-- There are tabs, not spaces, between columns. --%> 
Figure 12-4. Excel document ( Excel.jsp ) in Internet Explorer.


Core Servlets and JavaServer Pages (Vol. 1.Core Technologies)
Core Servlets and Javaserver Pages: Core Technologies, Vol. 1 (2nd Edition)
ISBN: 0130092290
EAN: 2147483647
Year: 2002
Pages: 194

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net