pnuts.servlet module

pnuts.servlet module is used for Web Scripting. This module requires J2SE environment.

readParameters ( { ServletRequest request {, String encoding }})
readGetParameters ( { ServletRequest request {, String encoding }})
readPostParameters ( { ServletRequest request {, String encoding }})

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 ( String param {, String encoding } )

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 ( Map map {, String encoding } )

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 ( String queryString {, String encoding } )

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 ( String str {, String encoding } )

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 ( String urlEncodedString {, String encoding } )

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 (String name, String value {, int maxAge })

addCookie() adds a Cookie. name and value are encoded in UTF8 and then translated into x-www-form-urlencoded format.

forward (String path)

Forwards a request to another resource (servlet script file, or other file) on the server.

getCookie (String name)

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.

getFile ( { String name } )

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()
Alternatively,
addCount = sync(function (){
                 count = readCount()
                 writeCount(++count)
                }, getFile())
addCount()
getURL( { String urlString { , String path } } ) or
( { File file { , String path } } ) or
( { URL url { , String path } })

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 ( { ServletRequest request, }
handler (
InputStream stream,
String nameAttribute,
String pathName,
String contentType
) )

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+1..]
     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 ( )

getRequest() returns the ServletRequest object which represents the current request.

getResponse ( )

getResponse() returns the ServletResponse object which represents the current response.

requestScope ( )

Returns a pnuts.lang.Package object that represents the request scope.

sendRedirect ( String url ) or
( URL url )

sendRedirect() sends a temporary redirect response to the client using the specified redirect location URL.

getSession ( { boolean create } )
getSessionMap ( { boolean create } )

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.

e.g.
m = getSessionMap()
check(m.userName, m.passsword)
escape ( String str )
unescape ( String str )

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>`)
    ===> &lt;a href=&quot;index.pnut&quote;&gt;Home&lt;/a&gt;

unescape("&lt;a href=&quot;index.pnut&quote;&gt;Home&lt;/a&gt;")
    ===> <a href="index.pnut">Home</a>
convertDynamicPage (
( InputStream | Reader | File | String ) input
{ , ( OutputStream | Writer | File | String ) output } )

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.

footer.pea
<HR><%= Date() %>
read(convertDynamicPage(open("footer.pea")))

==> print("<HR>", Date() )
readDynamicPage ( ( InputStream | Reader | File | URL | String ) input { , String encoding { , boolean reloadUpdatedScripts } } )

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)

If the character encoding of the input is not the system encoding, it should be specified in encoding .

When reloadUpdatedScripts is true, the last modified time of .pea files are checked, and if any .pea file included has been updated since last loaded, the whole page will be re-constructed.

sendPostRequest ( URL url, Map parameters, String encoding { , handler( URLConnection urlConnection )} )

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))

The default encoding for input

  1. The result of request.getCharacterEncoding(), if it is non-null.
  2. The result of response.getCharacterEncoding(), if it is non-null.
  3. UTF8

The default encoding for output

  1. The result of response.getCharacterEncoding(), if it is non-null.
  2. UTF8