• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Cookies

 
prakashgnana kota
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"))
...

 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Cookie[] cookies=request.getkookies();

is it correct?

Thanks
 
Vishwa Kumba
Ranch Hand
Posts: 1066
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 )
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic