Click to See Complete Forum and Search --> : HTML - Escaping string values
February 22nd, 2000, 02:59 PM
Is there a utility function that I can use to escape string values so that they can be used as values in an HTML form?
E.g: "Barnes & Nobles" would be used as the value of an HTML TextArea
<TEXTAREA NAME="txt">Barnes & Nobles</TEXTAREA>
Java provides a function to encode URLs but I could't find a similar function that can be used to encode form contents.
Thanks,
Frederic
Jan Meeuwesen
February 23rd, 2000, 08:21 AM
Hi there,
I don't exactly know what you mean but I guess that you mean that you want to set the values on an returned or an existing HTML form. Can you explain in which context? Do you do this with servlets? From an applet running on the same page? Just be a little more specific.
I might be able to help you.
Greets,
Jan Meeuwesen
February 23rd, 2000, 01:13 PM
Hi Jan,
Yes, I would use the strings to generate the contents of an HTML page or set initial form values that might contain reserved HTML characters.
All I want to do is to be able to replace the reserved HTML characters with their corresponding character entities.
E.g.: The following will not display propery
<TEXTAREA>
The TEXTAREA element indicates a multi-line text entry field.
The contents between the beginning <TEXTAREA> and end </TEXTAREA> tags represent the initial contents of the field in the browser.
</TEXTAREA>
...because the end tag in the content needs to be escaped.
or suppose that I want to display fragments of HTML source within an HTML page. You'd need to escape nearly all the character.
I.e.: "&amp;" would be translated into "&amp;amp;" if I wanted to display the the html fragment as source.
I would only need to take care of the important characters: '<', '>', '"' and '&' but I'd rather not write my own utility when there is something thats already available.
Hope this clears things up.
Frederic
Jan Meeuwesen
February 24th, 2000, 02:16 AM
Hi,
I'm very sorry to disappoint you (and this probably is, because my English is (assumingly) not so good) but I'm not certain what the main problem is.
for example if this is the line of HTML code you want to write to the HTML file:
<B>This is a line of "Bold" text! And this are some escape characters: < > " & .</B>
You just make a method that accepts a String and returns the string with escape characters for example:
public class EscapeCharacters {
private String ConvertToEscapeCharacters(String Temp) {
String ReturnValue = new String();
ReturnValue = "";
int i = 1;
while (i<=Temp.length()) {
if ((Temp.substring(i-1,i)).equals("<"))
ReturnValue = ReturnValue + "<";
else if ((Temp.substring(i-1,i)).equals(">"))
ReturnValue = ReturnValue + ">";
else if ((Temp.substring(i-1,i)).equals("\""))
ReturnValue = ReturnValue + """;
else if ((Temp.substring(i-1,i)).equals("&"))
ReturnValue = ReturnValue + "&";
else {
ReturnValue = ReturnValue + Temp.substring(i-1,i);
}
i++;
}
return ReturnValue;
}
public static void main(String[] args) {
//The following line will be converted.
//<B>This is a line of "Bold" text! And this are some escape characters: < > " & .</B>
EscapeCharacters MyEC = new EscapeCharacters();
String Temp = new String();
Temp = MyEC.ConvertToEscapeCharacters("<B>This is a line of \"Bold\" text! And this are some escape characters: < > \" & .</B>");
System.out.println(Temp);
}
}
The Output will be:
<B>This is a line of "Bold" text! And this are some escape characters: < > " & .</B>
You see... It's pretty easy to make your own converter!!!
The only thing that could be a little difficult is that I use this String :
"<B>This is a line of \"Bold\" text! And this are some escape characters: < > \" & .</B>
Instead of:
"<B>This is a line of "Bold" text! And this are some escape characters: < > " & .</B>
Because in java \" is an escape character for "
I hope this is of help to you,
Greets,
Jan Meeuwesen.
(Don't forget to vote)
February 24th, 2000, 10:05 AM
Hi,
That's exactly it. Except that I would have expected that such a utility exists already and that I could skip on future maintenance. But, in the end you end up having to right your own code anyway... which I did.
Thanks,
Frederic
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.