Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is server-side javascript used/implemented?

I'm aware of server-side javascript for a long time now, but I don't have a clue about how it works. Could someone point me in the right direction?

I'm interested in how to use server-side javascript + Java Servlet technology

EDIT

Great! I have seen those technologies before, but for some reason I didn't associate them with "server-side" javascript ( doh! )

Complementary question: if I want to use a javascript library to create content ( ExtJs for instance ), am I able to have a "document" to modify in the server-side as I do in the client?

( I guess I'll find out in my first attempts )

like image 937
OscarRyz Avatar asked Oct 20 '09 21:10

OscarRyz


People also ask

How does JavaScript work in server-side?

Server Side JavaScript (SSJS) is an extended version of JavaScript that enables back-end access to databases, file systems, and servers. Server side javascript, is javascript code running over a server local resources , it's just like C# or Java, but the syntax is based on JavaScript. A good example of this is Node.

Is JavaScript used on servers?

JavaScript on the server-side (or outside of the browser) has opened a wide range of applications for the language. And that's one of the reasons it is one of the most popular right now. According to reports, JavaScript is being used on 97% of all websites.

Why is JavaScript used as a server-side language?

The fact that the language itself doesn't contain the browser integration has made it easy to adapt it to other environments, including server side scripting. Node. js is far from the only server side scripting using JavaScript.


Video Answer


2 Answers

Running javascript server side requires a javascript engine that can be embedded. Most of these "embeddable" engines provide an API that lets you interface between the executing javascript code and your own objects/methods. For example, you might have javascript code hooked up to allow the execution of functions written in Java or C#, or you might augment the symbol table of a script to include access to non-javascript objects in your system.

I would take a look at some of these engines, I'm guessing Rhino may be the best fit for you as its written in Java. Their tutorials outline embedding Rhino in a Java environment.

  1. A Rhino embedding tutorial: http://www.mozilla.org/rhino/tutorial.html
  2. V8 is Google's engine. V8 is not currently threadsafe, so it probably won't suit your needs in a server environment. http://code.google.com/p/v8/
  3. SpiderMonkey is the engine powering Firefox's javascript execution. It's straight C. http://www.mozilla.org/js/spidermonkey/

Edit in response to your second question.

I'm not sure exactly what you mean by content generated by a javascript library. You mention ExtJS however, which would imply HTML content I believe?. It's important to understand the difference between the DOM (which Javascript can read and modify but is not "part" of Javascript per se) and Javascript the language. If you need the idea of a DOM server side that's a different story, if you need the Javascript language then the above options should help you out.

like image 190
Matt Baker Avatar answered Nov 02 '22 23:11

Matt Baker


Mozilla's Rhino JavaScript engine is pretty easy to embed; it allows subclassing of Java classes and implementing interfaces, as well as just doing some quick n' dirty JavaScript object trickery. I've been working on embedding it into GeoServer in my off moments for a couple of months now. You can take a look at both the Java code that embeds Rhino and a few JavaScript examples in our SVN repository. Rhino also has a pretty nice guide to getting started.

like image 38
David Winslow Avatar answered Nov 03 '22 00:11

David Winslow