解决java.lang.IllegalArgumentException: An invalid character [32] was present in the Cookie value错误
转发原文网址:https://blog.csdn.net/qq_34330286/article/details/79591202
问题阐述:
最近在学习Java Web,今天使用Eclipse编写一个Cookie的一个示例,模拟网站回显用户上次访问时间,代码如下:
-
package cn.jkdev;
-
-
import java.io.IOException;
-
import java.text.SimpleDateFormat;
-
import java.util.Date;
-
-
import javax.servlet.ServletException;
-
import javax.servlet.annotation.WebServlet;
-
import javax.servlet.http.Cookie;
-
import javax.servlet.http.HttpServlet;
-
import javax.servlet.http.HttpServletRequest;
-
import javax.servlet.http.HttpServletResponse;
-
-
@WebServlet(
"/HistServlet")
-
public
class HistServlet extends HttpServlet {
-
-
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-
response.setContentType(
"text/html;charset=utf-8");
-
-
// 获取当前时间
-
SimpleDateFormat format =
new SimpleDateFormat(
"yyyy-MM-dd hh:mm:ss");
-
String curTime = format.format(
new Date());
-
-
// 取得cookie
-
Cookie[] cookies = request.getCookies();
-
String lastTime =
null;
-
if (cookies !=
null) {
-
for (Cookie cookie : cookies) {
-
if (cookie.getName().equals(
"lastTime")) {
-
// 第n次访问
-
// 有lastTime的cookie
-
lastTime = cookie.getValue();
// 上次访问的时间
-
// 1.把上次显示时间显示到浏览器
-
response.getWriter().write(
"欢迎回来,你上次访问的时间为:" + lastTime +
",当前时间为:" + curTime);
-
// 2.更新cookie
-
cookie.setValue(curTime.trim());
-
cookie.setMaxAge(
3600);
-
// 3.把更新后的cookie发送到浏览器
-
response.addCookie(cookie);
-
break;
-
}
-
}
-
}
-
-
/**
-
* 第一次访问(没有cookie 或 有cookie,但没有名为lastTime的cookie)
-
*/
-
if (cookies ==
null || lastTime ==
null) {
-
// 1.显示当前时间到浏览器
-
response.getWriter().write(
"你是首次访问本网站,当前时间为:" + curTime);
-
// 2.创建Cookie对象
-
Cookie cookie =
new Cookie(
"lastTime", curTime);
-
cookie.setMaxAge(
3600);
-
// 3.把cookie发送到浏览器保存
-
response.addCookie(cookie);
-
}
-
}
-
}
当我在浏览器测试,多次点击后,依然如图下图显示效果
令人费解,于是看控制台输出信息,发现以下错误:
java.lang.IllegalArgumentException: An invalid character [32] was present in the Cookie value
原来如此,Cookie的值是非法的,非法字符是character [32],于是我找到对应的ASCII码表,如下图,character [32]对应的是一个空格,这样是不可以的。
解决办法:
1.在构造Date字符串的时候不要使用空格,使用其他字符来替换,这种方法比较简单。
2.如果非要使用空格来隔开日期与时间,我们需要对其进行编码就可以了,但是读取的时候不要忘记了解码,不然在用户界面上会出现乱码。
(1)对Date字符串进行编码:
String curTime = URLEncoder.encode(format.format(new Date()), "utf-8");
(2)读取的时候进行解码:
-
response.getWriter().write(
"欢迎回来,你上次访问的时间为:" + URLDecoder.decode(lastTime,
"utf-8") +
",当前时间为:"
-
+ URLDecoder.decode(curTime,
"utf-8"));
好了,再刷新浏览器两次,正常显示想要的效果的:
<li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true"> <use xlink:href="#csdnc-thumbsup"></use> </svg><span class="name">点赞</span> <span class="count">2</span> </a></li> <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{"mod":"popu_824"}"><svg class="icon" aria-hidden="true"> <use xlink:href="#icon-csdnc-Collection-G"></use> </svg><span class="name">收藏</span></a></li> <li class="tool-item tool-active is-share"><a href="javascript:;" data-report-click="{"mod":"1582594662_002"}"><svg class="icon" aria-hidden="true"> <use xlink:href="#icon-csdnc-fenxiang"></use> </svg>分享</a></li> <!--打赏开始--> <!--打赏结束--> <li class="tool-item tool-more"> <a> <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg> </a> <ul class="more-box"> <li class="item"><a class="article-report">文章举报</a></li> </ul> </li> </ul> </div> </div> <div class="person-messagebox"> <div class="left-message"><a href="https://blog.csdn.net/qq_34330286"> <img src="https://profile.csdnimg.cn/5/7/C/3_qq_34330286" class="avatar_pic" username="qq_34330286"> <img src="https://g.csdnimg.cn/static/user-reg-year/2x/4.png" class="user-years"> </a></div> <div class="middle-message"> <div class="title"><span class="tit"><a href="https://blog.csdn.net/qq_34330286" data-report-click="{"mod":"popu_379"}" target="_blank">极客开发者</a></span> </div> <div class="text"><span>发布了49 篇原创文章</span> · <span>获赞 69</span> · <span>访问量 7万+</span></div> </div> <div class="right-message"> <a href="https://im.csdn.net/im/main.html?userName=qq_34330286" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信 </a> <a class="btn btn-sm bt-button personal-watch" data-report-click="{"mod":"popu_379"}">关注</a> </div> </div> </div>


5184

被折叠的 条评论
为什么被折叠?



