• Post Reply Bookmark Topic Watch Topic
  • New Topic

equivalent to <c:out escapeXML="true"> for servlets?

 
JohnWilliam Fitz
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

Short version of my question: Is there a convenience method somewhere in the Servlet API that will accomplish the same thing the <cut escapeXML="true"> does?

Long version: I have some html to output from my servlet that should be output "as is", meaning, for example, the browser should see "<textarea>blahblahblah</textarea>" instead a text area in their browser. Before I embark on looping through my output strings and replacing all my '<' characters with "<", I thought I would ask if there is already a function that does that.

Thanks!
John
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is not.
 
Peter Johnson
author
Bartender
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like the org.apache.taglibs.standard.tag.common.core.OutSupport class in the standard tag library provides this static method:

private static void writeEscapedXml(char buffer[], int length, JspWriter w) throws IOException

Perhaps you could make use of that to do the XML escaping for you.

Ditto for org.apache.jasper.tagplugins.jstl.Util:

public static java.lang.String escapeXml(java.lang.String buffer)


Beats having to write the code yourself...
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or, use the same method mapped to ${fn:escapeXml()}:

org.apache.taglibs.standard.functions.escapeXml()

which is in the JSTL's standard jar (which you probably already have loaded into your web app).
 
JohnWilliam Fitz
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both for the replies. I will look at these different possibilities.

But now I have a new question: Where does one find the docs for these packages/classes/methods from Apache?

I searched around for documentation but I can find nothing. I even directly entered "org.apache.taglibs.standard.Functions" into the search field on "apache.org" and none of the hits were documentation. (Well, ok, I did find what appears to be the source code for the Functions class, and I guess source code is really the ultimate documentation, but I was looking for something a little more concise, maybe in javadoc format.)

Thanks for your help!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I opened the JSTL jar files and looked. I knew that there had to be an implementation of ${fn:escapeXml()} so I just looked for it.

Of course, using these methods ties you to the Apache implementation of the JSTL if that matters to you.

By the way, writing your own is a simple 2 or 3 line method if you want to avoid the use of any proprietary methods.
 
JohnWilliam Fitz
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you're right. I wrote my own in the meantime, converting all the "less thans" and "ampersands". I'm just worried I missed some poisonous combination of characters that I should also be watching out for that these other might already know about and handle.

Thanks again.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
escapeXML() converts <, > &, single quote and double quote.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!