Illegalargumentexception error when adding cookie to response

Many people often make this mistake when learning cookies. Someone asked me this question today, so I will share with you the solution to this problem (there is no specific solution on the Internet).
error code:

Cookie cookie=new Cookie("name","value value2");
response.addCookie(cookie);

Error
Java. Lang. IllegalArgumentException: An invalid character [32] was present in the Cookie value
Error code:

Cookie c=new Cookie("name","value,value2");
response.addCookie(c);

Error:
Java. Lang. IllegalArgumentException: An invalid character [44] was present in the Cookie value

the exception can be seen that this problem belongs to the invalid parameters, looking at the back of the prompt content
the An invalid character [44] was present in the Cookie value
cookies are invalid characters in character [44], so we query ASCII code is 44 said
“, “said 32 Spaces so we only need to replace the value of the corresponding characters, or coding


Read More: