pnuts.servlet module is used for Web Scripting. This module requires J2SE environment.
readPostParameters() reads the POST data sent by web client. readGetParameters() reads GET parameters. readParameters() reads both POST data and GET parameters.
The return value is a Map object that stores key-value pairs of Servlet Parameters.
encoding is one of Java's encoding name. When encoding is omitted, 'The default encoding for input' is used.
readPostParameters() and readParameters() can not be mixed with ServletRequest.getParameter() method.
response.setContentType("text/html; charset=UTF8")
if (request.getMethod() == "POST"){
postParam = readPostParameters(request, "UTF8")
param1 = postParam.get("param1")
...
}
getParameter() is a convenient function which retrieve the value of the specified parameter. This function is roughly same as the following expression.
readParameters().param readParameters(request, encoding).param
encoding is one of Java's encoding name. When encoding is omitted, 'The default encoding for input' is used.
makeQueryString() returns a query string in x-www-form-urlencoded format, from the specified map which defines parameter-value pairs .
encoding is one of Java's encoding name. When encoding is omitted, 'The default encoding for output' is used.
parseQueryString() makes a Map object which holds parameter-value pairs from queryString in x-www-form-urlencoded format.
encoding is one of Java's encoding name. When encoding is omitted, 'The default encoding for input' is used.
encodeURL() converts a plain text to x-www-form-urlencoded text.
encoding is one of Java's encoding name. When encoding is omitted, 'The default encoding for output' is used.
encodeURL("How are you?", "UTF-8") ==> "How+are+you%3F"
decodeURL() converts x-www-form-urlencoded text to plain text.
encoding is one of Java's encoding name. When encoding is omitted, 'The default encoding for input' is used.
decodeURL("How+are+you%3F", "UTF-8") ==> "How are you?"
addCookie() adds a Cookie. name and value are encoded in UTF8 and then translated into x-www-form-urlencoded format.
Forwards a request to another resource (servlet script file, or other file) on the server.
getCookie() gets a Cookie value. It assumes that the cookie name and the value are encoded in UTF8 and transformed into x-www-form-urlencoded format.
When no argument is specified, getFile() returns a java.io.File object which represents the servlet script file.
When one argument is given, it returns a java.io.File object which represents the file in the servlet directory.
To execute a part of servlet script exclusively, use sync() function with the value of getFile() function. For example, a simple access counter can be written as follows.
function _addCount(){
count = readCount()
writeCount(++count)
}
addCount = sync(_addCount, getFile())
addCount()
addCount = sync(function (){
count = readCount()
writeCount(++count)
}, getFile())
addCount()
getURL() returns a java.net.URL object. If two arguments are specified, the resulting URL is made up of the base URL specified in the 1st argument and the relative path specifed in the 2nd argument.
If one argument is specifed, it returns the URL specified as path, which is relative to the base URL of the servlet script.
When a period (.) is specified for name, it returns the base URL of the servlet script.
When no parameter is specified, it returns the URL of the servlet script.
readMultipartRequest() processes a multipart request such as file uploads. handler should be a callback function which takes the following parameters.
- stream
- InputStream of the uploaded contents
- nameAttribute
- The NAME attribute of <INPUT TYPE=file> tag
- pathName
- The original path name of the uploaded file.
- contentType
- CONTENT-TYPE header value
readMultipartRequest(
function (in, name, path, contentType){
i1 = path.lastIndexOf('/')
i2 = i2 = path.lastIndexOf('\\')
i = 0
if (i1 > i) i = i1
if (i2 > i) i = i2
filename = path[i..]
read(in, fd = open(File("/tmp", filename), "w"))
fd.close()
}
)
<FORM METHOD=POST ENCTYPE=multipart/form-data ACTION="/save.pnut"> <INPUT TYPE=file SIZE=30 NAME="upload"> <INPUT TYPE=reset VALUE="Clear the field"><br> <INPUT TYPE=submit VALUE="Upload"> </FORM>
getRequest() returns the ServletRequest object which represents the current request.
getResponse() returns the ServletResponse object which represents the current response.
Returns a pnuts.lang.Package object that represents the request scope.
sendRedirect() sends a temporary redirect response to the client using the specified redirect location URL.
getSession() returns the current HttpSession associated with the current request or, if there is no current session and create is true (default), returns a new session. If create is false and the request has no valid HttpSession, this function returns null.
getSessionMap() returns a Map object to access the current HttpSession. If no session is found, this function returns an empty Map object.
m = getSessionMap() check(m.userName, m.passsword)
escape() replaces the special characters included in the specified string with their escaped form. unescape() replaces all escaped characters included in the specifed string with their literal form.
escape(`<a href="index.pnut">Home</a>`)
===> <a href="index.pnut"e;>Home</a>
unescape("<a href="index.pnut"e;>Home</a>")
===> <a href="index.pnut">Home</a>
convertDynamicPage() converts a HTML file which special tag elements, <% .. %>, are embedded, to the equivalent script.
If output is specified, the converted data is written to the stream. Otherwise, it returns a java.io.PipedReader object which can be read with read() function.
<HR><%= Date() %>
read(convertDynamicPage(open("footer.pea")))
==> print("<HR>", Date() )
readDynamicPage() reads a dynamic page and returns an Executable object, which can be executed by run() function.
When the servlet initial parameter 'compile' is true, the given dynamic page is compiled.
page = readDynamicPage("footer.pea")
...
run(page)
sendPostRequest() sends a POST request to the specified url.
url = getURL("http://pnuts.org/wiki/index.pnut?action=find+in+content")
params = map(`pattern="pnuts"`)
sendPostRequest(url, params, "UTF-8", function (con) read(con.inputStream))