Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
christriddle@googlemail.com - 30 Aug 2007 10:49 GMT
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...
Lew - 30 Aug 2007 14:30 GMT
> 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%>
[quoted text clipped - 6 lines]
> 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:out value="${imageHTML}"/></a>

Signature

Lew

Daniel Pitts - 30 Aug 2007 16:29 GMT
On Aug 30, 2:49 am, "christrid...@googlemail.com"
<christrid...@googlemail.com> wrote:
> Hello!
> I should probably know this but I have some HTML in a variable (loaded
[quoted text clipped - 13 lines]
>
> Ta...

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

<% getOut().println(imageHTML) %>
Manish Pandit - 30 Aug 2007 17:58 GMT
On Aug 30, 2:49 am, "christrid...@googlemail.com"
<christrid...@googlemail.com> wrote:
> Hello!
> I should probably know this but I have some HTML in a variable (loaded
[quoted text clipped - 13 lines]
>
> Ta...

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

-cheers,
Manish
christriddle@googlemail.com - 31 Aug 2007 09:28 GMT
Cheers guys...


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.