SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy How to check if cookie exists or not

    How to check if cookie exists or not using JSP code?

  2. #2
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Check for cookies using Java/JSP

    How to check if cookie exists or not using JAVA/JSP?

  3. #3
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    look here
    my mobile portal
    ghiris.ro

  4. #4
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This codes looks with all cookies which is problem for me so i directly wanted to check if particular cookies exists or not.

  5. #5
    SiteP0int Weazle hooknc's Avatar
    Join Date
    Dec 2004
    Location
    Socialist Republic of Boulder
    Posts
    937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Open the cookie jar and look in. If there are cookies, then they exist.

    Except for oatmeal raisin, those type of cookies don't exist to me. I look into the cookie jar and if its full of oatmeal raisin, no cookies exist.
    baby steps... baby steps...

  6. #6
    SiteP0int Weazle hooknc's Avatar
    Join Date
    Dec 2004
    Location
    Socialist Republic of Boulder
    Posts
    937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But seriously...

    On HttpServletRequest there is a method called getCookies that returns an array of Cookie(s).

    You should then be able to look through that array to see if the cookie you want is present or not.

    To set a Cookie you will need to use the addCookie(Cookie) method on HttpServletResponse.
    baby steps... baby steps...

  7. #7
    SiteP0int Weazle hooknc's Avatar
    Join Date
    Dec 2004
    Location
    Socialist Republic of Boulder
    Posts
    937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The only method available on HttpServletRequest that deals with Cookies is getCookies(), which returns an array of Cookies.

    You will have to loop though the Cookies like the example in the above link to figure out if the Cookie you're interested in is there or not.

    You could write a helper method if needed and have something like a HttpUtils class.
    baby steps... baby steps...

  8. #8
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes we have the array and loop through it and you are able to track it down, but I was wondering if we have any solution to avoid array loop and directly check the cookie.

    Ex:

    if (cookie.name("my_cookie")) == -1)
    {
    cookie present
    }
    else
    not present

  9. #9
    SitePoint Wizard rozner's Avatar
    Join Date
    Oct 2002
    Location
    Paris
    Posts
    1,058
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The browser sends all cookies so you don't really have much choice.

  10. #10
    SiteP0int Weazle hooknc's Avatar
    Join Date
    Dec 2004
    Location
    Socialist Republic of Boulder
    Posts
    937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by simplecode View Post
    Yes we have the array and loop through it and you are able to track it down, but I was wondering if we have any solution to avoid array loop and directly check the cookie.

    Ex:

    if (cookie.name("my_cookie")) == -1)
    {
    cookie present
    }
    else
    not present
    So what are you trying to do exactly with that cookie value? Are you performing this cookie check in many different jsp files? Is this cookie value required on every page?

    You might consider using a filter to check for the cookie.
    baby steps... baby steps...

  11. #11
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    You could do

    boolean cookiePresent = Arrays.toList( request.getCookies() ).contains( "cookie_name" );

    but the array will still be looped through, behind the scenes (with this code, twice, I think).

    If it's something you check often, set a session attribute to denote that you checked. Then you only have to 'pay' for the check once, after that, it's as efficient as it's going to get.

  12. #12
    SitePoint Wizard
    Join Date
    Apr 2007
    Posts
    1,399
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by hooknc View Post
    Open the cookie jar and look in. If there are cookies, then they exist.

    Except for oatmeal raisin, those type of cookies don't exist to me. I look into the cookie jar and if its full of oatmeal raisin, no cookies exist.
    I'm so tired of those jokes... wish they would name it other than cookie....

  13. #13
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    looking for JSP logic instead of JS

  14. #14
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    We gave you JSP logic, more accurately, we gave you Java logic that can be used in a JSP.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •