- 看到了一段防止SQL注入的JavaScript代码,但是似乎在后台解决的话会更好。
- <SCRIPT language="JavaScript">
- function Check(theform)
- {
- if (theform.UserName.value=="")
- {
- alert("请输入用户名!")
- theform.UserName.focus();
- return (false);
- }
- if (theform.Password.value == "")
- {
- alert("请输入密码!");
- theform.Password.focus();
- return (false);
- }
- }
- function IsValid( oField )
- {
- re= /select|update|delete|exec|count|'|"|=|;|>|<|%/i;
- $sMsg = "请您不要在参数中输入特殊字符和SQL关键字!"
- if ( re.test(oField.value) )
- {
- alert( $sMsg );
- oField.value = '';
- oField.focus();
- return false;
- }
- }
- </SCRIPT>
看到了一段防止SQL注入的JavaScript代码,但是似乎在后台解决的话会更好。
<SCRIPT language="JavaScript">
function Check(theform)
{
if (theform.UserName.value=="")
{
alert("请输入用户名!")
theform.UserName.focus();
return (false);
}
if (theform.Password.value == "")
{
alert("请输入密码!");
theform.Password.focus();
return (false);
}
}
function IsValid( oField )
{
re= /select|update|delete|exec|count|'|"|=|;|>|<|%/i;
$sMsg = "请您不要在参数中输入特殊字符和SQL关键字!"
if ( re.test(oField.value) )
{
alert( $sMsg );
oField.value = '';
oField.focus();
return false;
}
}
</SCRIPT>
后台处理::::
- JAVA-字符串过滤类
- package cn.com.hbivt.util;
- /**
- * <p>Title: </p>
- *
- * <p>Description: </p>
- *
- * <p>Copyright: Copyright (c) 2005</p>
- *
- * <p>Company: </p>
- *
- * @author not attributable
- * @version 1.0
- */
- public class StringUtils {
- //过滤通过页面表单提交的字符
- private static String[][] FilterChars={{"<","<"},{">",">"},{" "," "},{"\"","""},{"&","&"},
- {"/","/"},{"\\","\"},{"\n","<br>"}};
- //过滤通过javascript脚本处理并提交的字符
- private static String[][] FilterScriptChars={{"\n","\'+\'\\n\'+\'"},
- {"\r"," "},{"\\","\'+\'\\\\\'+\'"},
- {"\'","\'+\'\\\'\'+\'"}};
- /**
- * 用特殊的字符连接字符串
- * @param strings 要连接的字符串数组
- * @param spilit_sign 连接字符
- * @return 连接字符串
- */
- public static String stringConnect(String[] strings,String spilit_sign){
- String str="";
- for(int i=0;i<strings.length;i++){
- str+=strings[i]+spilit_sign;
- }
- return str;
- }
- /**
- * 过滤字符串里的的特殊字符
- * @param str 要过滤的字符串
- * @return 过滤后的字符串
- */
- public static String stringFilter(String str){
- String[] str_arr=stringSpilit(str,"");
- for(int i=0;i<str_arr.length;i++){
- for(int j=0;j<FilterChars.length;j++){
- if(FilterChars[j][0].equals(str_arr[i]))
- str_arr[i]=FilterChars[j][1];
- }
- }
- return (stringConnect(str_arr,"")).trim();
- }
- /**
- * 过滤脚本中的特殊字符(包括回车符(\n)和换行符(\r))
- * @param str 要进行过滤的字符串
- * @return 过滤后的字符串
- * 2004-12-21 闫
- */
- public static String stringFilterScriptChar(String str){
- String[] str_arr=stringSpilit(str,"");
- for(int i=0;i<str_arr.length;i++){
- for (int j = 0; j < FilterScriptChars.length; j++) {
- if (FilterScriptChars[j][0].equals(str_arr[i]))
- str_arr[i] = FilterScriptChars[j][1];
- }
- }
- return(stringConnect(str_arr,"")).trim();
- }
- /**
- * 分割字符串
- * @param str 要分割的字符串
- * @param spilit_sign 字符串的分割标志
- * @return 分割后得到的字符串数组
- */
- public static String[] stringSpilit(String str,String spilit_sign){
- String[] spilit_string=str.split(spilit_sign);
- if(spilit_string[0].equals(""))
- {
- String[] new_string=new String[spilit_string.length-1];
- for(int i=1;i<spilit_string.length;i++)
- new_string[i-1]=spilit_string[i];
- return new_string;
- }
- else
- return spilit_string;
- }
- /**
- * 字符串字符集转换
- * @param str 要转换的字符串
- * @return 转换过的字符串
- */
- public static String stringTransCharset(String str){
- String new_str=null;
- try{
- new_str=new String(str.getBytes("iso-8859-1"),"GBK");
- }
- catch(Exception e){
- e.printStackTrace();
- }
- return new_str;
- }
- /**
- * 测试字符串处理类
- * @param args 控制台输入参数
- */
- public static void main(String[] args){
- //测试字符串过滤
- String t_str1="<h1>StringDispose字符串 处理\n\r\'\"</h1>";
- System.out.println("过滤前:"+t_str1);
- System.out.println("过滤后:"+StringUtils.stringFilter(t_str1));
- //测试合并字符串
- String[] t_str_arr1={"PG_1","PG_2","PG_3"};
- String t_str2=StringUtils.stringConnect(t_str_arr1,",");
- System.out.println(t_str2);
- //测试拆分字符串
- String[] t_str_arr2=StringUtils.stringSpilit(t_str2,",");
- for(int i=0;i<t_str_arr2.length;i++){
- System.out.println(t_str_arr2[i]);
- }
- }
- }