Cookie保存中文用户名报错(500)
在用Cookie保存用户名时候,当用户名是中文的时候服务器报错了。
HTTP Status 500 - An exception occurred processing JSP page /dologin.jsp at line 40
type Exception report
message An exception occurred processing JSP page /dologin.jsp at line 40
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /dologin.jsp at line 40 37: Cookie passwordCookie = new Cookie("password",password);
38: usernameCookie.setMaxAge(86400);
39: passwordCookie.setMaxAge(86400);
40: response.addCookie(usernameCookie);
41: response.addCookie(passwordCookie);
42: }else
43: { Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.IllegalArgumentException: Control character in cookie value or attribute.
org.apache.tomcat.util.http.CookieSupport.isHttpSeparator(CookieSupport.java:193)
org.apache.tomcat.util.http.CookieSupport.isHttpToken(CookieSupport.java:217)
org.apache.tomcat.util.http.ServerCookie.appendCookieValue(ServerCookie.java:186)
org.apache.catalina.connector.Response.generateCookieString(Response.java:1070)
org.apache.catalina.connector.Response.addCookie(Response.java:1012)
org.apache.catalina.connector.ResponseFacade.addCookie(ResponseFacade.java:380)
org.apache.jsp.dologin_jsp._jspService(dologin_jsp.java:107)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.56 logs.
------------------------------------------------------------------------------分割线------------------------------------------------------------------------------
这里设置request.setCharacterEncoding("utf-8");也改变不了报错
解决方法:Java网络包里面编码和解码的工具类(URLEncoder)
原来的代码
String username = request.getParameter("username");
String password = request.getParameter("password");
Cookie usernameCookie = new Cookie("username",username);
Cookie passwordCookie = new Cookie("password",password);
usernameCookie.setMaxAge(86400);
passwordCookie.setMaxAge(86400);
response.addCookie(usernameCookie);
response.addCookie(passwordCookie);
修改代码
用URLEncoder.encode解决无法在Cookie当中保存中文字符串的问题
String username = URLEncoder.encode(request.getParameter("username"),"utf-8") ;
String password = URLEncoder.encode(request.getParameter("password"),"utf-8");
Cookie usernameCookie = new Cookie("username",username);
Cookie passwordCookie = new Cookie("password",password);
usernameCookie.setMaxAge(86400);
passwordCookie.setMaxAge(86400);
response.addCookie(usernameCookie);
response.addCookie(passwordCookie);
然后还要解码
URLDecoder.decode
注意: 编码解码分别在java.net.URLDecoder 和 java.net.URLEncoder下。记得导入类
Cookie保存中文用户名报错(500)的更多相关文章
- Control character in cookie value, consider BASE64 encoding your value , java操作cookie遇到中文会报错的解决方案
项目当中用到cookie保存中文,但是会报如下错误: Control character in cookie value, consider BASE64 encoding your value 大概 ...
- 关于centos7字体缺失导致项目验证码丢失报错500问题
这个问题是这样的,迁移架构的时候项目验证码刷不出来, 页面报错500, 就像下面那样. tomcat报错是数组越界, 看下面 最诡异的是, 开发那边再三确定代码里面没有问题, 于是我试了一下把war包 ...
- webstorm中sass编译时目录或内容包含中文字符报错
ruby版本:ruby 2.3.1p112 (2016-04-26 revision 54768) [x64-mingw32] sass版本:Sass 3.4.22 (Selective Steve) ...
- 记一次 Hibernate 插入数据中文乱码报错解决
错误描述 程序运行,向表中插入数据(包含中文)报错:\xE6\xB2\x88\xE9\x9B\xAA... 但是自己另外新建一个数据库手动插入数据中文正常,同样修改数据库,表的编码之后同样不行.而且 ...
- 打开nginx配置的站点报错500
打开站点报错500的原因 有很多,这里只说明一点:nginx 的fastcgi.conf配置引起的问题 环境说明 1 站点目录结构 wwwroot website public application ...
- spring boot中连接数据库报错500(mybatis)
spring boot中连接数据库报错500(mybatis) pom.xml中的依赖 <!-- 集成mybatis--> <dependency> <groupId&g ...
- sass中中文注释报错
最近项目中用到了sass来编译css,但是scss代码中写了中文注释后编译报错, 经过查找文档和资料,终于找到了解决办法,即在scss文件顶部加上@charset "utf-8"; ...
- Linux虚拟机部署单机solr报错500解决方法之一
HTTP Status 500 - {msg=SolrCore 'collection1' is not available due to init failure: Could not load c ...
- 解决spring-boot配置文件使用加密方式保存敏感数据启动报错No decryption for FailsafeTextEncryptor. Did you configure the keystore correctly
spring-boot配置文件使用加密方式保存敏感数据 application.yml spring: datasource: username: dbuser password: '{cipher} ...
随机推荐
- ZigZag Conversion 之字形转换字符串
1.题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...
- ECharts折线图多个折线每次只显示一条
echart 两条折线图如何默认只显示一条,另一条隐藏呢 只需要在legend后加上, selectedMode: 'single', selectedMode [ default: true ] 图 ...
- netcore 下加密遇到的问题
KeyedHashAlgorithm algorithm = KeyedHashAlgorithm.Create(algorithmName.ToString().ToUpper(CultureInf ...
- 解决mysql连接linux上mysql服务器的问题
在远程连接mysql的时候,连接不上,出现如下报错:Lost connection to MySQL server at 'waiting for initial communication pack ...
- 聚宽获取财务数据+DataFrame写入txt
from jqdata import jy from jqdata import * #获取股票列表,这里是板块内股票 pool=get_industry_stocks(',date='2016-09 ...
- 关于感受野 (Receptive field) 你该知道的事
Receptive field 可中译为“感受野”,是卷积神经网络中非常重要的概念之一. 我个人最早看到这个词的描述是在 2012 年 Krizhevsky 的 paper 中就有提到过,当时是各种不 ...
- 独立安装CentOS7.4全记录
大学用了四年的笔记本快用废了,闲来想着用来装个centos,当个服务器也行,于是装上了CentOS6.9系统,由于最小化安装,而且在安装时没有安装wpa_supplicant包,笔记本本身网卡接口又坏 ...
- spring-framework源码编译及导入
环境说明: 时间:2017-09-26 OS name: "mac os x", version: "10.12.4", arch: "x86_64& ...
- WMI参数介绍
Win32_DiskDrive 硬盘 参数说明 vailability --设备的状态.BytesPerSector --在每个扇区的物理磁盘驱动器的字节数.Capabilities --媒体访 ...
- ORACLE设置用户密码不过期
1.查看用户的 profile 是哪个,一般是 default SELECT USERNAME, PROFILE FROM dba_users; 2.查看指定概要文件(这里是1中对应的profile) ...