I am wondering how to install a Javascript runtime engine to work with an Apache HTTP server. My aim is to run server-side Javascript.
Thanks in advance for any help!
I think you may have to clarify why you want to do this.
If you're just looking to execute JavaScript code, Rhino will get you the ability to run javascript within a Java application. However, since its not in a web browser you won't have any access to any sort of DOM.
Other than that, JavaScript is mostly a language that was intended to run in a web browser on the client.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am learning java 8 Nashorn Javascript engine. But i am not getting what is the practical useof it. Here are my specific
questions
Calling JavaScript from Java
1) What is the practical scenario when as a developer i would like to call javascript from java ?
Calling Java from Javascript
2) What is the practical scenario when as a developer i would like to call java from javascript ?
3) If yes, How browser will execute java from javascript. Does system hosting needs to have JRE and browser will use that ?
1) What is the practical scenario when as a developer i would like to call javascript from java ?
Suppose that you're writing a browser in Java then you'll need to run JS interpreter from Java to display the page and handle JS code.
Suppose you want to provide a JS syntax checker in Java for example like JS editors in eclipse.
Or you want to run a tutorial website of JS where users write JS code in Browser and you evaluate it and show errors like it's done by most of the sites like Learnstreet, Udacity.
As a back end (server side) developer you have to provide a functionality to you front end (client side) developer where they can send you JS code and you do stuff with it on server. Service Now and Presto Mashups are a few examples where it's done.
There is a JS API that you want to use without having to convert it to Java code first. It's common pattern to support your JS library for Java and several other languages now, so that they can be run with JS engines like Mozilla Rhino and Oracle Nashorn without much hasstle.
2) What is the practical scenario when as a developer i would like to call java from javascript ?
You'll have entire Java API at your finger tips while writing JS code, answer is why not?
You're running some JS code on the Server using JS engine and you need to communicate output back to JVM or invoke some event. May be even listing directories, reading files calling database is the requirement from JS code.
Consider Node JS, it's just a JS interpreter written in C, C++ It's doing the same thing. It's providing power of C and C++ to a JS developer. Same is done with the Rhino and Nashorn. By it's nature JS has a limited functionality as its used to run on brower, it needs some additional APIs to get a power boost. I took the example of Node JS as it's popularity shows just how much this power up was needed. Just for fun try this video interview Ryan Dahl.
3) If yes, How browser will execute java from javascript. Does system hosting needs to have JRE and browser will use that ?
Browser will not run JS code containing Java parts. JS engines are meant to get the JS code on server side. This gives a JS developer an opportunity to use his JS skills to write server side code. However if needed browser can send the JS code to server for interpretation as in case of Presto Jackbe Mashups.
I have no experience with calling java from Javascript. However I have seen Javascript being called from Java. The use case I have seen is where a Java framework needed to be extended by end clients. This was achieved by letting the end clients add logic in Javascript. The actual Javascript code could then be stored in the database and executed when required by the framework.
1) A practical use of a JavaScript engine in Java would be when creating or using an HTTP Adapter. In this case, you would need to be able to handle all javascript, to simulate a real browser connection. An example of a Java HTTP Adapter that deals with JavaScript is HTMLUnit.
2) I do not believe this is used very often, or at all, especially with the disapproval of the NPAPI plugin by Chrome and, in the future, Firefox (source). Despite these points, communicating between javascript and java could be used to add functionality to a website or application, although this does not seem feasible.
3) See above.
I have a little question here. Is there any way to build a server side JS without any use of external software. The reason for this is I am not able to install any software on the webspace but I wanted to experiment with realtime communication with the server and another user for a game or something like this.
If someone knows a way to establish something like this I would be quite happy to hear about it.
EDIT: Guys NOT NodeJS I AM NOT ALLOWED TO INSTALL ANY OTHER SOFTWARE!
https://cloud.google.com/nodejs/
or
https://devcenter.heroku.com/articles/getting-started-with-nodejs#introduction
Those $15 a year webhosts typically don't allow you advanced customization. You need a better web hosting service that allows you CLI access to the server back end.
Running JavaScript on backend requires NodeJS runtime environment. Download it here https://nodejs.org/en/
Alternatively you can can configure an instance on Heroku and run your application there. But I would suggest to try the first one.
No. Javascript is a scripting language that was designed to work within the browser. There's no out of the box solution for you, as Javascript doesn't do server-side out of the box
However, Node.Js (and others) use Chrome's V8 engine to create an event-driven runtime environment which can allow writing server-side code with Javascript. That's your best option
I've got a simple Javascript application with a JSON API. Currently it runs in the client, but I'd like to move it from the client to the server. I am accustomed to learning new platforms, but in this case, my time is very limited - so I need to find the absolute simplest way possible.
This should be an easy task, but all I'm finding are solutions that are way overcomplicated:
The application is currently hosted on an extremely basic server. Node.js is not available, and I do not have install privileges. I'll eventually move it to a different server, but I really don't know what will be available there.
Many solutions require installing and running a standalone server. Really? Just to evaluate Javascript server-side and spit out some data?
I can run Python and PHP, and I see that it's possible to call Javascript from inside a Python or PHP script. However, the specific Python solution that I've found also require installing some Python support via pip or easy-install, so probably not an option. Also, this just feels overcomplicated, and I'm concerned about setting myself up for errors such as data conversion or permissions, etc.
Any help?
#Quentin is correct. There is no way to run javascript on a server without a javascript interpreter on the server.
Node.js is not only the most robust and widely used one, it's also the simplest. It is certainly possible to write your own javascript interpreter in PHP or Python, but that would be much more complicated than using Node.js.
Try really hard to find a server solution that allows you to use Node. In the end, it's going to save you (and any other stakeholders interested in the project) a lot of time and money.
I'm attempting to write a particular script that logs into a website. This specific website contains a Javascript form so I had little to no luck by making use of "mechanize".
I'm curious if there exist other solutions that I may be unaware of that would help me in my situation. If this particular question or some related variant has been asked here before, please excuse me, and I would prefer the link to this particular query. Otherwise, what are some common techniques/approaches for dealing with this issue?
Thanks.
I've recently been using PhantomJS for this kind of work - it's a command-line tool that allows you to run Javascript in a browser environment (based on Webkit). This allows you to do scraping and online interactions that require Javascript-enabled interfaces. There's a Python-based implementation here that's fully compatible with the API of the C++ version, or you could run either version in Python via subprocess.
Depending on what you're trying to do, another good option might be to use Selenium, which has client driver implementation in Python - it's meant for integration testing, but can do a lot of automation as long as you're okay running the Java-based Selenium Server and having the automation happen in an open browser rather than as a background process.
I'm currently looking for a new web stack to build a hobby project on and would like it to be powered by JavaScript. I've had a quick look at Nitro, NarwhalJS etc. but was wondering if anyone had any solid recommendations or experience of an entire end-to-end javascript/json architecture ( jquery, middleware, standard libraries, db etc.) that they could share.
I'd prefer it to be a stack that you think is going to grow and is actively being looked after, documentation, community of nice like-minded individuals etc.
Thanks in advance.
Caveat: This answer somewhat fails to meet your basic requirement of personal experience with the resources listed. :-)
Off-the-cuff, there's Jaxer from Aptana and Chrome Server (which, despite the name, isn't related to Google Chrome AFAIK). Speaking of Chrome, though, there's an Apache CGI module that supports server-side scripting using JavaScript via Google's V8 engine, but that doesn't help you much with DB connectivity and such.
I'll also plug Java and Rhino, which I have used a bit. Via Rhino, you can compile JavaScript into Java bytecode (which, if you're using the Sun runtime, is JIT-compiled to machine code by Hotspot). That means you can run JavaScript in any servlet container (Tomcat, Resin, etc.). The joy here is that all of the huge array of goodies available for Java is instantly available to you via JavaScript -- so, MySQL connectors, image manipulation libraries, just about anything you can imagine. It's also amazingly easy to access those resources from JavaScript via Rhino. To give you an idea:
importPackage(java.io);
var f = new FileWriter("test.txt");
f.write("Testing 1 2 3");
f.close();
...and we've just written a file on the server via JavaScript, using Java's java.io.FileWriter class. You can also execute dynamic scripts at runtime via the javax.script package, which (for JavaScript) uses Rhino under the covers, although I'm not immediately coming up with a use case for doing that. :-)
For the database part of the stack:
Couchdb uses JSON and REST to store data in a document format. It uses PUT,DELETE for storage - I'm not sure how that would work with Javascript.
Helma should work well as a web server layer. It streamlines the use of Rhino as the web tier logic language.
I'm building a new service called PageForest that helps developers write totally client-side javascript programs, with PageForest providing storage and user management. Here's a sample page:
http://pfsamples.googlecode.com/svn/trunk/SAMTable/index.html#mckoss_16
This is still a work in progress, but I'd love to get some feedback on the approach. You can find more samples at the pfsamples.googlecode.com site.
Check out JScript / Windows Scripting Host(wsh) and possibly HTA's. HTA's can actually be served via a webserver and act as a locally running application with extended rights. If you want pure web development you can use WSH and some IIS tweaking to process server requests on the back end with pure javascript code in a WSF file. WSH also provides access to the file system, ODBC compliant databases and a slew of other COM exposed applications via the ActiveX model. We're not talking blazing speed, but you're programming in javascript to begin with.
Here are some links on the "stack"
http://msdn.microsoft.com/en-us/library/ms536496(VS.85).aspx
http://msdn.microsoft.com/en-us/library/15x4407c(VS.85).aspx
How about using
GWT-Spring-Hibernate-MYSql