package com.mison;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 CookieServlet extends HttpServlet {
private int cookiem; private int cookien; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Cookie cookie =new Cookie("cookiename" +cookiem++, "cookievalue"+cookien++); cookie.setMaxAge(15); response.addCookie(cookie); Cookie []cookies =request.getCookies(); if(null==cookies){ return; } for(Cookie c:cookies){ System.out.println("CookieName:"+c.getName()); System.out.println("CookieValue:"+c.getValue()); } }
}
运行上面的Cookies测试代码 为什么就马上出现如下 而不是刷新一次 出现一次 怎么一次性出现8个
CookieName:cookiename0
CookieValue:cookievalue 0
CookieName:cookiename1
CookieValue:cookievalue 1
CookieName:cookiename2
CookieValue:cookievalue 2
CookieName:cookiename3
CookieValue:cookievalue 3
CookieName:cookiename4
CookieValue:cookievalue 4
CookieName:cookiename5
CookieValue:cookievalue 5
CookieName:cookiename6
CookieValue:cookievalue 6
CookieName:cookiename7
CookieValue:cookievalue 7
CookieName:cookiename8
CookieValue:cookievalue 8