Advertisement



< Prev
Next >



JSP contentType Attribute



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 a client. MIME stands for Multipurpose Internet Mail Extensions. MIME type is a way to tell a browser about a specific format of a 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 to the value "text/plain" tells a browser to open the JSP web page as a plain text document. Let's see how it is done.

ContentType.jsp
<!–– Example of contentType attribute of page directive of 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.




Advertisement




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


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

ContentType2.jsp
<!–– Example of contentType attribute of page directive of 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 to the value "text/html" tells the browser to open this JSP web page as an HTML document. Let's see how it is done.

ContentType3.jsp
<!–– Example of contentType attribute of page directive of 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 -




< Prev
Next >
< JSP page Directive
JSP isThreadSafe Attribute >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement