brakebg
brakebg

Reputation: 414

Can't extract all cookies in Java Servlet

In my current application i m developing i need to exctract all cookies from the user browser created by the application server. The problem is that with Chrome and Opera i am able to get all cookies and this is fine, but using Firefox and IE only some of them. When i check if the cookies i need are crated in the browser history i see that they are but can't be got in my java servlet.

What i do is as follow:

public static Cookie getCookieByName(String cookieName, HttpServletRequest request) {
        Cookie cookie = null;
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (int i = 0; i < cookies.length; i++) {
                Cookie c = cookies[i];
                if (c.getName().compareTo(cookieName) == 0) {
                    cookie = c;
                    break;
                }
            }
        }
        return cookie;
    }

For Firefox and Opera the returned cookies are JSESSIONID, __utma ... but there are missing cookies .. the ones i need.. Any idea how to proceed ?

Upvotes: 2

Views: 3457

Answers (2)

Stephen C
Stephen C

Reputation: 719739

The browser will send the cookies that it thinks are appropriate to send, and you won't be able to convince it to send any more.

What you need to do is to examine the details of the cookies that are not being sent to figure out what distinguishes them from the ones that are being sent. Things to look at include:

  • the cookie's domain
  • the cookie's path
  • the cookie's port
  • the cookie's 'secure' flag

RFC 2965 - HTTP State Management Mechanism may be helpful in sorting this out.

Upvotes: 4

JB Nizet
JB Nizet

Reputation: 692281

Use Firebug to check if those cookies are really sent to the server by Firefox, and if their name matches the name you're searching for. Also add some debug traces and print the names of all the cookies.

Maybe you have some path set in the cookie, and Firefox is stricter than IE.

Upvotes: 2

Related Questions