tier1
tier1

Reputation: 6433

JavaFX WebView catch post request from javascript

I'm experimenting with JavaFX for the first time and I am intrigued by the WebView class. I would like to be able to use it to locally host a HTML5 site that would essentially be the front end for a desktop application.

My question is, if I make an ajax request from javascript within the webview, is there any way to handle that request with JavaFX? Part of the application could handle post requests but I'm wondering if there is anything built-in?

-Edit-

My question was originally poorly worded. What I'm essentially trying to do is use the WebView to create an HTML5 desktop app similar to what QT5 offers. So when I talk about ajax calls, I mean that I would like to somehow make a bridge between JS and JavaFX.

Upvotes: 0

Views: 2098

Answers (2)

jewelsea
jewelsea

Reputation: 159416

Your original question was a bit unclear to me, but your comment on your question seemed to make sense, so this answer only addresses that.

I want to know if there is a easy way that JavaFX can handle them. Basically turn it into a restful server

Most restful servers run over http, If you want to track html post traffic from a JavaFX WebView, you will need to run a http server. There are many restful servers for Java; e.g. jersey or restlet. There are also numerous general purpose servers for Java (glassfish, wildfly, jetty, tomcat etc) and most of them have APIs which allow you to embed them in other programs. An embedded server is a web server running on the same machine as your application (so no remote server required). Here is a link to an embedded jersey server tutorial, so you can get a sample of how an embedded server works.

Choose an appropriate server, embed it with your JavaFX application and use it to serve up HTML pages which are consumed by a JavaFX WebView. The server can also respond to http get and form post commands from the HTML served up by the WebView.

By packaging the embedded server with your application as a self-contained application, the user can install your software from a single installer package which contains, the java runtime, the embedded server and your application server - so everything they need to run your app locally in a completely self-contained package.

Upvotes: 1

ItachiUchiha
ItachiUchiha

Reputation: 36742

You are mistaken with what JavaFX and its webView is all about.

JavaFX is used to create Client Side Applications, which will run on the clients machine. Attaching a server to your application exploits the sole purpose of Client-Server architecture

WebView (From the docs)

This Web Engine component, is based on WebKit, which is an open source web browser engine that supports HTML5, CSS, JavaScript, DOM, and SVG. It enables developers to implement the following features in their Java applications:

  • Render HTML content from local or remote URL

  • Support history and provide Back and Forward navigation

  • Reload the content

  • Apply effects to the web component

  • Edit the HTML content

  • Execute JavaScript commands

  • Handle events

The WebView is just like any other browser which we use. As written above it is powered by WEBKIT. Nevertheless, it runs on JVM, through JavaFX.

JavaFX is generally used to create the client applications. Although, java provides method to design a server.

Consider the following :

  • For a simple Client-Server architecture, you can look into Sockets
  • To create a more complex web application, you need to look into creating web application using Java Servlets. These java programs need an Appication Server/Container to be executed. Support SOAP/RESTFUL web services
  • You can use an embedded server, if you would like to ship your server with the javafx application. e.g. Jetty Embedded Server

Upvotes: 0

Related Questions