Control character in cookie value, consider BASE64 encoding your value

3399 ワード

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)

ソリューション:
クッキーにデータを格納する際、UTF-8を用いてデータを符号化する.クッキーから値を取る場合は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"/>