简体   繁体   English

如何使用/实现服务器端的JavaScript?

[英]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. 我很长时间都知道服务器端的javascript,但我不知道它是如何工作的。 Could someone point me in the right direction? 有人能指出我正确的方向吗?

I'm interested in how to use server-side javascript + Java Servlet technology 我对如何使用服务器端javascript + Java Servlet技术感兴趣

EDIT 编辑

Great! 大! I have seen those technologies before, but for some reason I didn't associate them with "server-side" javascript ( doh! ) 我以前见过这些技术,但由于某种原因我没有将它们与“服务器端”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? 补充问题:如果我想使用javascript库来创建内容(例如ExtJs),我是否可以像在客户端一样在服务器端修改“文档”?

( I guess I'll find out in my first attempts ) (我想我会在第一次尝试时发现)

Running javascript server side requires a javascript engine that can be embedded. 运行javascript服务器端需要可嵌入的javascript引擎。 Most of these "embeddable" engines provide an API that lets you interface between the executing javascript code and your own objects/methods. 这些“可嵌入”引擎中的大多数都提供了一个API,允许您在执行的javascript代码和您自己的对象/方法之间进行接口。 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. 例如,您可能连接了javascript代码以允许执行用Java或C#编写的函数,或者您可以扩充脚本的符号表以包含对系统中非JavaScript对象的访问。

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. 我会看看其中的一些引擎,我猜测Rhino可能是最适合你的,因为它是用Java编写的。 Their tutorials outline embedding Rhino in a Java environment. 他们的教程概述了在Java环境中嵌入Rhino。

  1. A Rhino embedding tutorial: http://www.mozilla.org/rhino/tutorial.html Rhino嵌入教程: http//www.mozilla.org/rhino/tutorial.html
  2. V8 is Google's engine. V8是谷歌的引擎。 V8 is not currently threadsafe, so it probably won't suit your needs in a server environment. V8目前不是线程安全的,因此它可能无法满足您在服务器环境中的需求。 http://code.google.com/p/v8/ http://code.google.com/p/v8/
  3. SpiderMonkey is the engine powering Firefox's javascript execution. SpiderMonkey是Firefox的javascript执行的引擎。 It's straight C. http://www.mozilla.org/js/spidermonkey/ 这是直的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. 我不确定javascript库生成的内容究竟是什么意思。 You mention ExtJS however, which would imply HTML content I believe?. 你提到ExtJS,这会暗示我相信的HTML内容吗? 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. 理解DOM(Javascript可以读取和修改但不是Javascript本身的“部分”)和Javascript语言之间的区别非常重要。 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. 如果你需要一个不同故事的DOM服务器端的想法,如果你需要Javascript 语言,那么上面的选项可以帮助你。

Mozilla's Rhino JavaScript engine is pretty easy to embed; Mozilla的Rhino JavaScript引擎很容易嵌入; it allows subclassing of Java classes and implementing interfaces, as well as just doing some quick n' dirty JavaScript object trickery. 它允许子类化Java类和实现接口,以及只是做一些快速的脏JavaScript对象技巧。 I've been working on embedding it into GeoServer in my off moments for a couple of months now. 我一直在努力将它嵌入到GeoServer中几个月。 You can take a look at both the Java code that embeds Rhino and a few JavaScript examples in our SVN repository. 您可以在我们的SVN存储库中查看嵌入Rhino的Java代码和一些JavaScript示例 Rhino also has a pretty nice guide to getting started. Rhino也有很好的入门指南

ASP and ASP.NET supports server-side JavaScript. ASP和ASP.NET支持服务器端JavaScript。 For ASP, all you do is declare: 对于ASP,您所做的只是声明:

<%@LANGUAGE=JAVASCRIPT%>

At the very top of the ASP file and you're coding in JavaScript. 在ASP文件的最顶层,您使用JavaScript进行编码。 ASP.NET is basically the same except that you gain access to the .NET framework. 除了可以访问.NET框架之外,ASP.NET基本相同。

Can't help on the Java servlet front, no experience with it. 无法帮助Java servlet前端,没有经验。

"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是一个服务器端Javascript环境和Web应用程序框架,用于快速高效地编写脚本并为您的网站和Internet应用程序提供服务。” - http://helma.org/ - http://helma.org/

I have extensive experience using server-side Javascript in a Windows environment. 我在Windows环境中使用服务器端Javascript有丰富的经验。 The Windows Scripting Host on all Windows systems provides Javascript as one of the default languages. 所有Windows系统上的Windows Scripting Host都提供Javascript作为默认语言之一。 You can create a COM object to interface with it from any language that supports COM. 您可以使用支持COM的任何语言创建COM对象以与其进行交互。 I think this MSDN page will get you started if you want to use this approach. 我想如果您想使用这种方法, 这个MSDN页面将帮助您入门。

My gut feeling is that you'll be happier if you choose something that doesn't involve COM. 我的直觉是,如果你选择一些不涉及COM的东西,你会更快乐。 I just wanted to make sure you had all the options in front of you. 我只是想确保你面前有所有的选择。

If all you need to do is bless your HTML as Excel, you might be better served sending your HTML to the server and have it served back with a content-disposition header and the appropriate MIME type. 如果您需要做的就是将您的HTML保存为Excel,那么最好将HTML发送到服务器并使用内容处置标头和相应的MIME类型进行回送。 Server-side JS doesn't normally implement a browser-like DOM so Ext isn't going to work server-side. 服务器端JS通常不会实现类似浏览器的DOM,因此Ext不会在服务器端工作。

Alternatively you could generate your HTML serverside and skip the roundtrip. 或者,您可以生成HTML服务器端并跳过往返。 For example the POI Java library can generate real binary Excel files with multiple sheets and cell functions. 例如, POI Java库可以生成具有多个工作表和单元函数的真实二进制Excel文件。

If you really want to use a server-side JS as your app server, consider Myna . 如果您真的想使用服务器端JS作为您的应用服务器,请考虑Myna I mentioned in the this question some of its advantages. 我在这个问题中提到了它的一些优点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM