cookie中保存中文

news/2024/5/18 12:40:38 标签: cookie

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

Cookie中保存中文

  Cookie中保存中文,次要。

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

  向客户端响应中添加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);

                     }

              }     

       }

}


http://www.niftyadmin.cn/n/1564415.html

相关文章

Windows Phone实用开发技巧(37):创建一个全局播放器

在做windows phone 开发的时候&#xff0c;有时候我们需要在某些页面中进行声音的播放。而播放的方式也有多种&#xff1a; 1. 使用MediaElement 2. 使用SoundEffect 3. 使用后台播放 SoundEffect只能播放wav格式的文件&#xff1b;后台播放可以在程序退出后仍然继续播放&#…

cookie的禁用

------------------------------- cookie的禁用------------------------------------- Cookie的禁用处理 默认情况下浏览器的cookie是被启用的&#xff0c;但是其实我们是可以手动的禁用cookie的&#xff0c;强烈不建议禁用cookie Cookie一旦被禁用掉绝大多数互联网的网站都…

win7 命令行 utf-8 汉字乱码

为什么80%的码农都做不了架构师&#xff1f;>>> 以前用命令行的时候也经常遇到这个问题&#xff0c;貌似从没想过纠正一下&#xff0c;看到第九街上的这篇文章特别收藏一下。 在dos下执行一个PHP文件的时候&#xff0c;由于输出文件文字是UTF8编码&#xff0c;所以…

JSP的概述

---------------------------JSP的概述------------------------------------------ JSP概述 JavaWeb早期&#xff1a;applet servlet JavaWeb早期&#xff1a;servlet&#xff0c;在servlet中需要写大量的response.getWriter().println(“<html>”); JavaWeb后期&am…

JSP的scriptlet

----------------------------------- JSP的scriptlet------------------------------- JSP的scriptlet 从单词上分析script是脚本&#xff1a;let在JAVA中表示的小程序&#xff0c;scriptlet表示脚本小程序。 JSP中的Java代码块就是最常见的动态信息。它分为三种&#xff1a;…

SSM项目从零开始到入门020-mybatis的choose(when, otherwise)标签的用法

有时候我们并不想应用所有的条件&#xff0c;而只是想从多个选项中选择一个。而使用if标签时&#xff0c;只要test中的表达式为 true&#xff0c;就会执行 if 标签中的条件。MyBatis 提供了 choose 元素。if标签是与(and)的关系&#xff0c;而 choose 是或(or)的关系。choose标…

JSP的注释

-------------------JSP的注释----------------------------- JSP注释 语法&#xff1a;<%-- … --%> 快捷键ctrlshift/,取消注释ctrlshift\ 其中JSP只有一种注释&#xff1a;<%-- … --%>&#xff0c;注释中的内容会被JSP编译系统忽略&#xff01; <%-- &…

open flash chart-9 (说明)

2019独角兽企业重金招聘Python工程师标准>>> 原文注明不让转贴&#xff0c;但个人觉得对于初学open flash chart的人来说&#xff0c;这篇文章实在是太有用了&#xff0c;本人从中受益良多&#xff0c;在此对原作者表示感谢&#xff0c;为了让更多的人获益&#xff…