解决java.lang.IllegalArgumentException: An invalid character [32] was present in the Cookie value错误

解决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的一个示例,模拟网站回显用户上次访问时间,代码如下:


   
  1. package cn.jkdev;
  2. import java.io.IOException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Date;
  5. import javax.servlet.ServletException;
  6. import javax.servlet.annotation.WebServlet;
  7. import javax.servlet.http.Cookie;
  8. import javax.servlet.http.HttpServlet;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11. @WebServlet( "/HistServlet")
  12. public class HistServlet extends HttpServlet {
  13. public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  14. response.setContentType( "text/html;charset=utf-8");
  15. // 获取当前时间
  16. SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss");
  17. String curTime = format.format( new Date());
  18. // 取得cookie
  19. Cookie[] cookies = request.getCookies();
  20. String lastTime = null;
  21. if (cookies != null) {
  22. for (Cookie cookie : cookies) {
  23. if (cookie.getName().equals( "lastTime")) {
  24. // 第n次访问
  25. // 有lastTime的cookie
  26. lastTime = cookie.getValue(); // 上次访问的时间
  27. // 1.把上次显示时间显示到浏览器
  28. response.getWriter().write( "欢迎回来,你上次访问的时间为:" + lastTime + ",当前时间为:" + curTime);
  29. // 2.更新cookie
  30. cookie.setValue(curTime.trim());
  31. cookie.setMaxAge( 3600);
  32. // 3.把更新后的cookie发送到浏览器
  33. response.addCookie(cookie);
  34. break;
  35. }
  36. }
  37. }
  38. /**
  39. * 第一次访问(没有cookie 或 有cookie,但没有名为lastTime的cookie)
  40. */
  41. if (cookies == null || lastTime == null) {
  42. // 1.显示当前时间到浏览器
  43. response.getWriter().write( "你是首次访问本网站,当前时间为:" + curTime);
  44. // 2.创建Cookie对象
  45. Cookie cookie = new Cookie( "lastTime", curTime);
  46. cookie.setMaxAge( 3600);
  47. // 3.把cookie发送到浏览器保存
  48. response.addCookie(cookie);
  49. }
  50. }
  51. }

当我在浏览器测试,多次点击后,依然如图下图显示效果


令人费解,于是看控制台输出信息,发现以下错误:

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)读取的时候进行解码:


   
  1. response.getWriter().write( "欢迎回来,你上次访问的时间为:" + URLDecoder.decode(lastTime, "utf-8") + ",当前时间为:"
  2. + 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="{&quot;mod&quot;:&quot;popu_824&quot;}"><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="{&quot;mod&quot;:&quot;1582594662_002&quot;}"><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="{&quot;mod&quot;:&quot;popu_379&quot;}" 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="{&quot;mod&quot;:&quot;popu_379&quot;}">关注</a>
                                    </div>
                            </div>
                    </div>
    
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值