日期:2014-05-16  浏览次数:20476 次

对于特殊字符保存到数据库后再读出原样输出到页面
/**
   *如<a href="http://www.baidu.com">test</a>原样保存到数据库,然后到页面又原样输入
   * Convert the string which contains html Tag to common string.
   * @param value
   *          string to convert.
   * @return
   *         the result string.
   */
  public static String escapeHTMLTags(String sourcestr)
  {

    if (sourcestr == null)
      return "";

    int strlen;
    String restring = "";
    String destr = "";
    strlen = sourcestr.length();

    for (int i = 0; i < strlen; i++)
    {
      char ch = sourcestr.charAt(i);

      switch (ch)
      {
      case '<':
        destr = "&lt;";
        break;
      case '>':
        destr = "&gt;";
        break;
      case '\"':
        destr = "&quot;";
        break;
      case '&':
        destr = "&amp;";
        break;
      case 13:
        destr = "<br>";
        break;
      case 32:
        destr = "&nbsp;";
        break;
      default:
        destr = "" + ch;
        break;
      }
      restring = restring + destr;
    }
    return "" + restring;
  }