web应用文本输入框内容存储的特殊字符转换

文本框输入内容存储,需要转换特殊字符
1、保存到数据库之前

char[]input=text.toCharArray();
StringBuffer out = new StringBuffer();
int len = input.length;
char c;
int i=0;
while(i<len){
        c=input[i];
	if(c=='<')
	  out.append("&lt;");
        else if(c=='>')
          out.append("&gt;");
        else if(c=='\r'){
          char h=input[i+1]
          if(h=='\n'){
             out.append("<BR>");
          }
        }else if(c=='\n'){
	  out.append('<BR>');
	}else if(c==' '){
          out.append("&nbsp;");
        }else if(c=='"'){
          out.append("&quot;"); 
        }else
          out.append(c);
       i++;
}
text=out.toString();
text=text.replace("\\","\\\\");转换 \
text=text.replace("'","\\'");转换 '
text=text.replace("\"","\\\"")转换 "



2、从数据库取出,显示到页面之前

memo = memo.replace("\\\\","\\");
memo = memo.replace("\\'","'");
memo = memo.replace("\\\"","\"");

你可能感兴趣的:(Web应用)