M Sach
M Sach

Reputation: 34424

How to make the browser interpret the text as html content instead of plain text?

I have a Java String mailContent as

<p><span style="background-color: #ff6600; font-family: arial black,avant garde; font-size: large;">Testing Correct Formatting</span></p>

which i encode with methods formatString and formatParagraph in jsp before sending to browser. Jsp code is below

<td valign="center" >
<%=StringFormat.formatParagraph(StringFormat.formatString(mailContent,StringFormat.ALL))%>
 </td>

Now with with above code snippet i see it simply printed as plain text(as the value of mailContent) not as text with html tags. Due to which, i don't see the formmatted HTML content like font size, background color, font family etc. Thing is if simply use below code, it shows formatted html content because i am not encoding the text now.

 <td valign="center" >
 <%=mailContent%>
 </td>

now how i can make the browser interpret the encoded content as html tags as i dont want to remove the encoding?

For information , on browser side , when i do view source i see below as it has been encoded

 &lt;p&gt;&lt;span style=&quot;background-color: #ff6600; font-family: arial black,avant garde; font-size: large;&quot;&gt;Testing Correct Formatting&lt;/span&gt;&lt;/p&gt;<br/><br/>

Upvotes: 0

Views: 3467

Answers (2)

Kapil Gupta
Kapil Gupta

Reputation: 1

<%@ page contentType="text/html; charset=iso-8859-1" language="java"  %>



<%
request.setAttribute("Att","<p><span style=\"background-color: #ff6600; font-family: arial black,avant garde; font-size: large;\">Testing Correct Formatting</span></p>");
%>
<%= request.getAttribute("Att")%>

In place of value in setAttrbiute you can pass your string object.

Upvotes: 0

AlexR
AlexR

Reputation: 115328

The browser makes its decision about the content type using HTTP header named Content-Type. You should send content type value text/html. It seems that you are sending now text/plain, so browser treats the content as a plain text.

Please take a look here for more details.

Upvotes: 1

Related Questions