Question ad "arguments" and ScriptEngine.ARGV
Rony G. Flatscher
Rony.Flatscher at wu.ac.at
Thu Feb 25 15:36:15 UTC 2021
Keep forgetting, that adding boldness breaks the text. Here the JSP text without any styling (hence
without those injected asterisks):
<%@ page session="false" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="/WEB-INF/script-jsr223.tld" prefix="s" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<style>
body { background-color: ivory; }
</style>
<title>Sample Application JSP Page Using JavaScript Via JSR-223 (Title)</title>
</head>
<body>
<div style="float: left; padding: 10px;">
<img src="images/Unofficial_JavaScript_logo_2.png" alt="" />
</div>
<h1>Sample Application JSP Page Using JavaScript Via Script (JSR223)</h1>
This is the output of a JSP page that is part of the Hello, World
application.
<p>Here is the text partially generated by a simple JavaScript script: <br />
<!-- note: a script can be of any size and complexity -->
<s:script type="nashorn">
print("<p>Hello, world, this is Nashorn (JavaScript) speaking at ", Date(), "</p>")
print("<p>arguments: ", arguments, "</p>")
</s:script>
<p>Here is the text generated by a simple JavaScript expression: <br />
<!-- note: an expression only creates a value that is automatically fetched -->
<p>It is now exactly <s:expr type="javascript">Date()</s:expr>.</p>
<p>The following script includes a syntax error (division by zero) intentionally.
The error in the script will not inhibit the creation of the page.</p>
<!-- note: by default an error in a script will not inhibit the creation of the page -->
<s:script type="javascript">
print('cause a Nashorn/JavaScript error ...')
throw Error("This error is just for testing how the host behaves ...")
</s:script>
<p>Done.<p>
</body>
</html>
---rony
On 25.02.2021 16:31, Rony G. Flatscher wrote:
> While using Nashorn from Java 8 in the context of a Java based web server for which I authored a tag
> library for scripting engines, I noted that "arguments" does not get set to the arguments passed via
> the engine ScriptContext [1] entry ScriptEngine.ARGV [2] (i.e. "javax.script.argv" a single
> dimensioned Java array of type Object of positional arguments) when invoking the Nashorn script via
> engine.eval(script).
>
> Is this really to be expected? If so, how can I cause the Nashorn engine to supply the Java
> arguments to the script or have the script fetch the supplied arguments in a different way?
>
> To illustrate, this is an example of three Nashorn fragments in a single JSP (one 'script' element
> with type 'nashorn', one 'expr' element with type 'javascript' and a 'script' element with type
> 'javascript'):
>
> <%@ page session="false" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
> <%@ taglib uri="/WEB-INF/script-jsr223.tld" prefix="s" %>
> <!DOCTYPE html>
> <html>
> <head>
> <meta charset="UTF-8" />
> <style>
> body { background-color: ivory; }
> </style>
> <title>Sample Application JSP Page Using JavaScript Via JSR-223 (Title)</title>
> </head>
> <body>
>
> <div style="float: left; padding: 10px;">
> <img src="images/Unofficial_JavaScript_logo_2.png" alt="" />
> </div>
> <h1>Sample Application JSP Page Using JavaScript Via Script (JSR223)</h1>
> This is the output of a JSP page that is part of the Hello, World
> application.
>
> <p>Here is the text partially generated by a simple JavaScript script: <br />
>
> <!-- note: a script can be of any size and complexity -->
> *<s:script type="nashorn">****print("<p>Hello, world, this is Nashorn (JavaScript) speaking at ", Date(), "</p>")****print("<p>arguments: ", arguments, "</p>")****</s:script>*
>
> <p>Here is the text generated by a simple JavaScript expression: <br />
>
> <!-- note: an expression only creates a value that is automatically fetched -->
> <p>It is now exactly *<s:expr type="javascript">Date()</s:expr>*.</p>
>
> <p>The following script includes a syntax error (division by zero) intentionally.
> The error in the script will not inhibit the creation of the page.</p>
>
> <!-- note: by default an error in a script will not inhibit the creation of the page -->
> *<s:script type="javascript">****print('cause a Nashorn/JavaScript error ...')****throw Error("This error is just for testing how the host behaves ...")****</s:script>*
>
> <p>Done.<p>
>
> </body>
> </html>
>
> The resulting HTML text will show "arguments: " but no arguments going with it, although there are
> three arguments supplied via the engine scope ScriptContext with the entry ScriptEngine.ARGV as
> mentioned above.
>
> Here the resulting HTML text from the JSP above:
>
>
>
> <!DOCTYPE html>
> <html>
> <head>
> <meta charset="UTF-8" />
> <style>
> body { background-color: ivory; }
> </style>
> <title>Sample Application JSP Page Using JavaScript Via JSR-223 (Title)</title>
> </head>
> <body>
>
> <div style="float: left; padding: 10px;">
> <img src="images/Unofficial_JavaScript_logo_2.png" alt="" />
> </div>
> <h1>Sample Application JSP Page Using JavaScript Via Script (JSR223)</h1>
> This is the output of a JSP page that is part of the Hello, World
> application.
>
> <p>Here is the text partially generated by a simple JavaScript script: <br />
>
> <!-- note: a script can be of any size and complexity -->
> <p>Hello, world, this is Nashorn (JavaScript) speaking at Thu Feb 25 2021 16:30:38 GMT+0100 (CET) </p>
> <p>arguments: </p>
>
>
> <p>Here is the text generated by a simple JavaScript expression: <br />
>
> <!-- note: an expression only creates a value that is automatically fetched -->
> <p>It is now exactly Thu Feb 25 2021 16:30:38 GMT+0100 (CET).</p>
>
> <p>The following script includes a syntax error (division by zero) intentionally.
> The error in the script will not inhibit the creation of the page.</p>
>
> <!-- note: by default an error in a script will not inhibit the creation of the page -->
> cause a Nashorn/JavaScript error ...
>
>
> <p>Done.<p>
>
> </body>
> </html>
>
> ---rony
>
> [1] "Java Scripting Framework (JSR-223)":
> <https://docs.oracle.com/javase/8/docs/api/index.html?javax/script/package-summary.html>
> [2] "javax.script.ScriptContext":
> <https://docs.oracle.com/javase/8/docs/api/javax/script/ScriptContext.html>
> [3] "javax.script.ScriptEngine":
> <https://docs.oracle.com/javase/8/docs/api/javax/script/ScriptEngine.html>
> [4] "Nashorn and Shell Scripting":
> <https://docs.oracle.com/javase/10/nashorn/nashorn-and-shell-scripting.htm#JSNUG158>
>
> P.S.: It seems from samples on the Internet that when using the shell to invoke Nashorn that the
> arguments get passed on to the script [4].
>
More information about the nashorn-dev
mailing list