views:

121

answers:

5

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?

+1  A: 

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

Magnar
+2  A: 

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

bpapa
+2  A: 

I guess there's a major misunderstanding in the way how JS/Java is supposed to work in webapplications.

The normal way to let JavaScript access Java variables is to let JSP/EL print them as if it's a JS variable. Here's a basic example:

<script>
   var someJavaScriptVariable = '${someJavaBean.someProperty}';
</script>

The normal way to let Java access JavaScript variables it to let JS send them as request parameters along the (XML)HTTP request. Basic example:

<script>
    document.getElementById("inputId").value = 'newValue';
    document.getElementById("formId").submit();
</script>

or

<script>
    window.location = '/newpage?param1=value1';
</script>

or with any of the jQuery ajax requests.

To learn more, you may find this article useful: Java/JSP/JSF and JavaScript.

BalusC
+3  A: 

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.

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.

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. 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.

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.

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.

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.

Andrzej Doyle
A very thorough answer to an unclear question.
serg10
+1  A: 

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, Part 2: Call remote JavaScript functions with Ajax
  3. JavaScript EE, Part 3: Use Java scripting API with JSP

You can also have a look to 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 is written in Java and employs Javascript for its server-side scripting environment ...

DomreiRoam