站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2SE 专题URL传递参数中含有特殊符问题!100分相赠

URL传递参数中含有特殊符问题!100分相赠

摘要: URL传递参数中含有特殊符问题!100分相赠


在进行参数传递时,URL的参数值里有特殊符号的问题,导致接收的参数值不正常.如下:

http://mooner/20060828/servlet/Controller?type=BaseInfo&&op=qry_popnum&&cond=total>25000%20and%20time>=2005%20and%20time<=2005

键:cond
值:total>25000%20and%20time>=2005%20and%20time<=2005 (查询表的一过滤条件)

我希望在接受参数cond的值时,得到结果 cond = "total>25000 and time>=2005 and time<=2005";

请知道的兄弟点解............解决问题.......马上结帖!!!

(希望能提供部分代码) 小弟先谢过!



空格只是在浏览器传递的时候解释成了%20,实际接收到的还是空格,你打印出来试试。


特殊符号包括亚洲文字在传递之前服务器都要经过java.net.URLEncoder.encode();转吗的,所以自然会出现%20,这里的%20就代表你参数里的空格


public class HttpUtil {
private HttpUtil() {
}
private static final boolean[] safeUrl = new boolean[256];
static {
for (int i = 'a'; i <= 'z'; i++) {
safeUrl[i] = true;
}
for (int i = 'A'; i <= 'Z'; i++) {
safeUrl[i] = true;
}
for (int i = '0'; i <= '9'; i++) {
safeUrl[i] = true;
}
safeUrl['_'] = true;
safeUrl[':'] = true;
safeUrl['/'] = true;
safeUrl['.'] = true;
safeUrl['~'] = true;
}

/**
* Maps a string to be used in a query or post into a form that is
* acceptable in an URL. Typically used when the caller wants to safely
* generate an HREF containing an arbitrary string that may have special
* characters.
*


* URL strings may not contain non-alphanumeric characters. All
* non-alphanumeric characters are converted to the escape sequence "%XX",
* where XX is the hexadecimal value of that character's code.
*


* Note that the space character " " is NOT converted to "+". That is a
* common misconception. "+" represents a space only in query strings, not
* in the URL. "%20" is how an actual space character must be passed in an
* URL, and is also an acceptable way of passing a space in a query string.
*
* @param string
* The string to convert.
*
* @return The URL-encoded version of the given string.
*/
public static String urlEncode(String src) {
StringBuffer result = new StringBuffer();
int length = (src == null) ? 0 : src.length();
for (int i = 0; i < length; i++) {
int ch = src.charAt(i) & 0xff;
if (safeUrl[ch]) {
result.append((char) ch);
} else {
result.append('%');
result.append(Character.forDigit((ch >> 4) & 0x0f, 16));
result.append(Character.forDigit(ch & 0x0f, 16));
}
}

return result.toString();
}

/**
* Decodes a URL-encoded string by replacing all the "%XX" escape sequences
* in the string with the corresponding character.
*


* Malformed "%XX" sequences are silently ignored.
*
* @param string
* The URL-encoded string.
*
* @return The decoded version of the given string.
*/
public static String urlDecode(String src) {
if (src == null) {
return "";
}
int i = src.indexOf('%');
if (i < 0) {
return src;
}

StringBuffer result = new StringBuffer(src.substring(0, i));
int length = src.length();
for (; i < length; i++) {
char ch = src.charAt(i);
if (ch == '%') {
try {
ch = (char) Integer.parseInt(src.substring(i + 1, i + 3),
16);
i += 2;
} catch (Exception e) {
// Ignore malformed % sequences, just insert the '%'.
}
}
result.append(ch);
}
return result.toString();
}
}



这类问题可以考虑使用java.net.URLEncoder之类的解决

^_^


java.net.URLDecoder配合使用

^_^


mark!


别的先不说,你都把你的字段名告诉黑客了


谢谢楼上的提醒...!


↑返回目录
前一篇: 关于JAVA参数传输方式的问题 急急急~~~~
后一篇: 请教关于javax.servlet.RequestDispatcher.forward(request, response)