tomcat使用cookies缓存的时候中文报错解决办法 java.lang.IllegalArgumentException: Control character in cookie value or attribute.
报错出现
java.lang.IllegalArgumentException: Control character in cookie value or attribute.
at org.apache.tomcat.util.http.CookieSupport.isHttpSeparator(CookieSupport.java:205)
at org.apache.tomcat.util.http.CookieSupport.isHttpToken(CookieSupport.java:229)
at org.apache.tomcat.util.http.ServerCookie.appendCookieValue(ServerCookie.java:185)
at org.apache.catalina.connector.Response.generateCookieString(Response.java:1054)
at org.apache.catalina.connector.Response.addCookie(Response.java:996)
at org.apache.catalina.connector.ResponseFacade.addCookie(ResponseFacade.java:380)
at cn.lonecloud.CookieServlet.doGet(CookieServlet.java:18)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:442)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1082)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:623)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
该报错是由于这个中文的编码的问题中文采用的是unicode编码,而英文采用的是ASCII编码,所以当COOkie保存中文的时候需要对中文进行编码,而且从Cookie中取出内容的时候也要进行解码,编码和解码可以使用
URLEncoder.encode(name, "utf-8"); URLDecoder.decode(cookies[i].getName(),"utf-8");
示例:
CookieServlet.java 生成cookies
package cn.lonecloud; import java.io.IOException;
import java.net.URLEncoder; import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class CookieServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//该字符含有中文
String s="测试cookies";
String encode = URLEncoder.encode(s,"UTF-8");
Cookie cookie=new Cookie("Hello", encode);
response.addCookie(cookie);
response.getWriter().append("dssd");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
GetCookies.java 获取cookies
package cn.lonecloud; import java.io.IOException;
import java.net.URLDecoder; import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class GetCookies extends HttpServlet { @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Cookie[] cookies = req.getCookies();
for (Cookie cookie : cookies) {
String value = cookie.getValue();
String decode = URLDecoder.decode(value, "UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.getWriter().append(decode);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
}
}
web.xml 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>CookieServlet</servlet-name>
<servlet-class>cn.lonecloud.CookieServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>CookieServlet</servlet-name>
<url-pattern>/CookieServlet</url-pattern>
</servlet-mapping> <servlet>
<servlet-name>getCookies</servlet-name>
<servlet-class>cn.lonecloud.GetCookies</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>getCookies</servlet-name>
<url-pattern>/getCoookies</url-pattern>
</servlet-mapping> </web-app>
tomcat使用cookies缓存的时候中文报错解决办法 java.lang.IllegalArgumentException: Control character in cookie value or attribute.的更多相关文章
- Cookie存储中文报错:java.lang.IllegalArgumentException: Control character in cookie value or attribute.(转)
项目中做自动登录和保存密码时,Cookie报错Java.lang.IllegalArgumentException,上google查了下 在http://hi.baidu.com/xtxycy/blo ...
- 在浏览器访问Tomcat的时候报错java.lang.IllegalArgumentException: Control character in cookie value or attribute.
出现这种情况的原因就是因为cookie中存在中文或者特殊符号造成的,应为Tomcat7不支持 解决方法: 1,首先必须先删除本地的cookie,否则项目无法访问 2,然后调整编码就行了
- 解决Tomcat的IllegalArgumentException: Control character in cookie value or attribute错误
接口中带有中文,tomcat8 17-Apr-2019 13:21:23.734 严重 [http-nio-8082-exec-2] org.apache.coyote.http11.Abstract ...
- springboot整合mybatis的时候报错Caused by: java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
今天闲来无事,学习springboot整合mybatis,在bilibili看视频学的,视频中在dao层的interface上面加上org.apache.ibatis.annotations.Mapp ...
- springcloud中config启动时候报错Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'config.info' in value "${config.info}"
-noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dcom.sun.management.jm ...
- spring cloud报错解决:java.lang.ClassNotFoundException: com.netflix.servo.monitor.Monitors
见鬼的事发生了. 在家里电脑上拿样例代码,运行时OK的.但一到公司电脑,用同样的代码,就会报下面的错误 ===================== Caused by: java.lang.Class ...
- 【转载】struts应用在断网情况下启动报错解决办法(java/net/AbstractPlainSocketImpl.java:178:-1)
无意间struts应用在有网络的情况下启动正常,在断网的情况下启动报错,报错代码如下图所示: SEVERE: Exception starting filter struts2 Class: java ...
- Android使用xUtils3上传图片报错解决:java.lang.ArrayIndexOutOfBoundsException: 70918
今天在使用安卓xUtils3框架配合SmartUpload框架上传图片到Java服务端时,遇到了一个莫名其妙的错误: 安卓端代码如下: 似乎并没有发现什么问题,以前在用xUtils2.6老版本时也是这 ...
- Tomcat v7.0 java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
十二月 , :: 下午 org.apache.coyote.http11.AbstractHttp11Processor process 信息: Error parsing HTTP request ...
随机推荐
- Java读写Excel之POI超入门(转)
Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能.Apache POI ...
- C# 处理Word自动生成报告 四、程序处理
C# 处理Word自动生成报告 一.概述 C# 处理Word自动生成报告 二.数据源例子 C# 处理Word自动生成报告 三.设计模板 C# 处理Word自动生成报告 四.程序处理 现在说一下程序处理 ...
- 使用vue 遇到的问题————— 解决手机实时显示项目
Vue项目文件组织架构: src文件夹存放源代码. Static文件夹存放第三方静态资源. git将项目上传github http://blog.csdn.net/laozitianxia/ ...
- Github终于连上了hexo
2018-01-2722:59:28 我的妈呀,看看这感人的网速,哎不想吐槽在中国连外网的速度 总结一下连接过程吧 漫漫长征路,难的要死. 一.github的注册和使用不再详述 二.Git Desk ...
- tomcat server.xml context path配置需要注意的事情
在tomcat下放个war包,假如我是这样配置server.xml的,<Context docBase="eggchina" path="/yanan" ...
- Part 2:模型与后台管理admin站点--Django从入门到精通系列教程
该系列教程系个人原创,并完整发布在个人官网刘江的博客和教程 所有转载本文者,需在顶部显著位置注明原作者及www.liujiangblog.com官网地址. Python及Django学习QQ群:453 ...
- 【转】shell中如何判断一个变量是否为空
判断一个脚本中的变量是否为空,我写了一个这样的shell脚本: #!/bin/sh #filename: test.sh para1= if [ ! -n $para1 ]; then echo &q ...
- Java设计模式之策略模式与状态模式
版权声明:本文出自汪磊的博客,转载请务必注明出处. 一.策略模式定义 定义:策略模式定义了一系列的算法,并将每一个算法封装起来,而且使他们之间可以相互替换,策略模式可以在不影响客户端的情况下发生变化. ...
- 浅谈对SpringMVC的认识
SpringMVC概念: 他是一个轻量级的开源框架,应用于表现层,基于MVC的设计模式. SpringMVC的特点: 1.他是单例的可以设置成多例. 2.他的线程是安全的 ...
- SAP进度条
一.代码示例: ),"行数 ll_tabix(),"循环标号 ll_pecnt DECIMALS ,"百分比 ll_pecet(),"百分数 ll_text( ...