脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class CookieExample extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); // print out cookies Cookie[] cookies = request.getCookies(); for (int i = 0; i < cookies.length; i++) { Cookie c = cookies[i]; String name = c.getName(); String value = c.getValue(); out.println(name + " = " + value); } // set a cookie String name = request.getParameter("cookieName"); if (name != null && name.length() > 0) { String value = request.getParameter("cookieValue"); Cookie c = new Cookie(name, value); response.addCookie(c); } } } |
如果cookie的name中含有 “;” 则会抛出异常,出现500错误,这个是没有问题的,但是,如果value中含有 “;” 会怎么处理呢?在PHP中setcookie()函数会将value做url编码,如果是setrawcookie()则会报错误。而在JSP中却出现了如下现象:
1 2 3 4 5 |
Content-Length:779 Content-Type:text/html;charset=ISO-8859-1 Date:Thu, 17 Jan 2013 15:01:41 GMT Server:Apache-Coyote/1.1 Set-Cookie:m="M;n"; Version=1 |
浏览器会因为给value添加了双引号而将cookie m的只是为 “M;n”吗? 不会的: