The JavaFX webview uses an in memory cookie store, and therefore doesn't persist cookies between sessions.
You need to implement a cookie store yourself.
public class myCookieStore implements CookieStore {...this is where you implement the interface...}
Then, inside that store you maintain a list of the cookies.
Possibly like this, but it's up to you.
private List<HttpCookie> cookies = new ArrayList<>();
Once you do that, inside your javaFX initialize function, you set the webEngine cookiestore to your implementation.
@Override
public void initialize(URL location, ResourceBundle resources) {
WebEngine engine = this.webViewID.getEngine();
java.net.CookieManager manager = new CookieManager(new myCookieStore(),CookiePolicy.ACCEPT_ALL);
java.net.CookieHandler.setDefault(manager);}
Inside your implementation of the CookieStore, you'll have to save that list of cookies to a file (or database, or wherever you're putting it) whenever it changes, then reload it when your app reloads.
This is where I'm personally running into a roadblock and how I found your question. HttpCookie doesn't implement serializable and has private fields that won't reflect - which means you can't simply serialize and deserialize the list and using gson.toJson() (a solution listed here Setting a cookie using JavaFX's WebEngine/WebView) also fails. You'll likely have to manually save the cookie fields, and rebuild the cookie. I'll update this with a solution for that if I actually ever get it to work myself.
///UPDATE///
It turns out that the serilization problem with the HTTPCookie starts with Java 9. If you use Java 8, then you can simply implement the cookiestore interface, and then use the Gson example in the previously linked post to persist that list. However, if you're using Java 9 or higher, then you can no longer do this because it blocks the reflection that gson uses to gain access to the private fields.
I re-implemented the publicly accessible members of the HTTPCookie object, like this:
public class myCookieClass implements Serializable{
private URI uri;
private String name;
private String value;
private String domain;
private String path;
private String portList;
private String comment;
private String commentURL;
private boolean httpOnly;
private boolean discard;
private long maxAge;
private boolean secure;
private int version;
public myCookieClass(URI uri, HttpCookie cookie){
name = cookie.getName();
value = cookie.getValue();
domain = cookie.getDomain();
maxAge =cookie.getMaxAge();
path = cookie.getPath();
httpOnly = cookie.isHttpOnly();
portList = cookie.getPortlist();
discard = cookie.getDiscard();
secure = cookie.getSecure();
version = cookie.getVersion();
comment = cookie.getComment();
commentURL = cookie.getCommentURL();
this.uri = uri;
}
public HttpCookie toCookie(){
HttpCookie cookie = new HttpCookie(this.name,this.value);
cookie.setSecure(secure);
cookie.setDomain(domain);
cookie.setMaxAge(maxAge);
cookie.setPath(path);
cookie.setHttpOnly(httpOnly);
cookie.setPortlist(portList);
cookie.setDiscard(discard);
cookie.setVersion(version);
cookie.setComment(comment);
cookie.setCommentURL(commentURL);
cookie.setValue(value);
return cookie;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
myCookieClass that = (myCookieClass) o;
return uri.equals(that.uri) &&
name.equals(that.name);
}
@Override
public int hashCode() {
return Objects.hash(uri, name);
}
}
Then, I'm using essentially the same gson example as before to save and restore that list.
public void RestoreCookieStoreFromFile(){
try {
File file = new File(fileName);
if (file.exists()) {
System.out.println("RESTORE COOKIES");
FileReader fileReader = new FileReader(fileName);
String json = new String(Files.readAllBytes(Paths.get(fileName)));
Gson gson = new GsonBuilder().create();
Type type = new TypeToken<List<myCookieClass>>() {
}.getType();
cookies = gson.fromJson(json, type);
}
} catch(FileNotFoundException e){
//file not found
e.printStackTrace();
} catch(IOException e){
// cant create object stream
e.printStackTrace();
}
}
private void saveCookieStoreToFile(String location){
try {
Gson gson = new GsonBuilder().create();
String jsonCookie = gson.toJson(cookies);
Files.writeString(Path.of(fileName),jsonCookie);
} catch (FileNotFoundException e) {
// file not found
System.out.println("Can't Save File");
e.printStackTrace();
} catch (IOException e) {
System.out.println("Can't Save File OUTSTREAM");
// can't create output stream
e.printStackTrace();
}
catch (InaccessibleObjectException e){
System.out.println("Can't Access object");
e.printStackTrace();
}
}
These are the cookiestore interface members that are actually doing the work, at least in my case. There are a few more members, but nothing seems to call those.
@Override
public void add(URI uri, HttpCookie cookie)
{
cookies.add(new myCookieClass(uri,cookie));
saveCookieStoreToFile(fileName);
}
@Override
public List<HttpCookie> get(URI uri) {
List<HttpCookie> uriCookies = new ArrayList<>();
myCookieClass[] tempCookies = cookies.toArray(myCookieClass[]::new);
for (myCookieClass c: tempCookies) {
if(c.uri.toString().contains(uri.getRawAuthority())){
uriCookies.add(c.toCookie());
}
}
return uriCookies;
}
@Override
public List<HttpCookie> getCookies() {
List<HttpCookie> httpCookies = new ArrayList<>();
myCookieClass[] tempCookies = cookies.toArray(myCookieClass[]::new);
for (myCookieClass c : tempCookies) {
httpCookies.add(c.toCookie());
}
return httpCookies;
}
and just for completeness, this is the list that holds my cookies in memory.
private List<myCookieClass> cookies = new ArrayList<>();
As always, good luck!