Share on Google+Share on Google+

The "contentType" Attribute of Page Directive

Advertisement
In this Section, we will discuss about the "contentType" attribute of page directive with an example.

The "contentType" Attribute of Page Directive

In this Section, we will discuss about the "contentType" attribute of page directive with an example. The "contentType" attribute is used for setting the 'type of content' for sending 'response'. You can use any MIME type or character set that are valid for the JSP container. The default MIME type is "text/html", and the default character set is "ISO-8859-1".

The Syntax for this attribute is :

<%@page contentType="contentType="mimeType [;charset=characterSet ]" %>

Ex. :    <%@page contentType="text/html;charset=ISO-8859-1" %>

Example :  

In this example, we are taking 2 JSP pages to simply outputting a line in a table. We are using "text/html" contentType for "contenttypejsp1.jsp"  &  for "contenttypejsp2.jsp" we are using "text/xml". You will see that there is a major difference between both JSP's output, this is due to their "contentType" page directive attribute.

contenttypejsp1.jsp

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

<html>

<head><title>Example of contextType attribute of page directive in JSP.</title></head>

<body>

<table border="1" cellspacing="0" cellpadding="0" bgcolor="ffff00">

<tr>

<td><strong>This is the html page.</strong></td>

</tr>

</table>

</body>

</html>

OUTPUT

contenttypejsp2.jsp

<%@page contentType="text/xml" %>

<html>

<head><title>Example of contextType attribute of page directive in JSP.</title></head>

<body>

<table border="1" cellspacing="0" cellpadding="0" bgcolor="ffff00">

<tr>

<td><strong>This is the html page.</strong></td>

</tr>

</table>

</body>

</html>

OUTPUT :

Download Source code

Advertisements

Advertisement

Posted on: June 15, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.