/** * 防范SQL注入漏洞,检测输入的字符 * 需要检测的特殊字符及字符串有:",","-","/","\\","'","%",""" * @param strInput 待检测的字符 * @author Tony Lin Added on 2008-10-21 */ function checkInputChar(strInput){ var forbidChar = new Array(",","-","/","\\","'","%","\""); for (var i = 0;i < forbidChar.length ; i++){ if(strInput.indexOf(forbidChar[i]) >= 0){ alert("您输入的信息: "+strInput+" 中含有非法字符: "+forbidChar[i]+" 请更正!"); return false; } } return true; } /** * 遍历form内元素text类型检测字符合法性,此方法结合 checkInputChar(strInput)使用 * JSP页面只要调用该方法,并传入form名即可自动完成该form内所有元素字符的检测 * @param formName 待检测的form名 * @author Tony Lin Added on 2008-10-21 */ function validationFormText(formName){ var actionForm; //alert(typeof(formName)); if(typeof(formName)){ actionForm = formName; }else{ actionForm = document.forms[0]; } for (var i=0;i<actionForm.length;i++ ){ if (actionForm.elements[i].value != "" && actionForm.elements[i].type == "text"){ //alert(actionForm.elements[i].value); if(!checkInputChar(actionForm.elements[i].value)){ actionForm.elements[i].focus(); return false; } } } return true; } 在JSP页面的查询事件中增加如下代码即可: //检测form内所有text元素的字符合法性 if(!validationFormText(form1)){ return(false); }
转换编码试试:例如 String name = request.getParameter("name"); name = java.net.URLDecoder.decode(name); name=CoDataTran.chineseToUnicode(name); 自己试试,单个特殊字符应该可以拿到,但是中文有可能乱码,自己处理一下。
* 防范SQL注入漏洞,检测输入的字符
* 需要检测的特殊字符及字符串有:",","-","/","\\","'","%","""
* @param strInput 待检测的字符
* @author Tony Lin Added on 2008-10-21
*/
function checkInputChar(strInput){
var forbidChar = new Array(",","-","/","\\","'","%","\"");
for (var i = 0;i < forbidChar.length ; i++){
if(strInput.indexOf(forbidChar[i]) >= 0){
alert("您输入的信息: "+strInput+" 中含有非法字符: "+forbidChar[i]+" 请更正!");
return false;
}
}
return true;
} /**
* 遍历form内元素text类型检测字符合法性,此方法结合 checkInputChar(strInput)使用
* JSP页面只要调用该方法,并传入form名即可自动完成该form内所有元素字符的检测
* @param formName 待检测的form名
* @author Tony Lin Added on 2008-10-21
*/
function validationFormText(formName){
var actionForm;
//alert(typeof(formName));
if(typeof(formName)){
actionForm = formName;
}else{
actionForm = document.forms[0];
}
for (var i=0;i<actionForm.length;i++ ){
if (actionForm.elements[i].value != "" && actionForm.elements[i].type == "text"){
//alert(actionForm.elements[i].value);
if(!checkInputChar(actionForm.elements[i].value)){
actionForm.elements[i].focus();
return false;
}
}
}
return true;
} 在JSP页面的查询事件中增加如下代码即可:
//检测form内所有text元素的字符合法性
if(!validationFormText(form1)){
return(false);
}
String name = request.getParameter("name");
name = java.net.URLDecoder.decode(name);
name=CoDataTran.chineseToUnicode(name);
自己试试,单个特殊字符应该可以拿到,但是中文有可能乱码,自己处理一下。
< < <
∴ ∴ ∴
• • •这是HTML对转义字符的处理
该怎么办呢?总不能在ActionForm里对所有的转义字符 replaceAll() 吧?
public static String format(String content) {
content = content.replaceAll(" ", " ").replaceAll("<",
"<").replaceAll(">", ">").replaceAll("\r\n", "<br>"); return content;
}试试这个,我觉得行!
{//处理中文字符串的函数
String s=str;
try
{
byte tempB[]=s.getBytes("ISO-8859-1");
s=new String(tempB);
return s;
}
catch(Exception e)
{
return s;
}
}String t1=codeToString((String)request.getParameter("txt1"));//获取组件值