Pnuts allows simple and easy servlet programming. User can try 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.
In Pnuts, the following two variables are predefined for servlet scripts.
- request
- ServletRequest object passed to the Servlet.service() method
- response
- ServletResponse object passed to the Servlet.service() method
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.
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") script = request.getParameter("script") load(script, VisualDebugger().createContext(getContext()))
|