< Prev
Next >



JSP contentType Attribute of Page Directive



The contentType attribute of JSP's page directive is used to set the MIME type of a document in the response header that is being sent to the client. MIME stands for Multipurpose Internet Mail Extensions. MIME type is a way to tell the browser about the specific format of the file in which it should be opened.



Some important MIME type values



MIME Type Values Description
text/html HTML Document
text/plain Plain Text Document
text/xml XML Document
text/css Casacading Style Sheets
image/png PNG Image
image/gif GIF Image
image/jpeg JPEG Image
mpeg/video MPEG Video Clip.
audio/midi MIDI Audio File.
application/pdf PDF File
application/word MS-WORD File





Setting the contentType with a MIME type value of "text/plain"


Setting the contentType attribute of page directive of JSP to the value "text/plain" tells the browser to open the web page as a plain text document. Let's see how it is done.

ContentType.jsp
<html>

<head>
<title>The contentType Attribute</title>
</head>


<body>

Setting the contentType to text/plain
<br/>

<%@ page contentType = "text/plain" %>
<b>This is going to be displayed as a plain text only to the user.</b>

</body>

</html>

executing the above mentioned JSP web page forces the browser to display the page in a plain text format.






Setting the contentType with a MIME type value of "text/xml"


Setting the contentType attribute of page directive of JSP to the value "text/xml" tells the browser to open content of the web page as an xml document. Let's see how it is done.

ContentType2.jsp
<html>

<head>
<title>The contentType Attribute</title>
</head>


<body>

Setting contentType to text/xml.
<br/>

<%@ page contentType = "text/xml" %>
<b>The text on this page going to be displayed only in the form of text/xml to the user.</b>

</body>

</html>

executing the above mentioned JSP web page leads the browser to display the page in its XML form.






Setting the contentType with a MIME type value of "text/html"


Setting the contentType attribute of page directive of JSP to the value "text/html" tells the browser to open this web page as an HTML document. Let's see how it is done.

ContentType3.jsp
<html>

<head>
<title>The contentType Attribute</title>
</head>


<body>

Setting the contentType to text/plain
<br/>

<%@ page contentType = "text/html" %>
<b>The text on this page is displayed only in the form of html to user.</b>

</body>

</html>

executing the above mentioned JSP web page leads the browser to display the page in its default HTML document format.





Note -


the default MIME type is "text/html".




Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter



< Prev
Next >
< JSP page Directive
JSP isThreadSafe Attribute >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
C++ and Python

Ad2