HttpCookie SetMaxAge() Example

Started by VelMurugan, Dec 20, 2008, 10:37 PM

Previous topic - Next topic

VelMurugan

HttpCookie SetMaxAge() Example

Sets the maximum age of the cookie in seconds.

HttpCookieclass SetMaxAge() method example. This example shows you how to use SetMaxAge() method.This method Sets the maximum age of the cookie in seconds.

Here is the code:-

/*
* @Program that Sets the maximum age of the cookie in seconds.
* SetMaxAge.java
* Author:-RoseIndia Team
* Date:-25-Jun-2008
*/

import java.net.*;

public class SetMaxAge {

    public static void main(String[] args) throws Exception {
        String name = "Name";
        String value = "ABC";
        HttpCookie hc = new HttpCookie(name, value);
        //Returns the maximum age of the cookie, specified in seconds.
        System.out.println("Maximum age of the cookie is: " + hc.getMaxAge());

        //Sets the maximum age of the cookie in seconds.
        hc.setMaxAge(2345);
        System.out.println("Maximum age of the cookie is: " + hc.getMaxAge());
    }
}


Output of the program:-

Maximum age of the cookie is: -1
Maximum age of the cookie is: 2345

Source : codingdiary