Cookie与Session

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 {

@Override
protected 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);时则出现异常。 

解决办法:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值