0

I have an ArrayList, Whom i convert to String like

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()

Now when i retrieve cookie value, i get String, but i again want to convert it into ArrayList like

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

How can i again convert it to ArrayList? Thank you.

3
  • That really rather depends on what the contents of the arraylist were! Have you considered JSON? Commented Apr 17, 2012 at 6:34
  • Consider json encoding and decoding it instead. Edit: great minds think alike. Commented Apr 17, 2012 at 6:34
  • How can i use JSON for ArrayList. Actually when user loged in, then i use something like thisArrayList str = (ArrayList) retrieveList.get(1);session.setAttribute("getusercredentials", str);makeCookie("userCredentialsCookie", str.toString(), httpServletResponce); But if user closes his browser and it's timeout has not been expire, then i use cookie value to again set it to session. How can i use JSON here? Thanks Commented Apr 17, 2012 at 6:40

3 Answers 3

3

someList.toString() is not a proper way of serializing your data and will get you into trouble.

Since you need to store it as a String in a cookie, use JSON or XML. google-gson might be a good lib for you:

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);
Sign up to request clarification or add additional context in comments.

5 Comments

What do you mean by Serializable? read the above comment plz, thanks
@Basit, please disregard the previous comment about Serializable - you won't be able to use it since you need to store a String on the cookie. Also added an example of how to use the Gson lib in my edit.
HHmm you mean to say i should get the values from ArrayList and then set those values to Cookie, and then again get all the values from cookies and then make ArrayList from them, and set it to session? Is it? Because you use retrieveList.get(0). Means you are getting content from List and converting it to JSON? Am i right?
@Basit, it was a typo, meant to follow your original code (: The idea is that Gson will save your entire ArrayList to/from String for you, so you don't need to deal with individual values or manipulate the String yourself.
One strange thing happening. I am using code something like this. ` session.setAttribute("getusercredentials", str);String content = new Gson().toJson(str);makeCookie("userCredentialsCookie", content, httpServletResponce);session.setAttribute("sessionUserId", str.get(0).toString());makeCookie("userIdCookie", str.get(0).toString(), httpServletResponce);But the userCredentialsCookie` is not adding. Other cookeis are adding. Why it is happening? Because of the ArrayList size. Does Cookie can't hold large values? Thanks. When i am trying to get Cookie, i get null only userCredentialCookie.
3

As long as it's an ArrayList of String objects you should be able to write a small method which can parse the single String to re-create the list. The toString of an ArrayList will look something like this:

"[foo, bar, baz]"

So if that String is in the variable value, you could do something like this:

String debracketed = value.replace("[", "").replace("]", ""); // now be "foo, bar, baz"
String trimmed = debracketed.replaceAll("\\s+", ""); // now is "foo,bar,baz"
ArrayList<String> list = new ArrayList<String>(Arrays.asList(trimmed.split(","))); // now have an ArrayList containing "foo", "bar" and "baz"

Note, this is untested code.

Also, if it is not the case that your original ArrayList is a list of Strings, and is instead say, an ArrayList<MyDomainObject>, this approach will not work. For that your should instead find how to serialise/deserialise your objects correctly - toString is generally not a valid approach for this. It would be worth updating the question if that is the case.

Comments

0

You can't directly cast a String to ArrayList instead you need to create an ArrayList object to hold String values.

You need to change part of your code below:

ArrayList userCredntialsList = (ArrayList)value;  //Need String to ArrayList 
session.setAttribute(attributeName, userCredntialsList);

to:

ArrayList<String> userCredentialsList = ( ArrayList<Strnig> ) session.getAttribute( attributeName );
if ( userCredentialsList == null ) {
    userCredentialsList = new ArrayList<String>( 10 );
    session.setAttribute(attributeName, userCredentialsList);
}

userCredentialsList.add( value );

1 Comment

Thanks but i have no session attribute at that time, this method is call by my filter when the session expires, therefore i am trying to get it from cookie, and then want to convert it to ArrayList.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.