比如:
onClick="doSave('<bean:write name="beanname" property="name"/>');这个<bean:write name="beanname" property="name"/> 可能是含有' 的字符串。
onClick="doSave('<bean:write name="beanname" property="name"/>');这个<bean:write name="beanname" property="name"/> 可能是含有' 的字符串。
例如,如果要输出到javascript里,则要经过与以下jsEncoder类似的转换:
<%=CommStr.jsEncoder(yourString)%>/*
* Created by Seamus
* Updated on 2004-10-21 by JK
*
*/
package com.jk.util;public class CommStr
{
/**
*replace the old string to new string in the given destination string.
*/
public static java.lang.String strReplace(java.lang.String destStr, java.lang.String oldStr, java.lang.String newStr)
{
if(destStr==null)
return "";
String tmpStr = destStr;
int foundPos = tmpStr.indexOf(oldStr);
while (foundPos>=0)
{
tmpStr = tmpStr.substring(0,foundPos) + newStr + tmpStr.substring(foundPos + oldStr.length(),tmpStr.length());
foundPos = tmpStr.indexOf(oldStr,foundPos+newStr.length());
}
return tmpStr;
} /**
*Encode for HTML.
*/
public static String htmlEncoder(String str)
{
if(str==null || str.equals(""))
return "";
String res_str;
res_str=strReplace(str,"<","<");
res_str=strReplace(str,">","&rt;");
res_str=strReplace(str,"\"",""");
res_str=strReplace(str,"'","'");
return res_str;
} /**
*Encode for HTML-Text.
*/
public static String htmlTextEncoder(String str)
{
if(str==null || str.equals(""))
return "";
String res_str;
res_str=strReplace(str,"<","<");
res_str=strReplace(str,">","&rt;");
res_str=strReplace(str,"\"",""");
res_str=strReplace(str,"'","'");
res_str=strReplace(str," "," ");
res_str=strReplace(str,"\r\n","<br>");
res_str=strReplace(str,"\r","<br>");
res_str=strReplace(str,"\n","<br>");
return res_str;
} /**
*Encode for URL.
*/
public static String urlEncoder(String str) {
return java.net.URLEncoder.encode(str) ;
} /**
*Encode for XML.
*/
public static String xmlEncoder(String str)
{
if(str==null || str.equals(""))
return "";
String res_str;
res_str=strReplace(str,"&","&");
res_str=strReplace(res_str,"<","<");
res_str=strReplace(res_str,">",">");
res_str=strReplace(res_str,"\"", """);
res_str=strReplace(res_str,"\'", "´");
return res_str;
} /**
*Encode for SQL.
*/
public static String sqlEncoder(String str)
{
if(str==null || str.equals(""))
return "";
String res_str;
res_str=strReplace(str,"'","''");
return res_str;
} /**
*Encode for Javascript.
*/
public static String jsEncoder(String str)
{
if(str==null || str.equals(""))
return "";
String res_str;
res_str=strReplace(str,"'","\\'");
res_str=strReplace(str,"\"","\\\"");
res_str=strReplace(str,"\r\n","\\\n");
res_str=strReplace(str,"\n","\\\n");
res_str=strReplace(str,"\r","\\\n");
return res_str;
}}