public static String htmlEncode(String s) {
    StringBuffer encodedString = new StringBuffer("");
    char[] chars = s.toCharArray();
    for (char c : chars) {
      if (c == '<') {
        encodedString.append("&lt;");
      } else if (c == '>') {
        encodedString.append("&gt;");
      } else if (c == '\'') {
        encodedString.append("&apos;");
      } else if (c == '"') {
        encodedString.append("&quot;");
      } else if (c == '&') {
        encodedString.append("&amp;");
      } else {
        encodedString.append(c);
      }
    }
    return encodedString.toString();
  }