首页 > 代码库 > Jsp---cookie中文乱码解决方法
Jsp---cookie中文乱码解决方法
//写cookie
<form method="Get">
姓名:
<input type="text" name="name" />
<br />
<input type="submit" value="http://www.mamicode.com/确定" />
</form>
<!-- 写入cookie到客户端 -->
<%
if (request.getParameter("name") != null
&& !request.getParameter("name").equals("")) {
String str=request.getParameter("name");
String value = http://www.mamicode.com/URLEncoder.encode(str);
Cookie cookie = new Cookie("name", value);
response.addCookie(cookie);
}
%>
//读cookie
<%
request.setCharacterEncoding("utf-8");
Cookie myCookie[] = request.getCookies();
for (int i = 0; i < myCookie.length; i++) {
if (myCookie[i].getName().equals("name")) {
out.print(myCookie[i].getName() + ":"
+ URLDecoder.decode(myCookie[i].getValue()) + "<br>");
}
}
%>