1

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;
}  

}

Charles Mosndup
  • 600
  • 6
  • 19
  • Your question is not making a lot of sense to me. Are you asking about setting / using cookies in browser code, getting the server to set / use a cookie, or implementing cookies in a server-side connection to another server? Or something else? – Stephen C Jul 11 '17 at 08:01
  • For what it is worth, that class is almost certainly useless for what you are trying to do. At the very least, to get a *functioning* CookieStore you will need to implement those methods with auto-generated (dummy) implementations. – Stephen C Jul 11 '17 at 08:03
  • I am asking about getting the server to set / use a cookie – Charles Mosndup Jul 11 '17 at 08:10

2 Answers2

2

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

Charles Mosndup
  • 600
  • 6
  • 19
  • Also see this question https://stackoverflow.com/questions/56771069 where OP shows how to handle javascript cookies from Java app code. – Branislav Kuliha Jul 04 '19 at 15:07
-1

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.

Òscar Raya
  • 598
  • 5
  • 26
  • The question was about handling cookies in *Java*. Java is a different language than Javascript, in the same way that Austria is a different country than Australia. – RealSkeptic Jul 11 '17 at 08:10
  • just use javascript inside tha java with Rhino. As I understood he is converting the java into html5, so if he write javascript using rhino it will obtain the same. – Òscar Raya Jul 11 '17 at 09:56
  • The Java is on the *server side*, using Rhino is good for running general, non-browser-dependent Javascript. `Document` doesn't exist outside of a browser. – RealSkeptic Jul 11 '17 at 09:57
  • Hello, the program is Java, the platform Webswing does the job of translating everything in HTML5 behind the scene. – Charles Mosndup Jul 11 '17 at 10:16
  • And my question is: when the user enters his/her name in one of the application's form, I would like this name to be set as a cookie in his/her browser, I have never done that and I don't know how to do it in Java. – Charles Mosndup Jul 11 '17 at 10:19