问题
I have three types of get requests that are delivered to a class file on web application from a mobile device. Because the mobile device provides no cookies, the log file hit only has
in.ter.nal.ip ser.ver.i.p:port 2009-06-05 09:14:44 GET /applicationname/mobiledevicexml reqtype=login&userid=xx### 200 87 - MercuryMobile/1.0 CFNetwork/342.1 Darwin/9.4.1 cookieArrayLength=0;
If I can instantiate javascript in my class file, and generate a javascript function call to urchinTracker() from inside the class file, I can replace that useless cookieArrayLength=0; with some useful data urchin can read from the log file into analytics reports. We have been looking at scripting in Java with Rhino; Safari Bookshelf has:
Scripting in JavaTM: Languages, Frameworks, and Patterns
which helped us immediately demo that we can run javascript in class files --this works out-of-the-box on Java 6.
Anyone know any resources for scripting with Rhino on Java 1.5 or 1.4?
Alternately, any suggestions for running javascript from java 1.5 would be appreciated.
回答1:
[I'm posting in an answer, because I don't have enough points to post a comment on the question itself.]
Are you sure that the urchinTracker() function will operate outside of a web browser? Running the Rhino JavaScript interpreter (which isn't too difficult) won't be enough if the function relies on various browser objects, like the Document Object Model (DOM) or XmlHttpRequest.
I suggest that you at least scan the internals of the urchinTracker() function to see if this is the case.
回答2:
The Java Scripting API (javax.scripting
) package was introduced in Java 6, so that will not be available in Java 1.4 or 5. As the default installation, Java SE 6 comes with a stripped down version of Mozilla Rhino which is interfaced through javax.scripting
.
However, Mozilla Rhino itself does not require Java 6. From the requirements page:
Recent versions of Rhino have only been tested with JDK 1.4 and greater. Older versions support JDKs as early as 1.1.
Therefore, to use Rhino, it appears that Java 1.4 is actually sufficient.
As for resources, the documentation for Rhino seems to have a lot of information. In particular, the Embedding Rhino section might be useful to see how the scripting will work.
Of course, the lack of the javax.scripting
package means that interfacing to Rhino itself is going to require the use of the Rhino API rather than the Java 6 native scripting API, but I would guess that the functionality is going to be fairly similar. The only downside I can see is, if in the future, Java 6 is going to be supported on the target platform and/or using another language, it may necessitate a rewrite to use the Java Scripting API rather than directly supporting Rhino.
回答3:
See Server-side JavaScript for list of projects that runs JavaScript at the server-side.
For your usage, using Rhino seems like the way to go.
来源:https://stackoverflow.com/questions/989017/scripting-in-java-javascript-from-a-server-side-class-file-in-java-1-5