• Post Reply Bookmark Topic Watch Topic
  • New Topic

Library to do HTML entity encoding?  RSS feed

 
Lawrence Kennon
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In my application I retrieve String values from a database and before I display them on the webpage I want to make sure they are properly HTML encoded. For example, I might retrieve the value "AT&T" as a String from the database. Before outputing that String to a web page I probably ought to change the ampersand to the proper HTML encoding.

I have come across several examples on the web of people writing their own Java class to do this. I also understand there is a package called JTidy at SourceForge, but it looks to me like it hasn't been updated since around 2000.

My question is this - is there a good, supported, and well documented package out there that you would recommend for this purpose?

Thanks in advance for your help!

lwk
 
Peter Johnson
author
Bartender
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps this recent discussion will help: http://www.coderanch.com/t/435951/Servlets/java/equivalent-c-out-escapeXML-true
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you emitting HTML from a servlet rather than forwarding to a JSP when the servlet's job is done?
 
Lawrence Kennon
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Why are you emitting HTML from a servlet rather than forwarding to a JSP when the servlet's job is done?


Actually, that is exactly what I am doing. Let's say I have a String in the servlet. I add it as attribute to the request and forward it to a JSP to display it.

What I wanted to do was to HTML encode the String _before_ I sent it to the JSP. If there were an easy way to do it in JSP that would be fine - I just don't know how to do that.

lwk
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<c:out> will do it just fine. There's no need to convert before sending it to the JSP.
 
Lawrence Kennon
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter Johnson wrote:Perhaps this recent discussion will help: http://www.coderanch.com/t/435951/Servlets/java/equivalent-c-out-escapeXML-true


Thanks! Actually this pointed me to the tool I really need, the JSTL core library function that will HTML encode the String for me. Obviously I am still learning a lot of this!

lwk
 
Lawrence Kennon
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:<c:out> will do it just fine. There's no need to convert before sending it to the JSP.


Also, thanks! I just figured that out. Much easier. I am actually getting to like JSTL.

lwk
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's very little not to like.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!