javaEE cookie保存中文时报错
来源:互联网 发布:软件测试管理体系 编辑:程序博客网 时间:2024/04/29 20:49
做练习的时候老是出错:
The server encountered an internal error that prevented it from fulfilling this request.
源码如下:
for (int index = 0; index < cookies.length; index++) { if(cookies[index].getName().equals("lastAccessDate")) { lastAccessDate =cookies[index].getValue(); nowAccessDate = (nowDate.getYear() + 1900) + "年" + (nowDate.getMonth() + 1) + "月" + nowDate.getDate() + "日" + nowDate.getHours() + "时" + nowDate.getMinutes() + "分" + nowDate.getSeconds() + "秒"; oneCookie = new Cookie("lastAccessDate", nowAccessDate); oneCookie.setMaxAge(24 * 60 * 60); response.addCookie(oneCookie); break;
错误发生在:response.addCookie(oneCookie);
原来是编码格式不正确,cookie不能保存中文,把上面句子中的 年 月日 时分秒 改成英文就不会报错了
若是一定要保存为中文,则把中文转化为UTF-8字符串就可以保存了:
保存的时候用java.net.URLEncoder.encode(String,”UTF-8”)编码
取回来的时候用java.net.URLDecoder.decode(String,”UTF-8”)解码
全部源码如下:
“`
<%@page import=”org.apache.naming.java.javaURLContextFactory”%>
<%@page import=”java.util.Date”%>
<%@ page pageEncoding=”UTF-8”%>
cookie
<%!String lastAccessDate = null;
String nowAccessDate = null;
Cookie oneCookie = null;
Cookie[] cookies = null;
Date nowDate = null;%>
<% cookies = request.getCookies(); nowDate = new Date(); if (cookies == null) { lastAccessDate = (nowDate.getYear() + 1900) + "年" + (nowDate.getMonth() + 1) + "月" + nowDate.getDate() + "日" + nowDate.getHours() + "时" + nowDate.getMinutes() + "分" + nowDate.getSeconds() + "秒"; lastAccessDate = java.net.URLEncoder.encode(lastAccessDate,"UTF-8"); //编码 oneCookie = new Cookie("lastAccessDate", lastAccessDate); oneCookie.setMaxAge(24 * 60 * 60); response.addCookie(oneCookie); out.println("cookie:" + oneCookie.toString()); } else { for (int index = 0; index < cookies.length; index++) { if (cookies[index].getName().equals("lastAccessDate")) { lastAccessDate = cookies[index].getValue(); lastAccessDate = java.net.URLDecoder.decode(lastAccessDate,"UTF-8"); //解码 nowAccessDate = (nowDate.getYear() + 1900) + "年" + (nowDate.getMonth() + 1) + "月" + nowDate.getDate() + "日" + nowDate.getHours() + "时" + nowDate.getMinutes() + "分" nowAccessDate = java.net.URLEncoder.encode(nowAccessDate,"UTF-8"); //编码 oneCookie = new Cookie("lastAccessDate", nowAccessDate); oneCookie.setMaxAge(24 * 60 * 60); response.addCookie(oneCookie); break; } } } out.print("您上次访问本系统的时间是在:" + lastAccessDate);%>
0 0
- javaEE cookie保存中文时报错
- Cookie保存中文 编码
- Cookie保存中文
- cookie保存中文时异常
- Cookie不能保存中文解决方案
- 解决保存Cookie中带有中文会报错问题
- javaee cookie
- javaEE cookie
- JavaEE Cookie
- 实体重复保存时报错
- jsp中Cookie保存中文的问题
- Servlet --cookie编码保存中文信息
- PHPEXcel保存到本地时报错
- MyEclipse里保存文件时报错
- javaEE:day10-Cookie技术
- [JavaEE笔记]Cookie
- javaee(cookie和session)
- 【JavaEE】cookie和Session
- HDU 5877 Weak Pair(dfs + 树状数组 + 离散化)
- 7.22 T hdu1102 Constructing Roads
- ImageLoader
- innerText和innerHtml的区别
- 2016 ICPC 大连网络赛 HDU 5873 Football Games
- javaEE cookie保存中文时报错
- 华为交换机堆叠和集群配置
- 将本地代码上传到github
- Andorid中UI控件的详细介绍(四)——EditText
- 16读取通话记录
- 放肆的使用UIBezierPath和CAShapeLayer画各种图形(含仿微信视频眼镜Demo)
- 程序员必学,英文构词前缀
- Po学校Lesson12
- Linux网络配置