Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert html code dynamically using JSTL?

Tags:

html

jsp

jstl

I was learning [JSTL] and stuck at a point where I've a String that contains HTML code.

Following is the code I am writing and the out I'm achieving.

<c:set var = "str" scope = "session" value = "<div><b>Hello</b></div>"/>  
<c:out value="${str}"></c:out>

Output is:

<div><b>Hello</b></div>

But, actual output I want is: Hello

I am achieving this output when using java code in my [JSP]. As it is not a good practice to write java code in JSP page therefore JSTL is required.

Update: I was exploring JSTL and found something called escapeXml. This attribute can be used with <c:out> to use HTML/XML content.

Here is the example for that:

<c:set var = "str" scope = "session" value = "<div><b>Hello</b></div>"/>
<c:out value="${str}" escapeXml="false"/>

With this, I was able to achieve the desired result.

like image 531
Thakur Sahab Avatar asked Dec 01 '25 05:12

Thakur Sahab


1 Answers

First download/implement the libary Taglibs

Then a simple import at the start of the *.jsp page.

  • <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

Then you are able to impirt the file

  • <c:import url="./mypage.jsp" var="cPage" charEncoding="UTF-8"/>

or a absulute link, maybe a hostet html file:

  • <c:import url="www.w3schools.org/index.html" var="cPage" charEncoding="UTF-8"/>

'cPage' is the string which is returned back

Afterwards get used of the var

  • <c:out value="${cPage}" escapeXml="false"/>
like image 133
ru4ert Avatar answered Dec 03 '25 18:12

ru4ert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!