I have two applications: (1) a JavaFX application A and (2) a purely JavaScript web application B (no backend) accessed either as local files (for developers) or at a certain URL (for the users).
In A the user can save a file and then open B in the user's browser via Desktop.getDesktop().browse(). Then the user can load that file in B by clicking on an input element of type file, which allows the usage of FileReader.
That process is too cumbersome for the users, so I want to remove that requirement of the user to save and load a file.
My problem is: How can I transfer this data from A to B now?
- GET parameters only allow a few thousand characters, which is not enough for us
- I see no way to pass POST parameters from Java to the browser and also to access those parameters from within JavaScript
FileReadercannot read files using path names because of security- we can't use platform or browser dependent native code solutions because the users use different OSes and browsers
- the web application uses many libraries and advanced JavaScript features, which I don't expect a JavaFX HTML window to support
Is there any way to get one of the above methods to work or is there any alternative to pass data between Java and a purely client-based JavaScript application in a browser?
Update
As suggested in the accepted answer, I serve the data as using Jetty.
1. Java: Define JSON Server
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.json.JSONArray;
public class JsonServer extends Server
{
static final int PORT = 1234;
public JsonServer(JSONArray json)
{
super(PORT);
setHandler(new JsonHandler(json));
}
public static class JsonHandler extends AbstractHandler
{
final JSONArray json;
public JsonHandler(JSONArray json)
{
this.json = json;
}
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
response.setContentType("application/json; charset=utf-8");
response.setStatus(HttpServletResponse.SC_OK);
try(PrintWriter out = response.getWriter();)
{
out.println(json);
}
baseRequest.setHandled(true);
}
}
}
2. Java: Start Server
Server server = new JsonServer(json);
server.start();
browse("https://myapp.com?load=http://localhost:1234");
3. JavaScript: Fetch
const load = url.searchParams.get("load");
const json = await (await fetch(load)).json();