【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 value

报错原因:

Tomcat 8.5版本,在cookie值中不能使用空格。

代码:

 1 protected void doGet(HttpServletRequest request, HttpServletResponse response)
 2             throws ServletException, IOException {
 3
 4         Date date = new Date();
 5         SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
 6         String currentTime = format.format(date);
 7
 8         Cookie accessTime = new Cookie("lastAccessTime",currentTime);
 9         accessTime.setMaxAge(60*50);
10         response.addCookie(accessTime);
11
12         String lastAccessTime=null;
13         Cookie[] cookies = request.getCookies();
14         if (cookies != null) {
15             for(Cookie cookie:cookies) {
16                 if(cookie.getName().equals("laseAccessTime")) {
17                     lastAccessTime=cookie.getValue();
18                 }
19             }
20         }
21
22         if(lastAccessTime==null) {
23             response.setContentType("text/html;charset=UTF-8");
24             response.getWriter().write("您是第一次访问");
25             System.out.println(lastAccessTime);
26         }
27         else {
28             response.getWriter().write("您上次访问的时间是:"+lastAccessTime);
29         }
30
31     }

 

问题出在这里:日期格式有空格,换成/即可

原文地址:https://www.cnblogs.com/musecho/p/11237165.html

时间: 2024-10-24 11:50:18

【Cookie】java.lang.IllegalArgumentException An invalid character [32] was present in the Cookie value的相关文章

cookie设置日期时间有空格报错:java.lang.IllegalArgumentException: An invalid character [32] was present in the Cookie value

rt,代码及报错如下: java.lang.IllegalArgumentException: An invalid character [32] was present in the Cookie value 现暂时将"yyyy-MM-dd hh:mm:ss"中的空格用#替代,就能正常显示时间日期了...

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

今天在练习 cookie时意外的报了这个错. java.lang.IllegalArgumentException: An invalid character [32] was present in the Cookie value1这句话的意思是 一个不识别的字符[32]出现在了cookie当中 由于tomcat的版本比较高,所以在addCookie时是不能使用空格的 而在ASCII码中32对应的就是空格.只要把后台代码中的空格删掉就可以了. 我的代码:是在cookie中添加了时间,而时间的格

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 c

An invalid character [32] was present in the Cookie value

HTTP Status 500 – Internal Server Error Type Exception Report Message An invalid character [32] was present in the Cookie value Description The server encountered an unexpected condition that prevented it from fulfilling the request. Exception java.l

【异常】java.lang.IllegalArgumentException: Cannot locate declared field class org.apache.http.impl.client.HttpClientBuilder.dnsResolver 的解决方案

起因:使用htmlunit爬取外部网页的时候遇到这个异常:java.lang.IllegalArgumentException: Cannot locate declared field class org.apache.http.impl.client.HttpClientBuilder.dnsResolver <dependency> <groupId>net.sourceforge.htmlunit</groupId> <artifactId>html

Spring Boot报错:java.lang.IllegalArgumentException: An invalid character [..] was present in the Cookie value

原因及解决办法:https://docs.spring.io/spring-boot/docs/2.0.3.RELEASE/reference/htmlsingle/#howto-use-tomcat-legacycookieprocessor @Configuration public class CookieConfig { /** * 解决问题: * There was an unexpected error (type=Internal Server Error, status=500)

【已解决】java.lang.IllegalArgumentException: column &#39;_id&#39; does not exist

今天用到SimpleCursorAdapter来适配ListView的数据,依旧记得SimpleCursorAdapter必须匹配一个字段名为_id的主键列,一开始我用SQLiteDatabase的rawQuery方法拼sql查询,因为是用SELECT * FROM table_name,所以一切正常. 后来我用query方法就挂了,抛出了java.lang.IllegalArgumentException: column '_id' does not exist异常,然后我发现原来是参数col

java.lang.IllegalArgumentException: An invalid domain [.test.com] was specified for this cookie

https://blog.csdn.net/cml_blog/article/details/52135115 当项目中使用单点登录功能时,通常会使用cookie进行信息的保存,这样就可以在多个子域名上存取用户信息. 比如有三个domain分别为test.com,cml.test.com,b.test.com这三个域名下的cookie是需要互相访问的.这时会在response上写入cookie信息 Cookie cookie = new Cookie("testCookie", &qu

【Exception】 java.lang.NoSuchMethodError: android.app.AlertDialog$Builder.setOnDismissListener

f(Build.VERSION.SDK_INT >10) builder =newAlertDialog.Builder(getActivity(), R.style.Theme.Sherlock.Dialog);else builder =newAlertDialog.Builder(getActivity());AlertDialog.Builder builder;try{ builder =newAlertDialog.Builder(getActivity(), R.style.The