Charles Mosndup
Charles Mosndup

Reputation: 610

Writing cookies Java: Getting the server to set / use a cookie

I have a Java program running on http://serverIP:port on a server Webswing platform converting Java to HTML5. This is perfect. I need to use cookies on the user terminal browser. I have never done it and I am not sure I understand the technology. Can anyone explain? I have found this class but am not sure how to use it. Thanks in advance.

public class CookiesHandler extends CookieManager implements CookieStore{


public CookiesHandler() {
    super();
    // TODO Auto-generated constructor stub
}


@Override
public void add(URI uri, HttpCookie cookie) {
    // TODO Auto-generated method stub

}


@Override
public List<HttpCookie> get(URI uri) {
    // TODO Auto-generated method stub
    return null;
}


@Override
public List<HttpCookie> getCookies() {
    // TODO Auto-generated method stub
    return null;
}


@Override
public List<URI> getURIs() {
    // TODO Auto-generated method stub
    return null;
}


@Override
public boolean remove(URI uri, HttpCookie cookie) {
    // TODO Auto-generated method stub
    return false;
}


@Override
public boolean removeAll() {
    // TODO Auto-generated method stub
    return false;
}  

}

Upvotes: 1

Views: 679

Answers (2)

Charles Mosndup
Charles Mosndup

Reputation: 610

The solution was found here. Webswing offers javascript integration API, which allows invoking javascript functions from Java Swing application code and vice versa.

webswing.org

Upvotes: 2

&#210;scar Raya
&#210;scar Raya

Reputation: 608

It is more much simple. To create a cookie you only need to assing it to document.cookie.

document.cookie = "name=value";

You just assing an string. To get the cookies you call document.cookie again.

If you just assign the cookie as I did it will be removed once the browser close. You can control it's time alive adding a date i UTC format:

document.cookie = "name=value; expires=Thu, 18 Dec 2013 12:00:00 UTC";

Whatch 3WC cookies for more information. It explains you all you need to know aboaut cookies and also provide you 2 functions to set and get cookies.

Upvotes: -1

Related Questions