jsp中cookie传值中文乱码问题如何解决,快整死我了
存数据页面nam=newString(request.getParameter("st_name").getBytes("iso-8859-1"));nam=URLEnc...
存数据页面
nam = new String(request.getParameter("st_name").getBytes("iso-8859-1")); nam = URLEncoder.encode(nam,"GB2312");
if(nam != null){
cookie = new Cookie("nam",nam);
cookie.setMaxAge(30*60);
response.addCookie(cookie);
}
取数据页面
if(cook[i].getName().equals("nam")){
request.setAttribute("senam", cook[i].getValue());
senam = request.getAttribute("senam").toString();
senam = new String(senam.getBytes("ISO-8859-1"),"gb2312");
senam = URLDecoder.decode(senam, "GB2312");
}
输入“你好”输出为“?羲?”页面的编码方式是utf-8 展开
nam = new String(request.getParameter("st_name").getBytes("iso-8859-1")); nam = URLEncoder.encode(nam,"GB2312");
if(nam != null){
cookie = new Cookie("nam",nam);
cookie.setMaxAge(30*60);
response.addCookie(cookie);
}
取数据页面
if(cook[i].getName().equals("nam")){
request.setAttribute("senam", cook[i].getValue());
senam = request.getAttribute("senam").toString();
senam = new String(senam.getBytes("ISO-8859-1"),"gb2312");
senam = URLDecoder.decode(senam, "GB2312");
}
输入“你好”输出为“?羲?”页面的编码方式是utf-8 展开
2个回答
2016-02-19 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
jsp中cookie传值中文乱码问题:
通过java.net.URLEncoder对中文编码,然后通过java.net.URLDecoder对其进行解码。
添加cookie:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>set cookie</title>
</head>
<body>
<%
String str = "这是中文的cookie值";
Cookie c = new Cookie("str",java.net.URLEncoder.encode(str));
c.setMaxAge(24*3600);
//向客户端添加cookie对象
response.addCookie(c);
%>
</body>
</html>
接收cookie:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>get-cookie-value -- by www.phpddt.com</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
Cookie[] cookies = request.getCookies();
for(Cookie c : cookies)
{
//如果有名为str的cookie值,则是要需找的
if(c.getName().equals("str"))
{
out.print(java.net.URLDecoder.decode(c.getValue()));
}
}
%>
</body>
</html>
通过java.net.URLEncoder对中文编码,然后通过java.net.URLDecoder对其进行解码。
添加cookie:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>set cookie</title>
</head>
<body>
<%
String str = "这是中文的cookie值";
Cookie c = new Cookie("str",java.net.URLEncoder.encode(str));
c.setMaxAge(24*3600);
//向客户端添加cookie对象
response.addCookie(c);
%>
</body>
</html>
接收cookie:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>get-cookie-value -- by www.phpddt.com</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
Cookie[] cookies = request.getCookies();
for(Cookie c : cookies)
{
//如果有名为str的cookie值,则是要需找的
if(c.getName().equals("str"))
{
out.print(java.net.URLDecoder.decode(c.getValue()));
}
}
%>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询