如何在服务器端Java代码中运行JavaScript代码?

时间:2021-12-18 04:23:53

I want to run JavaScript code at the server side. I want to manipulate result returned by JavaScript inside my Java code. How can it be done?

我想在服务器端运行JavaScript代码。我想在Java代码中操作JavaScript返回的结果。怎么做呢?

6 个解决方案

#1


15  

The start is clearly to look into rhino.

显然,首先要研究的是犀牛。

I think you will find this 3 links very useful

我想你会发现这三个链接非常有用

  1. JavaScript EE, Part 1: Run JavaScript files on the server side
  2. JavaScript EE,第1部分:在服务器端运行JavaScript文件
  3. JavaScript EE, Part 2: Call remote JavaScript functions with Ajax
  4. JavaScript EE,第2部分:用Ajax调用远程JavaScript函数。
  5. JavaScript EE, Part 3: Use Java scripting API with JSP
  6. JavaScript EE,第3部分:在JSP中使用Java脚本API

You can also have a look to helma

你也可以看看helma。

Helma is a server-side Javascript environment and web application framework for fast and efficient scripting and serving of your websites and Internet applications.

Helma是一个服务器端Javascript环境和web应用程序框架,用于快速有效地编写脚本并为您的网站和Internet应用程序提供服务。

Helma is written in Java and employs Javascript for its server-side scripting environment ...

Helma是用Java编写的,其服务器端脚本环境使用Javascript。

#2


8  

The other answers are correct that if you want to execute Javascript on the server side, you'd need to evaluate it in the context of a JS runtime.

其他的答案是正确的,如果您想在服务器端执行Javascript,您需要在JS运行时的上下文中对其进行评估。

However, I'm not convinced that this is exactly what you're asking. I think there may be a chance that you want to run some "typical" JS functionality that relates to how the page is displayed on the client's machine or interacted with on the client - and that will not be possible to run on the server side.

然而,我不相信这就是你要问的。我认为您可能希望运行一些“典型的”JS功能,这些功能与页面在客户端机器上的显示方式或与客户端上的交互方式相关,而在服务器端则不可能运行这些功能。

As a concrete examples:

作为一个具体的例子:

  1. If you want to run some kind of algorithm in JS without porting it to Java - say, you have some opaque Javascript code that generates a particular sequence given a seed - this would work fine if you run it on Rhino on the server.
  2. 如果您想在JS中运行某种算法而不将其移植到Java——比如,您有一些不透明的Javascript代码,它在给定种子的情况下生成特定的序列——如果您在服务器上的Rhino上运行它,那么这将很好。
  3. If you want to invoke a Javascript function while creating the page, rather than while it's running - say, to get the user's colour depth/screen resolution and change how the page is generated - then this will not be possible from the server, as there is no client at this point to query.
  4. 如果你想在创建页面时,调用一个Javascript函数,而不是运行时——比如说,让用户/屏幕分辨率和颜色深度的改变如何生成页面,然后从服务器,这是不可能的,因为没有客户查询。

Broadly speaking, any Javascript that involves document or navigator or even any elements of the page itself, is likely to fall into the latter category.

一般来说,任何涉及文档或导航器甚至页面本身的任何元素的Javascript都可能属于后者。

If you really need to get information about the client environment in order to control how the page is rendered, this must be extracted from the client on the previous page, and encoded into the request (as query parameters or form data). These parameters can then be read directly on the server and used to control the output.

如果您确实需要获取关于客户端环境的信息,以便控制页面的呈现方式,那么必须从上一页的客户端提取这些信息,并将其编码到请求中(作为查询参数或表单数据)。然后可以在服务器上直接读取这些参数并用于控制输出。

Remember that when your code is running on the server side, you're creating a page (ultimately a bunch of HTML, CSS and JS) that will be sent to the client once it's done - at this point there is no client yet and so you can't interact with them.

请记住,当您的代码在服务器端运行时,您正在创建一个页面(最终是一堆HTML、CSS和JS),该页面将在完成后发送给客户机——此时还没有客户机,因此您无法与它们交互。

Apologies if I've got the wrong end of the stick on this one, but this type of question is typically asked by people who haven't grasped the client/server separation.

如果我在这个问题上有错误的想法,请向我道歉,但是这种类型的问题通常是由那些没有理解客户端/服务器分离的人提出的。

#3


3  

You need a JS runtime inside of a Java runtime. One way to do this is Rhino

在Java运行时中需要一个JS运行时。一种方法是Rhino。

#4


2  

You execute the JavaScript with Rhino, a JavaScript library for Java.

使用Rhino (Java的JavaScript库)执行JavaScript。

#5


0  

You can use RHINO or NASHORN.

你可以使用RHINO或NASHORN。

public class RhinoApp {
private String simpleAdd = "var z=9; z*=9";

public void runJavaScript() {
    Context jsCx = Context.enter();
    Context.getCurrentContext().setOptimizationLevel(-1);
    ScriptableObject scope = jsCx.initStandardObjects();
    Object result = jsCx.evaluateString(scope, simpleAdd , "formula", 0, null);
    Context.exit();
    System.out.println(result);
}

#6


-3  

If you want to call JavaScript in browser from Java on the server, look at the FeRMI Framework. It does so over WebSockets.

如果您想从服务器上的Java调用浏览器中的JavaScript,请查看FeRMI框架。它是通过WebSockets实现的。

#1


15  

The start is clearly to look into rhino.

显然,首先要研究的是犀牛。

I think you will find this 3 links very useful

我想你会发现这三个链接非常有用

  1. JavaScript EE, Part 1: Run JavaScript files on the server side
  2. JavaScript EE,第1部分:在服务器端运行JavaScript文件
  3. JavaScript EE, Part 2: Call remote JavaScript functions with Ajax
  4. JavaScript EE,第2部分:用Ajax调用远程JavaScript函数。
  5. JavaScript EE, Part 3: Use Java scripting API with JSP
  6. JavaScript EE,第3部分:在JSP中使用Java脚本API

You can also have a look to helma

你也可以看看helma。

Helma is a server-side Javascript environment and web application framework for fast and efficient scripting and serving of your websites and Internet applications.

Helma是一个服务器端Javascript环境和web应用程序框架,用于快速有效地编写脚本并为您的网站和Internet应用程序提供服务。

Helma is written in Java and employs Javascript for its server-side scripting environment ...

Helma是用Java编写的,其服务器端脚本环境使用Javascript。

#2


8  

The other answers are correct that if you want to execute Javascript on the server side, you'd need to evaluate it in the context of a JS runtime.

其他的答案是正确的,如果您想在服务器端执行Javascript,您需要在JS运行时的上下文中对其进行评估。

However, I'm not convinced that this is exactly what you're asking. I think there may be a chance that you want to run some "typical" JS functionality that relates to how the page is displayed on the client's machine or interacted with on the client - and that will not be possible to run on the server side.

然而,我不相信这就是你要问的。我认为您可能希望运行一些“典型的”JS功能,这些功能与页面在客户端机器上的显示方式或与客户端上的交互方式相关,而在服务器端则不可能运行这些功能。

As a concrete examples:

作为一个具体的例子:

  1. If you want to run some kind of algorithm in JS without porting it to Java - say, you have some opaque Javascript code that generates a particular sequence given a seed - this would work fine if you run it on Rhino on the server.
  2. 如果您想在JS中运行某种算法而不将其移植到Java——比如,您有一些不透明的Javascript代码,它在给定种子的情况下生成特定的序列——如果您在服务器上的Rhino上运行它,那么这将很好。
  3. If you want to invoke a Javascript function while creating the page, rather than while it's running - say, to get the user's colour depth/screen resolution and change how the page is generated - then this will not be possible from the server, as there is no client at this point to query.
  4. 如果你想在创建页面时,调用一个Javascript函数,而不是运行时——比如说,让用户/屏幕分辨率和颜色深度的改变如何生成页面,然后从服务器,这是不可能的,因为没有客户查询。

Broadly speaking, any Javascript that involves document or navigator or even any elements of the page itself, is likely to fall into the latter category.

一般来说,任何涉及文档或导航器甚至页面本身的任何元素的Javascript都可能属于后者。

If you really need to get information about the client environment in order to control how the page is rendered, this must be extracted from the client on the previous page, and encoded into the request (as query parameters or form data). These parameters can then be read directly on the server and used to control the output.

如果您确实需要获取关于客户端环境的信息,以便控制页面的呈现方式,那么必须从上一页的客户端提取这些信息,并将其编码到请求中(作为查询参数或表单数据)。然后可以在服务器上直接读取这些参数并用于控制输出。

Remember that when your code is running on the server side, you're creating a page (ultimately a bunch of HTML, CSS and JS) that will be sent to the client once it's done - at this point there is no client yet and so you can't interact with them.

请记住,当您的代码在服务器端运行时,您正在创建一个页面(最终是一堆HTML、CSS和JS),该页面将在完成后发送给客户机——此时还没有客户机,因此您无法与它们交互。

Apologies if I've got the wrong end of the stick on this one, but this type of question is typically asked by people who haven't grasped the client/server separation.

如果我在这个问题上有错误的想法,请向我道歉,但是这种类型的问题通常是由那些没有理解客户端/服务器分离的人提出的。

#3


3  

You need a JS runtime inside of a Java runtime. One way to do this is Rhino

在Java运行时中需要一个JS运行时。一种方法是Rhino。

#4


2  

You execute the JavaScript with Rhino, a JavaScript library for Java.

使用Rhino (Java的JavaScript库)执行JavaScript。

#5


0  

You can use RHINO or NASHORN.

你可以使用RHINO或NASHORN。

public class RhinoApp {
private String simpleAdd = "var z=9; z*=9";

public void runJavaScript() {
    Context jsCx = Context.enter();
    Context.getCurrentContext().setOptimizationLevel(-1);
    ScriptableObject scope = jsCx.initStandardObjects();
    Object result = jsCx.evaluateString(scope, simpleAdd , "formula", 0, null);
    Context.exit();
    System.out.println(result);
}

#6


-3  

If you want to call JavaScript in browser from Java on the server, look at the FeRMI Framework. It does so over WebSockets.

如果您想从服务器上的Java调用浏览器中的JavaScript,请查看FeRMI框架。它是通过WebSockets实现的。