• Post Reply Bookmark Topic Watch Topic
  • New Topic

Converting &, > " and other odd characters  RSS feed

 
Sam Gehouse
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would like to hear your recommendation on any good utility or Javascript that would convert &, >, <, ", and other odd-ball characters for display purposes in JSP.

It is for a JSP that displays data as:
<%=myValue%>

The content of myValue needs to be properly displayed.

JSTL, escapeXML etc. is not in use.

Thanks in advance,
SG
 
Sam Gehouse
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, cannot use StringEscapeUtils of Apache as that version of jar is not in the project.
 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you've just discounted the most common solutions I would suggest.

JSTL <cut> tag would do that for you. As will the struts <bean:write>
The Jakarta commons StringUtils would have been my next stop.

A quick google search finds this bit of code:
http://www.javapractices.com/Topic96.cjp

Or you could always reinvent the square wheel yourself.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66158
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSTL, escapeXML etc. is not in use.

Why not?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, cannot use StringEscapeUtils of Apache as that version of jar is not in the project.


Copy it into a class of your own naming, pretend you wrote it, ask for a bonus?

Seriously, I was in the same position and copied what I thought I needed from there to my own class. Silly rules about open source.
[ October 27, 2007: Message edited by: Stan James ]
 
Joe Gliniecki
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This topic just came up for me too, and using the JSTL <cut> tag worked great.

I had been using the JSP EL to set the value for HTML form elements. My question is whether its good practice to always wrapper the EL statement with <cut>, or is this typically done only when characters such as ", <, &, etc. are expected and thus need to be encoded?

Thanks,
Joe
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66158
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the source of the text is untrusted (as is all user input) or unknown, it should be protected via encoding.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!