偶写了一个写入Cookie的代码
import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SetCookies extends HttpServlet{ @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
    for(int i=0;i<3;i++)
    {
    
     Cookie cookie =new Cookie("Session-Cookie- "+i,"Cookie-Value-S"+i);
     resp.addCookie(cookie);
    
            cookie = new Cookie("Presistent-Cookie-"+i,"Cookie-Value-P"+i);
            cookie.setMaxAge(3600);
     resp.addCookie(cookie);
    }
    
    resp.setContentType("text/html;charset=gb2312");
    PrintWriter out=resp.getWriter();
    
    out.println("<html><head><title>设置cookie</title></head>"
     +"<body>\n"
     +"<h1 align=\"center\">"
     +"设置cookie"+"</h1>\n"
     +"6个cookie\n"
     +"<a href=\"Show cookies\">\n"
     +"查看</a>.\n"
     +"</body></html>");
}}
运行的时候出现了如下的错误~HTTP Status 500 - --------------------------------------------------------------------------------type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception java.lang.IllegalArgumentException: Cookie name "Session-Cookie- 0" is a reserved token
javax.servlet.http.Cookie.<init>(Cookie.java:141)
SetCookies.doGet(SetCookies.java:19)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.24 logs.
请问是什么原因?谢谢

解决方案 »

  1.   

    还在学习中,PHP中的Cookie,我都没有搞太清楚
      

  2.   

    public Cookie(String name,
                   String value)Defines a cookie with an initial name/value pair. Names must not contain whitespace, comma, or semicolons and should only contain ASCII alphanumeric characters. 你的name有空格。
      

  3.   

    Cookie name "Session-Cookie- 0" is a reserved token用了保留的东西了,换个Cookie名
      

  4.   

    注意看错误提示这一行
    java.lang.IllegalArgumentException: Cookie name "Session-Cookie- 0" is a reserved token 
    你的Cookie名字是保留字!