Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cookie problem (i read many post and doc before to ask)

Status
Not open for further replies.

tyris

Programmer
Nov 2, 2000
311
FR
hi all,
i looked at 120 first results found on "cookie" search onthe forum, but i still can't find a solution so i'm asking it here :

my servlet tryes to read a cookie containing a user name. if it founds it, the client is redirected; if no cookie has been created, it creates it, verify it has been created, and redirect.

my servlet is on domain ".lionbridge.com", the path is set to "/", IE shows me the cookie, so it's created, but the servlet doesn't find it. the line System.out.println("LIONSSOretrievedValue : " + retrievedValue) displays me : NULL

what's wrong ??
regards,
here is the code :
Code:
===================================
retrievedValue = this.getCookie("LionSessId", request);
if (retrievedValue == null)
{
	response = this.setCookie("LionSessId", userName, response); //cookie not set ? so let's set it
	retrievedValue = this.getCookie(request); //and get back the cookie value
}
if (retrievedValue == userName)
{ 
	response.sendRedirect(wantedURL);
}
System.out.println("LIONSSOretrievedValue : " + retrievedValue);



public HttpServletResponse setCookie(String cookieName, String cookieValue, HttpServletResponse response) 
{

	LongLivedCookie MyCookie = new LongLivedCookie(cookieName, cookieValue);
	MyCookie.setDomain(".lionbridge.com");
	MyCookie.setPath("/");
	MyCookie.setMaxAge(60*60*24); //24h should be enought
	response.addCookie(MyCookie);
	return response;

}

public String getCookie(HttpServletRequest request) 
{
	String Valeur=null; 
	Cookie[] cookies = request.getCookies();
	for(int i=0; i < cookies.length; i++) 
	{
		System.out.println(i);
		Cookie thisCookie = cookies[i];
		if (thisCookie.getName().equals(&quot;LionSessId&quot;)) //what i'm looking for
		{
			Valeur = thisCookie.getValue()
		}
		else
		{
			System.out.println(&quot;not found&quot;);
		}
	}	
	return Valeur;
}

===================================
Best regards,
Elise, XML learning girl X-)
 
in fact i must modify something : the cookie retrival works if the IE window is not closed, and if the .class has not been changed, or the http server not been retarted. as soon as one of this action is performed, the cookie is &quot;lost&quot;
what can i do for avoid the cookie loss ? i was thinking that setting the max age to 24h would be enought ?

MyCookie.setMaxAge(60*60*24); Best regards,
Elise, XML learning girl X-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top