我正在尝试动态命名Cookie。在下面的代码中,我使用增强的for循环创建cookie,然后使用另一个来查看cookie。当我尝试分配
时,该过程停止myCookie = new Cookie("cardAnswer " + i, fC.getCardAnswer());
有没有办法实现这个目标?或者这不可能吗? cardList包含带问题和答案字符串的FlashCard对象。 ArrayList中可以存在不同数量的FlashCard对象,这就是我试图动态设置cookie名称的原因。有更好的方法可以解决这个问题吗?
Boolean useSecureCookie = false;
int expiryTime = 60 * 10;
String cookiePath = "/";
Cookie myCookie;
int i = 0;
ArrayList<FlashCard> cardList;
String fk_deckID = request.getParameter("fk_deckID");
Deck deck = new Deck(fk_deckID);
cardList = deck.getFlashCardList();
for (FlashCard fC : cardList) {
myCookie = new Cookie("cardAnswer " + i, fC.getCardAnswer());
myCookie.setSecure(useSecureCookie);
myCookie.setMaxAge(expiryTime);
myCookie.setPath(cookiePath);
response.addCookie(myCookie);
i++;
}
Cookie[] cookies = request.getCookies();
i = 0;
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("cardAnswer " + i)) {
//value can be retrieved using #cookie.getValue()
out.printf(cookie.getValue());
//response.sendRedirect("flashCardShow.html");
} i++;
}
}