aspose file tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Regarding Cookies Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Reply Bookmark "Regarding Cookies" Watch "Regarding Cookies" New topic
Author

Regarding Cookies

prakashgnana kota
Greenhorn

Joined: May 25, 2005
Posts: 1
Hi Everybody,

I want to add Remember me Option in the login page using cookies.Code snippet is given below but I am getting some errors.

The error is:

The flow goes from login.jsp page to a Servlet.

creating a cookie in the servlet if the username and password matches and the user selects the option.

Cookie cookieuser;
cookieuser=new Cookie("username",strInputUserName);
cookieuser.setMaxAge(3*24*60*60);
response.setContentType("text/html");
response.addCookie(cookieuser);
cookieuser=new Cookie("password",strInputPassword);
cookieuser.setMaxAge(3*24*60*60);
response.addCookie(cookieuser);

Actually the below code snippet reads the cookie from the created cookie but it is not reading.So pl suggest me where the error occurs:

Cookie[] cookies=request.getKookies();
if(cookies !=null){
for(int i=0;i& lt;cookies.length;i++)
{

if(cookies[i].getName().equals("username"))
{
names=cookies[i].getValue();
System.out.println("cookie username"+names);
}
if(cookies[i].getName().equals("password"))
{
passes=cookies[i].getValue();

}

} }
Note:If we r typing kookies starts with "C" in the above code it is considering the browser ones.That's the reason we have used request.getKooies().

If we add the cookies in the same JSP/servlets page the above code is working properly.How do we retrieve the cookies when we use in different pages?

If am redirecting to servlet i am getting another problem unable to create session after response has been committed.

Thanx & Regards
Prakash
[ May 26, 2005: Message edited by: prakashgnana kota ]
Rodrigo Dinis
Ranch Hand

Joined: May 28, 2003
Posts: 40
Hi Prakash,
looking quickly I think u are putting a cookie with a name and want to get it with another...

cookieuser=new Cookie("cookiename",strInputUserName);
...

if(cookies[i].getName().equals("username"))
...



Rodrigo Dinis
SCEA
SCJWSD
SCJD
SCBCD
SCWCD
SCJP
CNA
Brasilia - BRASIL
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi,

Cookie[] cookies=request.getkookies();

is it correct?

Thanks


Narendra Dhande
SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0
Vishwa Kumba
Ranch Hand

Joined: Aug 27, 2003
Posts: 1064
I think the original poster used the call getKookies() method name in the post as the ranch sofware has some problem. It cannot interpret the actual method name.(try making a post with the "ghetCookies()" method name.You will get the error. I have replaced get with ghet )
 
I agree. Here's the link: http://zeroturnaround.com/jrebel
 
subject: Regarding Cookies
 
Similar Threads
adding and retriving cookies.
Unable to understand output of these servlets
add user
Tomcat refusing to set a cookie...
response.addCookie is not working