0
try {
    CookieManager manager = new CookieManager();
    CookieHandler.setDefault(manager);
    CookieStore cookieJar =  manager.getCookieStore();


    HttpCookie cookie = new HttpCookie("user", username);

    URL url = new URL("http://host.example.com");
    cookieJar.add(url.toURI(), cookie);
    System.out.println("Added cookie using cookie handler");
} catch(Exception e) {
    System.out.println("Unable to set cookie using CookieHandler");
    e.printStackTrace();
}

How will I add time for the cookie to expire because right now it is more like a session cookie. It expires every time the application is destroyed/closed

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

Assuming you are using java.net.HttpCookie, there is a setMaxAge(long) that sets the expiration of a cookie.

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120