Skip to Content
author's profile photo Former Member
Former Member

JSPDynpage + set Cookie

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??

Add a comment
10|10000 characters needed characters exceeded

Related questions

3 Answers

  • author's profile photo Former Member
    Former Member
    Posted on Jul 22, 2005 at 07:33 PM

    Also found something weird ..

    When i alert in set.jsp like:

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

    it displays Desktop Innerpage.

    watz happening??

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jul 22, 2005 at 07:50 PM

    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

    Add a comment
    10|10000 characters needed characters exceeded

    • 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

  • author's profile photo Former Member
    Former Member
    Posted on Jul 24, 2005 at 08:47 AM

    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

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.