The "contentType" Attribute of Page Directive


 

The "contentType" Attribute of Page Directive

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

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

Ads