简体   繁体   中英

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/>

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.

<%@ 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM