Using Nashorn with Other Web Applications

In JavaOne 2013, I attended a seminar on the Nashorn project. I was surprised to learn about this. Call Java from JavaScript and vice versa.

But one question remains unclear to me: how can we use Nashorn in favor of web applications such as JSF, ADF Faces or Wicket, etc. If someone gives any pointer, it will be very noticeable.

+4
source share
3 answers

The scripting APIs for the Java platform in general and Nashorn in particular offer a wide range of opportunities for developing web applications on the JVM. Framework such as node.js and vert.x solvency JavaScript as a server structure. Yes, we are still awaiting news about node.jar, the mysterious Oracle project to implement the node.js API on the Java platform.

In today's modern web applications, we should think of the server side as a RESTful services, rather than the presentation structure that generates html on the server. But even for server pages, you don’t have to adhere to such frameworks as JSF, Wicket, ADF. With Nashorn / Rhino, you can use JavaScript templates to generate html markup for the backend. LinkedIn, for example, has already described the benefits of having templates written in JavaScript in both the browser and server. In case your browser fails to compress client templates, you can competently degrade and switch to server-side rendering.

If you are looking for an example of using JavaScript in a server side web environment, you can start with Dust4j . Do not confuse the words Rhino in the description. Dust4j does not use Rhino's internal APIs. It uses the jsr223 API, so if you run it on JDK8 or JDK7 using the Nashorn backport , it should work. The Dust4j project shows how you can integrate scripts in your JSP / Servlet / Filter application.

+3
source

Nashorn is a JavaScript compiler and runtime for the Java virtual machine. It is not a web application platform in itself, but it can be built on top of it.

Therefore, this is not a replacement for JSF or ADF.

+2
source

Apache Wicket provides functionality for hosting JavaScript code on the server now, and also allows access to objects with limited access to the session / request. It does not replace the Web Framework itself, but allows you to create a programming interface for running code on the server and changing Java objects, calculating values, and returning results.

Wicket Nashorn integration documentation can be found here: https://github.com/wicketstuff/core/wiki/NashornIntegration

And if you redo the git repository, you can run nashorn integration examples: https://github.com/wicketstuff/core/tree/master/nashorn-parent/nashorn-examples

It remains only to mention: since this is arbitrary code execution, this can lead to security problems, so make sure that only registered users can access this interface.

0
source

Source: https://habr.com/ru/post/1491338/


All Articles