如何在同一服务器上拥有许多JavaFX WebView
组件,每个组件都有自己的基于私有cookie的会话,例如:来自具有多个标签的用户界面?
作为用例,运行以下SSCCE并从“浏览器1”选项卡登录到站点(我一直在使用https://mail.google.com/)。从“浏览器2”选项卡打开同一站点,您将看到“浏览器1”中的会话。 (注意:您必须输入完整地址,并带有协议前缀 - http://
- 然后按“开始”按钮 - ENTER将不起作用。)
编辑:使用engine.userDataDirectory
属性也不起作用。
import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import javafx.concurrent.Worker.State;
public class TwoBrowserTabs extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
TabPane tabPane = new TabPane();
Tab tab1 = new Tab();
tab1.setText("Browser 1");
tab1.setContent(makeBrowserTab("browser1"));
tabPane.getTabs().add(tab1);
Tab tab2 = new Tab();
tab2.setText("Browser 2");
tab2.setContent(makeBrowserTab("browser2"));
tabPane.getTabs().add(tab2);
Scene scene = new Scene(tabPane);
primaryStage.setTitle("Two browsers");
primaryStage.setScene(scene);
primaryStage.show();
}
private Node makeBrowserTab(String identifier) {
BorderPane borderPane = new BorderPane();
TextField address = new TextField();
HBox.setHgrow(address, Priority.ALWAYS);
Button go = new Button();
go.setText("Go");
HBox hbox = new HBox();
hbox.getChildren().add(address);
hbox.getChildren().add(go);
WebView browser = new WebView();
browser.setPrefSize(960, 640);
borderPane.setTop(hbox);
borderPane.setCenter(browser);
WebEngine engine = browser.getEngine();
// EDIT: Does not work either
engine.setUserDataDirectory(new File(System.getProperty("user.home") + File.separator + "TwoBrowserTabs" + File.separator + identifier));
// Let the button reflect the state of the loader,
// so as to have a minimalistic feedback.
engine.getLoadWorker().stateProperty().addListener((ov, oldState, newState) -> {
if( newState == State.READY || newState == State.SUCCEEDED ) {
go.setDisable(false);
go.setText("Go");
}
else if( newState == State.FAILED || newState == State.CANCELLED ) {
go.setDisable(false);
go.setText("Go (!)");
}
else {
go.setDisable(true);
go.setText("Go");
}
});
go.setOnAction(event -> {
engine.load(address.getText());
});
return borderPane;
}
public static void main(String[] args) {
launch(args);
}
}
答案 0 :(得分:0)
我认为您可以通过以下链接获得帮助: Clear the session/cache/cookie in the JavaFX WebView
此外,如果服务器部分在您的控制之下,您可以在其中进行一些会话设置,以便不维护会话和你也可以阻止cookie存储在客户端。