jsp代码
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<div>
ID:<input type="text" name="name"><br/>
密码:<input type="text" name="pwd"/><br/>
<input type="button" οnclick="location='Login'" value="登录">
</div>
</body>
</html>
servlet代码
public class LoginSerlvet extends HttpServlet {
@Overrideprotected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");//设置解码的编码格式
Cookie cookie=new Cookie("cookie1","我的cookie");//创建一个cookie
response.addCookie(cookie);
response.setCharacterEncoding("utf-8");
PrintWriter printWriter=response.getWriter();
printWriter.println("<div>cookie测试<div>");
}
运行后报错
2013-7-16 20:55:51 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet LoginSerlvet 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 servlet.LoginSerlvet.service(LoginSerlvet.java:21)
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.Http11AprProcessor.process(Http11AprProcessor.java:879)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:617)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1760)
at java.lang.Thread.run(Thread.java:619)
2013-7-16 20:57:45 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet LoginSerlvet 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 servlet.LoginSerlvet.service(LoginSerlvet.java:21)
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.Http11AprProcessor.process(Http11AprProcessor.java:879)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:617)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1760)
at java.lang.Thread.run(Thread.java:619)
分析:因为cookie中部分值是通过文本框录入的,在<textarea/>标签中键入回车键后,value值中就加入了"\r\n",把这样的值付给cookie,执行response.addCookie(cookie);时则出现异常。
解决办法: