商城购物车的一个方法(代码见最后一段),response 添加cookie 这行会报错:
java.lang.IllegalArgumentException: An invalid character [34] was present in the Cookie value
"34" 是ASCII 编码,表示一个双引号;JSON 是正确的格式;换了7.0.85 版本的tomcat 之后就可以正常运行了。
stackflow 上找到这个答案,尚未尝试是否OK。
CookieProcessor is a new configuration element, introduced in Tomcat 8.0.15. The CookieProcessor element allows different cookie parsing configuration in each web application, or globally in the default conf/context.xml file.
According to official docs at Apache Tomcat 8 Configuration Reference Version 8.0.47 :
The standard implementation of CookieProcessor is:org.apache.tomcat.util.http.LegacyCookieProcessor. Note that it is anticipated that this will change to org.apache.tomcat.util.http.Rfc6265CookieProcessor in a future Tomcat 8 release.
Later..
According to official docs at Apache Tomcat 8 Configuration Reference Version 8.5.23
The standard implementation of CookieProcessor is org.apache.tomcat.util.http.Rfc6265CookieProcessor
To resolve this issue: add this line in conf/context.xml at location %CATALINA_HOME% (i.e. C:\apache-tomcat-8.5.20\conf\context.xml in my case):
<CookieProcessor className="org.apache.tomcat.util.http.LegacyCookieProcessor" />
This is how it looks like after adding:
<?xml version="1.0" encoding="UTF-8"?>
<Context reloadable="true">
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
<Transaction factory="bitronix.tm.BitronixUserTransactionObjectFactory"/>
<CookieProcessor className="org.apache.tomcat.util.http.LegacyCookieProcessor" />
</Context>
stackoverflow 链接:https://stackoverflow.com/questions/46686018/java-lang-illegalargumentexceptionan-invalid-character-34-was-present-in-the
public String addShopCar(Caritem caritem, HttpServletRequest request, HttpServletResponse response){
List<Caritem> clist = getShopCarByCookie(request);
boolean flag = false;
for (Caritem ct : clist) {
if(Objects.equals(ct.getGid(), caritem.getGid())){
ct.setGnumber(ct.getGnumber() + caritem.getGnumber());
flag = true;
break;
}
}
if(!flag){
clist.add(caritem);
}
String json = JSONUtil.parseJSONArray(clist);
Cookie cookie = new Cookie(SHOPCAR_COOKIE_NAME, json);
cookie.setMaxAge(180*24*60*60);
response.addCookie(cookie);
return "redirect:front/addshopcarsucc.jsp";
}