黑马程序员技术交流社区
标题:
代码中关于对象和传入值的使用注意,各位有什么好的方法
[打印本页]
作者:
ximi
时间:
2014-8-15 21:17
标题:
代码中关于对象和传入值的使用注意,各位有什么好的方法
本帖最后由 ximi 于 2014-8-18 08:48 编辑
首先我说说我的一般处理方法,然后请问各位平时怎么样处理的,有什么更好的方法?
/**
* 过滤非法字符
* @param value 要查询的字符
* @return 过滤后的字符
*/
private synchronized static String removeIIIagelChar(String value){
return value.replaceAll("[\\pP|~|$|^|<|>|\\||\\+|=|`| ]*", "");
}
public static void main(String[] args) {
/*
* 在日常的开发过程中,为了保证程序的健壮性,防止SQL注入,
* 我们一般对于参数的传入与使用会做一些判断处理
*/
//1. 对于字符串使用
String test1 = null;
if (test1 != null && test1.trim().length() != 0){
System.out.println(test1);
}
//2. 对于集合的使用
List test2 = null;
if (test2 != null && test2.size() != 0){
System.out.println(test2.get(0));
}
//3. 对于对象Map的使用
Map<String, String> test3 = null;
String key = "1";
if (test3 != null && test3.size() != 0){
if (test3.containsKey(key)){
System.out.println(test3.get(key));
}
}
//4. 对于查询参数的使用
String test4 = " 1-- ";
//非空判断
if (test4 != null && test4.trim().length() != 0){
//不作为查询参数则去除空格即可(密码可以不需要)
test4 = test4.trim();
//作为查询参数则需要去除特殊字符
test4 = removeIIIagelChar(test4);
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2