Control character in cookie value, consider BASE64 encoding your value

在做Cookie测试时, 因用户名中含有中文, 而后台处理时, 没有对cookie进行编码解码, 导致了以下错误.

七月 15, 2014 4:06:04 下午 org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.IllegalArgumentException: Control character in cookie value, consider BASE64 encoding your value
	at org.apache.tomcat.util.http.ServerCookie.maybeQuote2(ServerCookie.java:396)
	at org.apache.tomcat.util.http.ServerCookie.maybeQuote2(ServerCookie.java:389)
	at org.apache.tomcat.util.http.ServerCookie.appendCookieValue(ServerCookie.java:293)
	at org.apache.catalina.connector.Response.generateCookieString(Response.java:1063)
	at org.apache.catalina.connector.Response.addCookieInternal(Response.java:1035)
	at org.apache.catalina.connector.Response.addCookieInternal(Response.java:1020)
	at org.apache.catalina.connector.Response.addCookie(Response.java:968)
	at org.apache.catalina.connector.ResponseFacade.addCookie(ResponseFacade.java:343)
	at org.apache.jsp.login_jsp._jspService(login_jsp.java:67)
	at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
	at java.lang.Thread.run(Thread.java:724)

解决方案:

在将数据存入到cookie中时, 使用UTF-8对数据进行编码; 在从cookie中取值时, 使用UTF-8进行解码

编码:

<% 
	Cookie cookie = new Cookie(URLEncoder.encode("姓名","UTF-8"),URLEncoder.encode("张三","UTF-8"));
	response.addCookie(cookie);
%>
解码:
<%
	if(request.getCookies() != null){
		for(Cookie cc : request.getCookies()){
			String cookieName = URLDecoder.decode(cc.getName(),"UTF-8");
			String cookieValue = URLDecoder.decode(cc.getValue(),"UTF-8");
			out.println(cookieName + "=" + cookieValue + "; <br/>");
		}
	}else{
		out.println("Cookie已经写入客户端,请刷新页面.");
	}
%>

注: 在JSP页面中使用时, 还需要引入编解码包
<jsp:directive.page import="java.net.URLEncoder"/>
<jsp:directive.page import="java.net.URLDecoder"/>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值