Menu

Cookies behaviour seems undefined?

Anonymous
2003-01-06
2003-01-06
  • Anonymous

    Anonymous - 2003-01-06

    I am running the following code below ( A Jsp page and an included httpunit code)
    and I am having the following problems:

    1.  The cookie  i'm setting (User="root")  does not show up. [However, the Tomcat server cookie appears]

    2.  When I run the same request again, neither cookies show up at all.

    Why is this?  Is this a bug? Can somebody check out this code and tell me what I'm doing wrong?

    JSP: "installcookie.jsp"
    <%
    session.setAttribute("UserID", "root");
    %>

    HTTPUNIT code:

    import com.meterware.httpunit.GetMethodWebRequest;
    import com.meterware.httpunit.WebConversation;
    import com.meterware.httpunit.WebRequest;
    import com.meterware.httpunit.WebResponse;
    import junit.framework.Test;
    import junit.framework.TestCase;
    import junit.framework.TestSuite;

    public final class cookietest extends TestCase {

        public static void main(final String[] args) {
            junit.textui.TestRunner.run( suite() );
        }

        private static Test suite() {
            return new TestSuite( cookietest.class );
        }

        public cookietest( final String name ) {
            super( name );
        }

        public static void testcookie() throws Exception {
            final WebConversation     conversation = new WebConversation();
            final WebRequest request = new GetMethodWebRequest( "http://localhost:8080/installcookie.jsp" );

            WebResponse response = conversation.getResponse( request );
            int c = 0;
            for ( c = 0; c < response.getNewCookieNames().length; c++) {
                System.out.println("Cookie: " + response.getNewCookieNames()[c] + "\t" + response.getNewCookieValue(response.getNewCookieNames()[c]));
            }
            System.out.println("Number of cookies : " + c);

            response = conversation.getResponse( request );
            int c1 = 0;
            for ( c1 = 0; c1 < response.getNewCookieNames().length; c1++) {
                System.out.println("Cookie: " + response.getNewCookieNames()[c1] + "\t" + response.getNewCookieValue(response.getNewCookieNames()[c1]));
            }
            System.out.println("Number of cookies : " + c1);
        }

    }

    Thanks,
    opus

     
    • Russell Gold

      Russell Gold - 2003-01-06

      setAttribute does not create a cookie. It merely sets a value in the server session, identified by the Tomcat cookie you are already receiving. And the cookie is only sent in the first reply, since your next request sends it back and the server knows that you have it.

       

Log in to post a comment.

Auth0 Logo