在使用"get"時抓取的頁面最後加上編碼類型
代碼如下:
<%
服務器端
servletactioncontext
getresponse()
setcharacterencoding("utf
");
客戶端 網頁特效p/jsp
html target=_blank >jsp教程
<%@ page language="java" contenttype="text/html; charset=utf
" pageencoding="utf
"%>
response
expires =
response
addheader "pragma"
"no
cache"
response
addheader "cache
ctrol"
"no
cache"
response
addheader "content
type"
"text/html; charset=gb
"
這是重點
否則會出現亂碼
response
write "中文漢字"%>
在使用post時用vbscript解決了編碼問題
源碼如下
代碼如下:
<script language="vbscript">
function urlencoding(vstrin)
strreturn = ""
for i =
to len(vstrin)
thischr = mid(vstrin
i
)
if abs(asc(thischr)) < &hff then
strreturn = strreturn & thischr
else
innercode = asc(thischr)
if innercode <
then
innercode = innercode + &h
end if
hight
= (innercode and &hff
) &hff
low
= innercode and &hff
strreturn = strreturn & "%" & hex(hight
) & "%" & hex(low
)
end if
next
urlencoding = strreturn
end function
function bytes
bstr(vin)
strreturn = ""
for i =
to lenb(vin)
thischarcode = ascb(midb(vin
i
))
if thischarcode < &h
then
strreturn = strreturn & chr(thischarcode)
else
nextcharcode = ascb(midb(vin
i+
))
strreturn = strreturn & chr(clng(thischarcode) * &h
+ cint(nextcharcode))
i = i +
end if
next
bytes
bstr = strreturn
end function
</script>
下面是使用vbscript函數
代碼如下:
<script language=網頁特效>
/**
* 初始化一個xmlhttp對象
*/
function initajax()
{
var ajax=false;
try {
ajax = new activexobject("msxml
xmlhttp");
} catch (e) {
try {
ajax = new activexobject("microsoft
xmlhttp");
} catch (e) {
ajax = false;
}
}
if (!ajax && typeof xmlhttprequest!=
undefined
) {
ajax = new xmlhttprequest();
}
return ajax;
}
function saveuserinfo()
{
var msg = document
getelementbyid("msg");
var f = document
user_info;
var username = f
user_name
value;
var userage = f
user_age
value;
var usersex = f
user_sex
value;
var url = "save
asp教程";
var poststr = urlencoding("user_name="+ username +"&user_age="+ userage +"&user_sex="+ usersex);//post時采用編碼傳遞
var ajax = initajax();
ajax
open("post"
url
true);
ajax
setrequestheader("content
type"
"application/x
www
form
urlencoded");
ajax
send(poststr);
ajax
onreadystatechange = function() {
if (ajax
readystate ==
&& ajax
status ==
) {
msg
innerhtml = bytes
bstr(ajax
responsebody); //獲取時解碼
}
}
}
</script>
<form name="user_info">
姓名
<textarea name="user_name" /></textarea><br />
年齡
<input type="text" name="user_age" /><br />
性別
<input type="text" name="user_sex" /><br />
<input type="button" value="提交表單" onclick="saveuserinfo()">
</form>
<div id="msg"></div>
成功
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20587.html