Cookie的值保存中文内容,可以使用Java.net.URLDecoder进行解码。
示例:
<%@page import="java.net.URLDecoder"%> <%@ 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>Insert title here</title> </head> <body> <% //保存一个中文值得Cookie Cookie c = new Cookie("cnName" , java.net.URLEncoder.encode("张三" , "utf-8")); c.setMaxAge(24 * 3600); //向客户端增加Cookie对象 response.addCookie(c); //获取客户端的Cookie,并输出 Cookie [] cookies = request.getCookies(); for (Cookie cookie : cookies){ if(cookie.getName().equals("cnName")){ out.println( java.net.URLDecoder.decode(cookie.getValue() , "utf-8")); } } %> </body> </html>
时间: 2024-11-03 21:21:20