商城購物車的一個方法(代碼見最後一段),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-thepublic 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";
}