► |  | Web Server: PLT HTTP Server

1 | Running the Web Server
2 | Writing Servlets
3 | Extending the Web Server
4 | Troubleshooting and Tips
5 | Acknowledgements
| Index

► | 2 | Writing Servlets

2.1 | Defining a Servlet
2.2 | APIs
2.3 | Common Contracts
2.4 | HTTP
2.5 | Web Interaction
2.6 | Stateless Web Interaction
2.7 | Web Cells
2.8 | File Boxes
2.9 | Stateless Web Parameters
2.10 | Formlets
2.11 | Templates
2.12 | Continuation Managers

▼ | 2.1 | Defining a Servlet

2.1.1 | Version 1 Servlets
2.1.2 | Version 2 Servlets
2.1.3 | Stateless Servlets

► | 2.1.3 | Stateless Servlets

2.1.3.1 | Usage Considerations

On this page:

interface-version
start
2.1.3.1`` Usage Considerations

Version: 4.1.4
top← prev up next →

##### 2.1.3`` Stateless Servlets

interface-version : (one-of/c 'stateless)

This indicates that the servlet is a stateless servlet.

(start initial-request) → response?
initial-request : request?

This function is called when an instance of this servlet is started. The argument is the HTTP request that initiated the instance.

An example 'stateless servlet module:

#lang web-server
(define interface-version 'stateless)
(define (start req)
`(html (body (h2 "Look ma, no state!"))))

The web-server language automatically provides the web-server/lang/lang-api API.

##### 2.1.3.1`` Usage Considerations

#lang web-server

A servlet has the following process performed on it automatically:

- All uses of letrec are removed and replaced with equivalent uses of let and imperative features. ("lang/elim-letrec.ss")

- The program is converted into ANF (Administrative Normal Form), making all continuations explicit. ("lang/anormal.ss")

- All continuations (and other continuations marks) are recorded in the continuation marks of the expression they are the continuation of. ("lang/elim-callcc.ss")

- All calls to external modules are identified and marked. ("lang/elim-callcc.ss")

- All uses of call/cc are removed and replaced with equivalent gathering of the continuations through the continuation-marks. ("lang/elim-callcc.ss")

- The program is defunctionalized with a serializable data-structure for each anonymous lambda. ("lang/defun.ss")

This process allows the continuations captured by your servlet to be serialized. This means they may be stored on the client’s browser or the server’s disk. Thus, your servlet has no cost to the server other than execution. This is very attractive if you’ve used Scheme servlets and had memory problems.

This process IS defined on all of PLT Scheme and occurs AFTER macro-expansion, so you are free to use all interesting features of PLT Scheme. However, there are some considerations you must make.

First, this process drastically changes the structure of your program. It will create an immense number of lambdas and structures your program did not normally contain. The performance implication of this has not been studied with PLT Scheme. However, it is theoretically a benefit. The main implications would be due to optimizations MzScheme attempts to perform that will no longer apply. Ideally, your program should be optimized first.

Second, the defunctionalization process is sensitive to the syntactic structure of your program. Therefore, if you change your program in a trivial way, for example, changing a constant, then all serialized continuations will be obsolete and will error when deserialization is attempted. This is a feature, not a bug!

Third, the values in the lexical scope of your continuations must be serializable for the continuations itself to be serializable. This means that you must use define-serializable-struct rather than define-struct, and take care to use modules that do the same. Similarly, you may not use parameterize, because parameterizations are not serializable.

Fourth, and related, this process only runs on your code, not on the code you require. Thus, your continuations – to be capturable – must not be in the context of another module. For example, the following will not work:

(define requests
(map (lambda (rg) (send/suspend/url rg))
response-generators))

because map is not transformed by the process. However, if you defined your own map function, there would be no problem.

Fifth, the store is NOT serialized. If you rely on the store you will be taking huge risks. You will be assuming that the serialized continuation is invoked before the server is restarted or the memory is garbage collected.

This process is derived from the paper Continuations from Generalized Stack Inspection. We thank Greg Pettyjohn for his initial implementation of this algorithm.

top← prev up next →