Executing some Javascript feature explicitly in a Java application is impossible. In fact, this is not restricted only to Java, but to any Server-Side language with the exception of Node.JS, that does not expressly work with the front-end.
To solve your problem, you can use AJAX. A very simple way to solve this is with a solution that you yourself proposed in a comment from before: send data via an HTTP request in JSON format.
Conceptually, what you need to do is very simple: serialize the content you want to send to Javascript through a java method and then, there on the front end, in Javascript code, you can do something like this using the jQuery library:
$.ajax({
url: 'localhost/user',
success: function (response) {
console.log(response);
}
});
Being url
the address you provide your JSON data to - through your Java application - and the method success
to work with the return if the request is made, suggestively, successfully.
The parameter response
of the method success
are the data sent by your Java application. From there, you can work as well as understand.
At this link I take a very similar approach that might be useful.
Thanks... it’s really seeing here that it’s impossible even. I’ll see these examples you sent me, thank you.
– Erico Souza
It is possible to communicate javascript with java via json?
– Erico Souza
@Ericosouza Bingo! I explained it better in my reply.
– Guilherme Oderdenge