Pnuts allows simple and easy servlet programming. User can test his/her servlet easily without compiling and installing the servlet class. Also the rich functionalities of the Java Servlet API are all available to the scripts.
init-param the meaning module If specified, the module is added to the context. initialScript If specified, the script is loaded when the servlet is initialized. compile If true, scripts are compiled into Java bytecode includeLineNo If both compile and includeLineNo are true, line number information is generated by the compiler.
If an 'Alias' is defined in the Web server configuration, the servlet container may not be able to find the script file which corresponds to a servlet script URI.
In Pnuts, the following variables are predefined for servlet scripts.
- request
- The ServletRequest object passed to the Servlet.service() method
- response
- The ServletResponse object passed to the Servlet.service() method
- this
- The Servlet object.
For example, the following script is a simple "hello world" servlet. The default output stream of the script is used to write document data back to Web clients.
e.g. (${TOMCAT}/webapps/test/hello.pnut)
response.setContentType("text/html") println(` <HTML> <BODY> Hello World </BODY> </HTML> `)
Note that the default output stream is a PrintWriter object. If OutputStream is required in a method call, response.getOutputStream() should be used instead of the default output stream.
e.g. (${TOMCAT}/webapps/test/image.pnut)
response.setContentType("image/png") import("java.awt.Color") im = makeImage(20, 20, function (g) { g.setColor(Color::white) g.fillRect(0, 0, 20, 20) g.setColor(Color::orange) g.fillOval(0, 0, 20, 20) }) writeImage(im, "image/png", response.getOutputStream())
Top-level variables and functions in servlet scripts are defined in an anonymous package assigned for the servlet script, so that they do not interfere with other servlets.
Modules that a servlet script uses are separated from modules of other servlet scripts. So even if two modules define two different functions as a same name, the functions are defined separately and they won't be overwritten.
use("foo") ... func() ...
use("bar") ... func() ...
The directory in which the script is located is automatically added to the classpath by setClassPath(). Therefore, servlet script can use Java classes in the same directory. Also it can load other scripts in the directory with load(String, Context) function.
load("otherScript", getContext())
debug.pnut:
import("pnuts.tools.VisualDebugger") param = readGetParameters(request, "ISO8859_1") script = param.get("script") load(script, VisualDebugger().createContext(getContext()))
|
See also "Debugging Scripts".