我有一个ArrayList,我将其转换为字符串,如
ArrayList str = (ArrayList) retrieveList.get(1);
...
makeCookie("userCredentialsCookie", str.toString(), httpServletResponce);
....
private void makeCookie(String name, String value, HttpServletResponse response) {
Cookie cookie = new Cookie(name, value);
cookie.setPath("/");
response.addCookie(cookie);
} //end of makeCookie()现在,当我检索Cookie值时,我得到了字符串,但我再次希望将其转换为ArrayList,如下所示
private void addCookieValueToSession(HttpSession session, Cookie cookie, String attributeName) {
if (attributeName.equalsIgnoreCase("getusercredentials")) {
String value = cookie.getValue();
ArrayList userCredntialsList = (ArrayList)value; //Need String to ArrayList
session.setAttribute(attributeName, userCredntialsList);
return;
}
String value = cookie.getValue();
session.setAttribute(attributeName, value);
} //end of addCookieValueToSession如何再次将其转换为ArrayList?谢谢。
发布于 2012-04-17 14:35:51
someList.toString()不是序列化数据的适当方式,而且会给您带来麻烦。
由于您需要将其作为字符串存储在cookie中,因此请使用JSON或XML。google-gson可能是一个很好的库:
ArrayList str = (ArrayList) retrieveList.get(1);
String content = new Gson().toJson(str);
makeCookie("userCredentialsCookie", content, httpServletResponce);
//...
ArrayList userCredntialsList = new Gson().fromJson(cookie.getValue(), ArrayList.class);https://stackoverflow.com/questions/10186220
复制相似问题