您的位置:首页 > 编程语言 > Java开发

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  cookie cookies java