Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

HTML-Safe Bean Output  RSS feed

 
Marcellus Tryk
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have beans with text properties that might contain characters that need to be escaped before being presented in html content.
How can I do this elegantly while preserving the separation of presentation and business logic? I.e. I don't want to a lot of java code in the jsp but I also don't want the beans to be responsible for html encoding. Any suggestions?
Thanks,
Mars
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd suggest either a static method along the lines of URLEncoder.encode() (except HTMLEncoder.encode(), of course), or a custom tag to perform the encoding.
hth,
bear
 
Marcellus Tryk
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear,
I'm using a html encoding method, but that prevents me from using jsp:getProperty. This seems like it should be a pretty common problem. How can one be confident that a property returned by getProperty is HTML-safe?
Mars
 
Shawn Bayern
Author
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Marcellus Tryk:
Thanks Bear,
I'm using a html encoding method, but that prevents me from using jsp:getProperty. This seems like it should be a pretty common problem. How can one be confident that a property returned by getProperty is HTML-safe?
Mars

You can't, but JSTL's <cut> tag, which effectively replaces <jsp:getProperty>, will automatically encode characters by default, thus making them "HTML-safe." (You can turn off this behavior by using the "escapeXml" attribute.)
See http://java.sun.com/products/jstl for more info on JSTL, a new JCP standard.
 
Marcellus Tryk
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shawn -
That sounds like what I'm looking for. I guess I need to start using JSTL.
Thanks!
Mars
 
Ruilin Yang
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know ASP have a technique: buffer statement, which appears to solve the similar problem - i.e. to escape a block of html display under certain condition. I am wonder if JSP have something similar ?
 
Shawn Bayern
Author
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ruilin Yang:
I know ASP have a technique: buffer statement, which appears to solve the similar problem - i.e. to escape a block of html display under certain condition. I am wonder if JSP have something similar ?

Yes, the <c ut> tag does exactly this.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!