jsp页面获取后台传输cookie

jsp页面获取后台传输cookie

此方法适用获取单个cookie值

<%
//取出Cookie中的中文内容
Cookie [] cookies = request.getCookies();
String str = "";
for(int i=0;i<cookies.length;i++){
 if(cookies[i].getName().equals("userNameZH")) {
 str = cookies[i].getValue();
 }
}
out.println(URLDecoder.decode(str, "UTF-8"));
%>

cookie在保存中文字符时需要进行转码
存放时转码:

cookieNameZH = new Cookie("userNameZH",URLEncoder.encode(userNameZH, "UTF-8"));

取出时转码:

out.println(URLDecoder.decode(str, "UTF-8"));

你可能感兴趣的:(javascript,jquery)