cancel
Showing results for 
Search instead for 
Did you mean: 

JSPDynpage + set Cookie

Former Member
0 Kudos

Iam trying to set a cookie in one JSP and trying to retrieve in another JSP of one JSPDynpage application..

<b>set.jsp</b>

<script language="javascript">

function createCookie()

{

<%

Cookie ck = new Cookie("name", "sap");

ck.setMaxAge(246060);

response.addCookie(ck);

%>

}

</script>

read.jsp

<script language="javascript">

function readCookie(){

<%

Cookie ck =null;

Cookie[] cookies = request.getCookies();

for(int i=0;i<cookies.length;i++)

{ ck = cookies<i>;

if(ck.getName().toString()=="name"){

out.println("Cookie:"+ck.getValue());

break;

}

}

%>

}

Iam not getting any errors..but not retrieving the stored cookie..Iam not sure if it setting it also.

Any hints??

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

Hi Peter,

Try these:

<b>set.jsp</b>
<script type="text/javascript">
function setCookie() 
{
  NameOfCookie = "ck";
  value = "sap";
  expiredays = 365;
  var ExpireDate = new Date ();
  ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
  document.cookie = NameOfCookie + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
  document.write("cookie: "+NameOfCookie+" set with value "+value);
}
</script>

<b>read.jsp</b>
function getCookieValue(NameOfCookie)
{
  var valueFromCookie = getCookie(NameOfCookie);
  if (valueFromCookie != "")
  {
    document.write("Cookie: "+NameOfCookie +"value is "+valueFromCookie);
  }
}
function getCookie(NameOfCookie)
{ 
  if (document.cookie.length > 0) 
  { 
    begin = document.cookie.indexOf(NameOfCookie+"="); 
    if (begin != -1) 
    { 
      begin += NameOfCookie.length+1; 
      end = document.cookie.indexOf(";", begin);
      if (end == -1) end = document.cookie.length;
      return unescape(document.cookie.substring(begin, end)); 
    } 
  }
  return null; 
}
</script>

Regards,

Ville

Former Member
0 Kudos

Thanks, wud try ur code and post the feedback here again!

Thanx for the code Ville.

Former Member
0 Kudos

Hello Peter,

I think you've 2 mistakes in source (read.jsp):

1) "ck" is a type of "Cookie", and "cookies" is an array, so ck = cookies is illegal (conversion error)

2) to compare two string use equals() method instead of "==" operator.

Why you set / retrieve cookies in <javascript> tags?

For future development check existence of cookie files in yours cookie directory (in IE Tools->Settings->Temporary internet files)

You can also use some sniffer to check cookies.

There is a great tool (an eclipse plugin "Solex") for recording http sessions - very useful in debugging. It works like a proxy and with this tool you can easily analyze http requests.

And a last useful tool is an "WebDeveloper" extension for Firefox browser. Also nice when writing web apps.

Hope it helps

Artur

Former Member
0 Kudos

Thanx Artur,

Well ,

ck = cookies<i>; is not bug code line.

Thatz wat all the tutorials abt cookies say..

Anymore Hints??

Thanks

detlev_beutner
Active Contributor
0 Kudos

Hi Artur, hi Peter,

- Peter wrote in both cases [ i ] behind cookies, but the forum interpreted this a staring italics...

- Peter, did you try <i>equals</i> instead of "=="?!

- Did you understand why it is senseless to put this server side-fragments into pseudo client-side JS-functions?

Hope it helps

Detlev

Former Member
0 Kudos

Also found something weird ..

When i alert in set.jsp like:

alert(<%=ck.getName()%>);

it displays Desktop Innerpage.

watz happening??