Unicode编码:保存中文
2014-01-03 22:16
288 查看
提示:Cookie中保存中文只能编码,一般使用UTF-8编码即可。不推荐使用GBK等中文编码,
因为浏览器不一定支持,而且JavaScript也不支持GBK编码
<%@page
language="java"import="java.util.*"pageEncoding="UTF-8"isErrorPage="true"%>
<jsp:directive.pageimport="java.net.URLEncoder"/>
<jsp:directive.pageimport="java.net.URLDecoder"/>
<%
Cookie cookie =
new Cookie(URLEncoder.encode("姓名",
"UTF-8"),URLEncoder.encode("黎明","UTF-8"));
response.addCookie(cookie);
%>
<!DOCTYPEHTML
PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My
JSP 'index.jsp' starting page</title>
<meta
http-equiv="pragma"content="no-cache">
<meta
http-equiv="cache-control"content="no-cache">
<meta
http-equiv="expires"content="0">
<meta
http-equiv="keywords"content="keyword1,keyword2,keyword3">
<meta
http-equiv="description"content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
if(request.getCookies()!=null){
for(Cookie cc :request.getCookies()){
String cookieName = URLDecoder.decode(cc.getName(),"UTF-8");
String cookieValue
=URLDecoder.decode(cc.getValue(),"UTF-8");
out.print(cookieName+"="+cookieValue+";<br/>");
}
}else{
out.print("Cookie已经写入客户端,请刷新页面");
}
%>
</body>
</html>
因为浏览器不一定支持,而且JavaScript也不支持GBK编码
<%@page
language="java"import="java.util.*"pageEncoding="UTF-8"isErrorPage="true"%>
<jsp:directive.pageimport="java.net.URLEncoder"/>
<jsp:directive.pageimport="java.net.URLDecoder"/>
<%
Cookie cookie =
new Cookie(URLEncoder.encode("姓名",
"UTF-8"),URLEncoder.encode("黎明","UTF-8"));
response.addCookie(cookie);
%>
<!DOCTYPEHTML
PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My
JSP 'index.jsp' starting page</title>
<meta
http-equiv="pragma"content="no-cache">
<meta
http-equiv="cache-control"content="no-cache">
<meta
http-equiv="expires"content="0">
<meta
http-equiv="keywords"content="keyword1,keyword2,keyword3">
<meta
http-equiv="description"content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
if(request.getCookies()!=null){
for(Cookie cc :request.getCookies()){
String cookieName = URLDecoder.decode(cc.getName(),"UTF-8");
String cookieValue
=URLDecoder.decode(cc.getValue(),"UTF-8");
out.print(cookieName+"="+cookieValue+";<br/>");
}
}else{
out.print("Cookie已经写入客户端,请刷新页面");
}
%>
</body>
</html>
相关文章推荐
- Unicode – CSS中文字体转编码
- python unicode编码转中文字符
- java unicode编码 中文 转换
- Python处理unicode编码的txt文件(Python中文处理)——解决to_excel()和to_csv()导出文件内容为空的问题
- Css中常用中文字体的Unicode编码对照
- C语义char*显示中文---ASCII、DBCS、Unicode三种编码---char* CString string区别
- 中文字符 与 十六进制Unicode编码 相互转换
- CSS中常用中文字体的Unicode编码
- 让记事本默认以Unicode或UTF-8编码保存避免ANSI乱码的方法技巧
- 黄聪:python访问抓取网页常用命令(保存图片到本地、模拟POST、GET、中文编码问题)
- Python 中的编码转换 编码方案 中文解码 gbk , utf ,unicode
- 解决php函数json_encode转换后中文被编码为unicode
- 中文信息转Unicode编码
- WindowsPhone的中文GB2312、GBK编码与Unicode相互转换
- 使用 Java 将中文字符转换成 Unicode 编码
- 汉字编码(【Unicode】 【UTF-8】 【Unicode与UTF-8之间的转换】 【汉字 Unicode 编码范围】【中文标点Unicode码】【GBK编码】【批量获取汉字UNICODE码】)
- php 中文unicode编码问题
- QT4.6中文GB2312与utf-8和unicode及URL encoding %编码汇总
- 中文字符集编码Unicode,gb2312,cp936,GBK,GB18030
- 中文在unicode中的编码范围