This was discussed in JSP list. I attach a method (I think it's from PurpleTech
FAQ)
public static String HTMLEncode(String text)
{
if (text == null) return "";
StringBuffer results = null;
char[] orig = null;
int beg = 0, len = text.length();
for (int i = 0; i < len; ++i){
char c = text.charAt(i);
switch (c){
case 0:
case '&':
case '<':
case '>':
case '"':
if (results == null){
orig = text.toCharArray();
results = new StringBuffer(len+10);
}
if (i > beg)
results.append(orig, beg, i-beg);
beg = i + 1;
switch (c){
default: // case 0:
continue;
case '&': results.append("&"); break;
case '<': results.append("<"); break;
case '>': results.append(">"); break;
case '"': results.append("""); break;
}
break;
}
}
if (results == null)
return text;
results.append(orig, beg, len-beg);
return results.toString();
}
Jay Macarty wrote:
> I have a servlet which needs to be able to show the user the source of an
> HTML file. In performing this same type function in other languages I simply
> read the text and converted the characters to their & equivalent. For
> example, <HTML> would become <HTML>
>
> I was wondering if there is an easier way to accomplish this task? If not,
> does someone have, or know of, existing Java code to do the conversions?
>
> --------------------------------------------------
> Jay Macarty - Technical Specialist
> Phone: (972) 591-7385
>
> ___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
--
David Mossakowski [EMAIL PROTECTED]
Programmer 212.310.7275
Instinet Corporation
"I don't sit idly by, I'm planning a big surprise"
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html