Servlet Cookie 处理需要对中文进行编码与解码
String str = java.net.URLEncoder.encode("中文","UTF-8"); //编码
String str = java.net.URLDecoder.decode("编码后的字符串","UTF-8"); // 解码
Cookie cookie = new Cookie("key","value");
[ ] ( ) = , " / ? @ : ;创建的时候一定不要在key或者value包含空格和左边所有的字符
response.addCookie(cookie);
通过 Servlet 删除 Cookie
ookie cookie = null;
Cookie[] cookies = null;
// 获取与该域相关的 Cookie 的数组
cookies = request.getCookies();
// 设置响应内容类型
response.setContentType("text/html;charset=UTF-8");
for (int i = 0; i < cookies.length; i++){
cookie = cookies[i];
if((cookie.getName( )).compareTo("url") == 0 ){
cookie.setMaxAge(0);
response.addCookie(cookie);
out.print("已删除的 cookie:" +
cookie.getName( ) + "<br/>");
}
out.print("名称:" + cookie.getName( ) + ",");
out.print("值:" + cookie.getValue( )+" <br/>");
}