core
core

Reputation: 33059

Browserless JavaScript

I talked to the Team Lead at Snap-On Tools once, and she told they used an "implementation of JavaScript" for their server-side coding. It's been a while, but I was thinking, WTF is she talking about? Are there interpreters for JavaScript besides those implemented in browsers?

How can you create a program or code, especially server-side, using JavaScript that doesn't execute in the context of a browser? What the hell is server-side about JavaScript if it's not generating content after the browser has loaded it? Can "server-side" JavaScript generate content before the HTTP response is deliver—and if so, how does that work/is set up?

I have many issues with JavaScript, but first-class functions are so sexy. And JavaScript Object Notation is so pure; I couldn't imagine an easier way to define data structures. Plus, you can hack out some code pretty quickly with dynamic typing if you're not writing something that's mission critical.

As a side question, given the last paragraph, have any suggestions about a good language to learn (comments will suffice)?

Upvotes: 5

Views: 1868

Answers (12)

DavGarcia
DavGarcia

Reputation: 18792

JavaScript does not have to be run in a browser if you use an ECMAScript engine. Actually, both SpiderMonkey and Rhino are ECMAScript engines.

Flash's ActionScript is another ECMAScript derived language that doesn't have to run in a browser.

Edit - Wow, a lot has changed in three years. For your server-side needs, I now recommend node.js.

Upvotes: 13

Bnicholas
Bnicholas

Reputation: 14121

For reference, node.js

Upvotes: 4

Franck Freiburger
Franck Freiburger

Reputation: 28428

jslibs is a good browserless JavaScript runtime (based on Firefox's JS engine)

Upvotes: 0

orip
orip

Reputation: 75427

List of JS interpreters that I know of, that can run standalone or embedded with other code:

Upvotes: 5

oberhamsi
oberhamsi

Reputation: 1361

choosing an engine

the other answers already mentioned several JS engines. one very important factor in deciding which one to use should be the language in which it is implemented (C, C++ or Java are the choices), since this "host language" will be the one you have access to very easily.

for example if you use rhino, you can easily access the whole java standardlib from javascript code (typically you will write wrappers in JS so you don't have all those java-lib calls sprinkled in your JS code).

choosing a framework

there were several mentioned alread. I like helma most. old and tried. lots of big sites run on it. there is also a sexy but very alpha version of helma over at github: http://github.com/hns/helma-ng/

serverside JS

you might also be interested in this group, they try to unify how JS works serverside (provide a standardlib, etc). the just started this group, but have a lot of smart people working on serversideJS for years:

https://wiki.mozilla.org/ServerJS

Upvotes: 0

leeand00
leeand00

Reputation: 26382

Have you checked out John Resig's post about creating a serverside Javascript engine with Rhino?

(http://ejohn.org/projects/bringing-the-browser-to-the-server/)

Upvotes: 0

Brian Whitlock
Brian Whitlock

Reputation: 151

There is also HTTPUnit which is more of a browser simulator, so, it allows you to interact with the DOM.

http://httpunit.sourceforge.net/

Upvotes: 0

coobird
coobird

Reputation: 160954

As have been noted, Rhino is a JavaScript engine written in Java.

Java 6 has a new scripting engine facility, appropriatedly called the Java Scripting API in the javax.script package, for which a Rhino-based engine is available for use out of the box. I'm not sure if this is available on Java EE, but it is a standard feature of Java SE.

Using the Scripting API is quite simple and allows for Java applications to run scripts written in Javascript quite easily. If you require scripting in your application, there's no need to write your own interpreter -- just use the Scripting API. Also, the script engine has access to the Java class libraries, so it integrates nicely to perform tasks within your application as well.

For example, calculating the square root of 4, using the Scripting API is as simple as:

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
engine.eval("var x = java.lang.Math.sqrt(4);");
engine.eval("println(x);");

In the context of using this on the server side, I can envision an external Jaavscript being executed by Rhino and the results are then embedded onto the dynamically-generated HTML page that is being served. This way, features could be added or modified without making changes to the web application itself. The Javascipt scripts themselves can act more or less like plugins for the main application.

The Java Scripting Programmer's Guide has several examples which are built up step-by-step to introduce the features of the Java Scripting API.

Javascript doesn't necessarily have to be confined to a browser. As with any scripting language, it really depends on where the scripting engine is being hosted.

Upvotes: 0

Roger Lipscombe
Roger Lipscombe

Reputation: 91825

As well as VBScript, classic ASP pages can use JScript as the underlying script language. You can run JScript programs on the Windows command line by using CSCRIPT.EXE. In fact, it's the same scripting engine, and it's extensible to support any number of languages.

Upvotes: 2

Paolo Bergantino
Paolo Bergantino

Reputation: 488384

I've heard good things about Jaxer.

By the far the most popular/attractive part of these server-side javascript solutions is in regards to data validation. You can use the same code that you use to validate forms client-side again on the server to ensure their integrity. This comes really useful in simply being DRY and not getting rules out of sync when something changes.

Upvotes: 2

Zach
Zach

Reputation: 24768

Yes, JavaScript Virtual Machines exists outside of the browser. Here's a list of specially tailored server side adaptations.

I personally use it Spidermonkey on the command line to try code out. Rhino is the same adaptation of ECMAScript as Spidermonkey (same language implementation) but Rhino runs on the Java VM, and Spidermonkey was written in C.

Upvotes: 1

Henrik Paul
Henrik Paul

Reputation: 67703

SpiderMonkey and Rhino seem to be pretty much related. SpiderMonkey seems to be a C-library for executing JS, while Rhino is a Java-ditto.

Upvotes: 1

Related Questions