• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

Cookie method error

I got this method from an older jsp question:

public static String getCookieValue(String cookieName) {
     Cookie[] theCookies = request.getCookies();
     if (theCookies != null) {
        for (int i =0; i< cookies.length; i++) {
           Cookie currentCookie = theCookies[i];
           if (currentCookie.getName().equalsIgnoreCase(cookieName)
            return currentCookie.getValue();
           }
        }
   return null;
}

but when I try to use it, I get an error on the line:


     Cookie[] theCookies = request.getCookies();

that says package request does not exist.

Any ideas?

Thanks!
0
k41d3n
Asked:
k41d3n
  • 3
  • 3
2 Solutions
 
ldbkuttyCommented:
you should have import javax.servlet.http.*; and request defined, something like:

import javax.servlet.http.*;

public static String getCookieValue(String cookieName, HttpServletRequest request) {
    Cookie[] theCookies = request.getCookies();
    if (theCookies != null) {
        for (int i =0; i< theCookies.length; i++) {
            Cookie currentCookie = theCookies[i];
            if (currentCookie.getName().equalsIgnoreCase(cookieName))
                 return currentCookie.getValue();
            }
        }
    return null;
}
   
0
 
k41d3nAuthor Commented:
I do :(
0
 
ramazanyichCommented:
In your original method you access non-static variable request from static method.
So you should provide request as parameter for yur method:
public static String getCookieValue(String cookieName, HttpServletRequest request){
//...your code
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
k41d3nAuthor Commented:
Excellent, thank you.

0
 
ldbkuttyCommented:
I dont understand..i showed the same in my code and explained it as "request defined"

?
0
 
k41d3nAuthor Commented:
You are absolutely correct, it's my fault I didn't read it properly.
0
 
ldbkuttyCommented:
Thanks for replying.

Please post here a Question and the moderators will re-open this Question:
http://www.experts-exchange.com/Community_Support/

:-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now