tomcat-8.5.24 加含双引号的cookie 报错,tomcat-7.0.85 OK

商城购物车的一个方法(代码见最后一段),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";
	}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值