views:

101

answers:

2

Hi all, My problem with cookies are in following I'm try to add new cookie and few lines after I'm going to read all cookies and to make new object and to assign it to Spring View, problem is that mine cookies are not added, everything works after page reload but not in same time...

    Cookie[] cookies = request.getCookies();
if (cookies != null) {
    for (int i = 0; i < cookies.length; i++) {
        if (cookies[i].getName().equals(
                "proizvod_" + Integer.toString(proizvodiId))) {
            String kolicina_cur = cookies[i].getValue();
            int pom = Integer.parseInt(kolicina_cur);
            cookies[i].setValue(Integer.toString(pom + kolicina));
            response.addCookie(cookies[i]);                     
        }

    }
}

Cookie[] cookiesN = request.getCookies();
ProizvodiCommand proizvodiCommand = new ProizvodiCommand();
if (cookiesN != null) {
    for (int i = 0; i < cookiesN.length; i++) {
        if (cookiesN[i].getName().startsWith("proizvod_")) {
            String ime = cookiesN[i].getName();
            String kolicinaN = cookiesN[i].getValue();
            String id_s = ime.replace("proizvod_", "");
            int id = Integer.parseInt(id_s);
            // prepare for spring view....
        }


    }
}

I was remove few lines for spring data preparation, for better view.

Same problem occurs when I try to remove cookie and to collect all existing cookies few lines below, it works after page reload.

I was try to add some delay, but not helps...If someone have a solution go ahead and share it with me :)

Thanks!

A: 

If I understand you right, you read cookies that come with request, than add cookie to response. And then expect to see added cookie among cookies that come with request...

Nulldevice
+1  A: 

First, the code that you posted looks like it only modifies cookies that already exist rather than adding new cookies.

Second, each call to request.getCookies() returns the same set of cookie/value pairs during a single request. Therefore, your second block of code doesn't see your new cookies.

Try these changes (in the second block of code):

Cookie[] cookies = request.getCookies();
if (cookies != null) {
    for (int i = 0; i < cookies.length; i++) {
        if (cookies[i].getName().equals(
                "proizvod_" + Integer.toString(proizvodiId))) {
            String kolicina_cur = cookies[i].getValue();
            int pom = Integer.parseInt(kolicina_cur);
            cookies[i].setValue(Integer.toString(pom + kolicina));
            response.addCookie(cookies[i]);                     
        }

    }
}

ProizvodiCommand proizvodiCommand = new ProizvodiCommand();
if (cookies != null) {
    for (int i = 0; i < cookies.length; i++) {
        if (cookies[i].getName().startsWith("proizvod_")) {
            String ime = cookies[i].getName();
            String kolicinaN = cookies[i].getValue();
            String id_s = ime.replace("proizvod_", "");
            int id = Integer.parseInt(id_s);
            // prepare for spring view....

    }
}
This is good start, but still have a same problem :/
vaske
What do you see in the debugger?