Newbie problem! Using <%=%> to output HTML in JSP

C

christriddle

Hello!
I should probably know this but I have some HTML in a variable (loaded
from a database), and want to output it to a JSP file (you know what I
mean). I'm using the following code:
<% String ImageHTML = "<a blah blah blah>Link</a>";
....
<%=ImageHTML%>

But when I load it up it literally prints the HTML and in the source
it looks like:
&lt;a blah blah blah&gt; ....

How do I output it so it acts like HTML rather than a string of text?
I googled but something like this is difficult to find since I don't
quite know specific keywords that aren't too general.

Ta...
 
L

Lew

Hello!
I should probably know this but I have some HTML in a variable (loaded
from a database), and want to output it to a JSP file (you know what I
mean). I'm using the following code:
<% String ImageHTML = "<a blah blah blah>Link</a>";

The Sun-recommended and almost universally applied convention is for variable
names to begin with a lower-case letter.
....
<%=ImageHTML%>

But when I load it up it literally prints the HTML and in the source
it looks like:
&lt;a blah blah blah&gt; ....

How do I output it so it acts like HTML rather than a string of text?
I googled but something like this is difficult to find since I don't
quite know specific keywords that aren't too general.

<a href="<%= imageHTML %>"><%= imageHTML %></a>

Best practices prohibit the use of scriptlet in JSP. Use tag libs like JSTL
instead:

<a href="${imageHTML}"><c:eek:ut value="${imageHTML}"/></a>
 
D

Daniel Pitts

Hello!
I should probably know this but I have some HTML in a variable (loaded
from a database), and want to output it to a JSP file (you know what I
mean). I'm using the following code:
<% String ImageHTML = "<a blah blah blah>Link</a>";
...
<%=ImageHTML%>

But when I load it up it literally prints the HTML and in the source
it looks like:
&lt;a blah blah blah&gt; ....

How do I output it so it acts like HTML rather than a string of text?
I googled but something like this is difficult to find since I don't
quite know specific keywords that aren't too general.

Ta...

This is a guess, I haven't tried it...

<% getOut().println(imageHTML) %>
 
M

Manish Pandit

Hello!
I should probably know this but I have some HTML in a variable (loaded
from a database), and want to output it to a JSP file (you know what I
mean). I'm using the following code:
<% String ImageHTML = "<a blah blah blah>Link</a>";
...
<%=ImageHTML%>

But when I load it up it literally prints the HTML and in the source
it looks like:
&lt;a blah blah blah&gt; ....

How do I output it so it acts like HTML rather than a string of text?
I googled but something like this is difficult to find since I don't
quite know specific keywords that aren't too general.

Ta...

You can use escapeXml=false in the c:eek:ut tag that Lew pointed out, to
ensure the HTML does not end up being escaped as &lt; etc.

-cheers,
Manish
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top