java中Cookie使用问题(message:invalid character [32] was present in the Cookie value)
1、 问题描述
Servlet中执行下面一段代码:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
System.out.println( new Date().toString());
Cookie cookie = new Cookie("lasttime", new Date().toString());
response.addCookie(cookie);
String s = "欢迎您首次访问该网站!~";
Cookie[] cookies = request.getCookies();
if (cookies != null)
for (Cookie cs : cookies) {
if (cs.getName().equals("lasttime")) {
s = "您上次登录的时间为:" + cs.getValue().replace("-", " ");
}
}
response.getWriter().print(s);
}
抛出如下异常:

2、 追根溯源
出现上述问题觉得很奇怪,因为程序编译通过,至少证明没有语法错误,根据编译器提示,定位问题到:
Cookie cookie = new Cookie("lasttime", new Date().toString());
response.addCookie(cookie);
查看JAVAEE-API,发现有如下

回过去看代码,发现
输入: System.out.println( new Date().toString());
输出: Fri Apr 20 21:56:39 CST 2018
很明显输出字符串中存在 空格 ,所以程序会报错,存在无效字符。
3、解决方案
解决问题的方法其实很简单,只要字符串中不存在空格即可成功,下面将给出几种具体的解决办法,程序修改如下:
法一
思路:用“-”代替“ ”,之后记得换回来即可,程序成功运行。
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
System.out.println( new Date().toString());
Cookie cookie = new Cookie("lasttime", new Date().toString().replace(" ", "-"));
cookie.setMaxAge(60*60);
response.addCookie(cookie);
String s = "欢迎您首次访问该网站!~";
Cookie[] cookies = request.getCookies();
if (cookies != null)
for (Cookie cs : cookies) {
if (cs.getName().equals("lasttime")) {
s = "您上次登录的时间为:" + cs.getValue().replace("-", " ");
}
}
response.getWriter().print(s);
}
法二
思路:进行URL编码,然后把编码后的字符串放到Cookie中,之后进行URL解码即可。
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
System.out.println( new Date().toString());
Cookie cookie = new Cookie("lasttime", URLEncoder.encode(new Date().toString(), "UTF-8"));
cookie.setMaxAge(*);
response.addCookie(cookie);
String s = "欢迎您首次访问该网站!~";
Cookie[] cookies = request.getCookies();
if (cookies != null)
for (Cookie cs : cookies) {
if (cs.getName().equals("lasttime")) {
s = "您上次登录的时间为:" + URLDecoder.decode(cs.getValue(), "UTF-8");
}
}
response.getWriter().print(s);
}
程序运行正常,符合预期值。
java中Cookie使用问题(message:invalid character [32] was present in the Cookie value)的更多相关文章
- 【Cookie】java.lang.IllegalArgumentException An invalid character [32] was present in the Cookie value
创建时间:6.30 java.lang.IllegalArgumentException: An invalid character [32] was present in the Cookie va ...
- cookie实例---显示上一次访问的时间与java.lang.IllegalArgumentException: An invalid character [32] was present in the Cookie value
创建Cookie,名为lasttime,值为当前时间,添加到response中: 在A.jsp中获取请求中名为lasttime的Cookie: 如果不存在输出“您是第一次访问本站”,如果存在输出“您上 ...
- 异常java.lang.IllegalArgumentException: An invalid character [32] was present in the Cookie value
通过HttpServletResponse的addCookie(Cookie cookie)向客户端写cookie信息,这里使用的tomcat版本是8.5.31,出现如下报错: java.lang.I ...
- An invalid character [32] was present in the Cookie value 错误
今天在做cookie部分的demo的时候出现了一个错误Servlet部分的代码如下 Date data=new Date(); SimpleDateFormat format=new SimpleDa ...
- An invalid character [32] was present in the Cookie value
系统安装Tomcat版本为:tomcat8,登录时报错"An invalid character [32] was present in the Cookie value" 处理方 ...
- java.lang.IllegalArgumentException: An invalid character [34] was present in the Cookie value
java.lang.IllegalArgumentException: An invalid character [34] was present in the Cookie value at org ...
- exception 'DOMException' with message 'Invalid Character Error' Php + Mongodb
问题描述: 项目属于MVC设计模式,技术和框架采用了php5.6 + Yii2.0 + MongoDB. 在我从Controller中调用Model 的 findAll([]) 方法获取数据打印到屏幕 ...
- Eclipse 中 Syntax error on token "Invalid Character", delete this token 的解决
eclipse中遇到了Syntax error on token "Invalid Character", delete this token(令牌“无效字符”上的语法错误,删除此 ...
- 理解Java中的字符串类型
1.Java内置对字符串的支持: 所谓的内置支持,即不用像C语言通过char指针实现字符串类型,并且Java的字符串编码是符合Unicode编码标准,这也意味着不用像C++那样通过使用string和w ...
随机推荐
- [LOJ2271] [SDOI2017] 遗忘的集合
题目链接 LOJ:https://loj.ac/problem/2271 洛谷:https://www.luogu.org/problemnew/show/P3784 BZOJ太伤身体死活卡不过还是算 ...
- HNOI2013旅行
一道欺负我智商的题... 本来想打单调队列优化dp的,结果看到算法标签就点了此题 洛谷题面 首先你要理解题意,蒟蒻理解了好久.它就是说,给你一个由1和-1组成的数列,让你分成m段,并让这m段区间和最大 ...
- jsp中的js中获取项目路径的方法
在jsp中加上 <% String path = request.getContextPath(); String basePath = request.getScheme()+":/ ...
- Chapter3 (字符串,向量,数组) --C++Prime笔记
1.using用法:using namespace ::name;注意事项:一般不在头文件使用using否则很容易导致运用命名空间不对错误. 2.string的方法: ①getline(输入流,str ...
- django中的request对象详解
Request 我们知道当URLconf文件匹配到用户输入的路径后,会调用对应的view函数,并将 HttpRequest对象 作为第一个参数传入该函数. 我们来看一看这个HttpRequest对 ...
- 基于索引的MySQL优化
今天查看MySQL慢查询日志,查看一个四表关联的SQL操作,耗时1006s.这次也是基于基于子查询的思路,对上表进行优化.使时间复杂度降到n^2级别.但优化之后时间反而是原来的三倍多. 原SQL语句: ...
- 科学计算三维可视化---TVTK入门(安装与测试)
推文:http://docs.huihoo.com/scipy/scipy-zh-cn/tvtk_intro.html 推文:http://code.enthought.com/pages/mayav ...
- Vuex深入理解
store下的index.js: import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) let store = new Vuex.St ...
- 洛谷P2766 最长递增子序列问题
https://www.luogu.org/problemnew/show/P2766 注:题目描述有误,本题求的是最长不下降子序列 方案无限多时输出 n 网络流求方案数,长见识了 第一问: DP 同 ...
- 何凯文每日一句打卡||DAY1~DAY3
01长难句 In an open meeting with congressional Democrats and Republicans, Trump embraced raising the ag ...