cookie中保存中文

时间:2024-05-31 12:29:04

-------------------------------- cookie中保存中文--------------------------------

Cookie中保存中文

  Cookie中保存中文,次要。

Cookie中是不可以设置中文的,但可以使用URLEncodor.encode()方法编码后在存放到Cookie中。在获取Cookie时,需要先使用URLDecoder.decode()方法解码,再使用。

cookie中保存中文

  向客户端响应中添加Cookie

       String name = URLEncoder.encode("姓名", "UTF-8");

       String value = URLEncoder.encode("张三", "UTF-8");

       Cookie c = new Cookie(name, value);

       c.setMaxAge(3600);

       response.addCookie(c);

 

从客户端请求中获取Cookie

       response.setContentType("text/html;charset=utf-8");

       Cookie[] cs = request.getCookies();

       if(cs != null) {

           for(Cookie c : cs) {

              String name = URLDecoder.decode(c.getName(), "UTF-8");

              String value = URLDecoder.decode(c.getValue(), "UTF-8");

               String s = name + ": " + value + "<br/>";

              response.getWriter().print(s);

           }

       }

 

代码示例:

存储中文cookie

package com.rl.cookie;

 

import java.io.IOException;

import java.io.PrintWriter;

import java.net.URLEncoder;

 

import javax.servlet.ServletException;

import javax.servlet.http.Cookie;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

public class RegistServlet extends HttpServlet {

 

       @Override

       protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

              doPost(req, resp);

       }

      

       public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

              request.setCharacterEncoding("UTF-8");

              String userpass = "李昆鹏,123";

              //编码用URLEncoder.encode

              userpass = URLEncoder.encode(userpass,"UTF-8");

              //创建cookie对象,值使用中文存储

              //cooike中存储中文会报错

              Cookie cookie = new Cookie("userpass", userpass);

              //设置cookie的存活时间,里面的参数是以秒为单位

              cookie.setMaxAge(60*60);

              cookie.setPath("/cookie_demo4/");

              //把cookie写入浏览器

              response.addCookie(cookie);

              response.getWriter().print("success");

       }

 

}

 

取出中文cookie

public class Regist1Servlet extends HttpServlet {

 

       @Override

       protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

              doPost(req, resp); 

       }

      

       public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

              //从浏览器中来读取当前项目的所有cookie

              Cookie[] cookies = request.getCookies();

              String userpassVal = null;

              //遍历cookies

              for (Cookie cookie : cookies) {

                     //获取cookie的name

                     String cookieName = cookie.getName();

                     if("userpass".equals(cookieName)){

                            //获得Base64编码后的字符串

                            userpassVal = cookie.getValue();

                            System.out.println(userpassVal);

                            //解码用URLDecoder.decode

                            userpassVal = URLDecoder.decode(userpassVal,"UTF-8");

                            System.out.println(userpassVal);

                     }

              }     

       }

}

cookie中保存中文