Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
11.2 The contentType Attribute
255
Figure 11 6 Result of Excel.jsp on system that has Excel installed.
A second way to format Excel content is to use a normal HTML table,
which recent versions of Excel can interpret properly as long as the page is
marked with the proper MIME type. This capability suggests a simple
method of returning either HTML or Excel content, depending on which
the user prefers: just use an HTML table and set the content type to
application/vnd.ms excel
only if the user requests the results in Excel.
Unfortunately, this approach brings to light a small deficiency in the
page
directive: attribute values cannot be computed at run time, nor can
page
directives be conditionally inserted as can template text. So, the following
attempt results in Excel content regardless of the result of the
checkUser
Request
method.
<% boolean usingExcel = checkUserRequest(request); %>
<% if (usingExcel) { %>
<%@ page contentType="application/vnd.ms excel" %>
<% } %>
Fortunately, there is a simple solution to the problem of conditionally set
ting the content type: just use scriptlets and the normal servlet approach of
response.setContentType
, as in the following snippet:
<%
String format = request.getParameter("format");
if ((format != null) && (format.equals("excel"))) {
response.setContentType("application/vnd.ms excel");
}
%>
Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved