File: servlet2.html

package info (click to toggle)
drscheme 1%3A352-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 71,608 kB
  • ctags: 55,284
  • sloc: ansic: 278,966; cpp: 63,318; sh: 32,265; lisp: 14,530; asm: 7,327; makefile: 4,846; pascal: 4,363; perl: 2,920; java: 1,632; yacc: 755; lex: 258; sed: 93; xml: 12
file content (227 lines) | stat: -rw-r--r-- 8,128 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<html>
<head>
<title>Teachpack : Simplified Scheme Web Servlets</title>
</head>
<body bgcolor="#ffffff" text="#000000"
      link="#009900" vlink="#007700" alink="#cc0000">

<a href="index.html">Teachpacks for How to Design Programs</a>

<h1>Simplified Scheme Web Servlets</h1>

<hr> <h3><a name="servlet2.ss">servlet2.ss</a></h3> <!-- DOCNOTE="teach=servlet2.ss" -->

<p>This teachpack provides a simplified API for PLT Scheme servlets. The
teachpack does not require any understanding of HTML and higher-order
functions. It uses structures and lists, and is therefore well-suited for
use with an HtDP course.

<hr>
<h3>Interface</h3>

<pre>
   ; Data Constructors  -------------------------------------------------------
   <a name="make-password">make-password</a> ; asking for a password
   <a name="make-number">make-number</a>   ; asking for a number
   <a name="make-yes-no">make-yes-no</a>   ; asking for a Yes-No style answer
   <a name="make-boolean">make-boolean</a>  ; asking for a true-false style answer
   <a name="make-radio">make-radio</a>    ; requesting one choice from some mutually exclusive choices

   #| Data Definitions --------------------------------------------------------

   Forms ----------------------------------------------------------------------
   Query    = (union String
                (make-password String)
                (make-number   String)
                (make-boolean  String)
                (make-yes-no   String String String)
                (make-radio    String (cons String (listof String))))
   FormItem = (list Symbol Query)
   Form     = (cons FormItem (listof FormItem))

   Table -------------------------------------------------------------------
   Response = (union String Number Boolean)
   Table    = (listof (list Symbol Response))
   |#

   ; Functions ----------------------------------------------------------------

   <a name="form?">form?</a>               ; TST -> Boolean
   ; checking the structure of forms

   <a name="form-element?">form-element?</a>       ; TST -> Boolean
   ; checking the structure of form-elements

   <a name="single-query">single-query</a>        ; Query -> Response
   ; posing a single question

   <a name="queries">queries</a>             ; (listof Query) -> (listof Response)
   ; posing a list of questions

   <a name="echo-answers">echo-answers</a>        ; (listof Response) -> true
   ; echoing the answers from a list of questions to a Web page (development)

   <a name="form-query">form-query</a>          ; Form -> Table
   ; posing a list of questions with tag

   <a name="extract/single">extract/single</a>      ; Symbol Table -> Response
   ; extracting the answer for a given tag from a response; the tag must
   ; occur exactly once.

   <a name="extract">extract</a>             ; Symbol Table -> (listof Response)
   ; extracting all the answers for a given tag from a response; the list
   ; is empty if there is no such tag.

   <a name="echo-response">echo-response</a>       ; Table -> true
   ; echoing the response from a form to a Web page (development)

   <a name="inform">inform</a>              ; String String *-> true
   ; posting some information on a Web page, wait for continue signal

   <a name="final-page">final-page</a>          ; String String *-> true
   ; posting some information on a Web page,
   ; killing the script and all associated backtrack/clone points

</pre>
<hr>
<h3>Pragmatics</h3>

<p>The following table specifies what kind of answer each form of query
produces:

<pre>
  Query                                      GUI Item         Response
________________________________________________________________________________
  String                                     text field       String
  (make-password String)                     password field   String
  (make-number   String)                     text field       Number
  (make-boolean  String)                     check box        Boolean
  (make-yes-no   String String1 String2)     2-pronged radio  String1 or String2
  (make-radio    String loString)            radio button     one of loString
  (make-button   String0)                    submit button    String0
</pre>

<p>The functions play the following role:
<ul>
<li>
   <code>form?</code> and <code>form-element?</code> enable programmers to
   check the well-formedness of their queries.

<li>
   <code>single-query</code> poses a question and waits for a answer from
   the consumer.

<li>
   <code>queries</code> poses a list of questions and waits for the
   submission of answers; it produces a list of answers that is as
   long as the list of given questions.

<li>
   <code>echo-answers</code> echoes a list of answers to a Web page; this
   function is useful for testing Web form designs.

<li>
   <code>form-query</code> poses a form with queries and waits for the
   submission of answers; it produces a list of tagged or labeled answers
   that is as long as the list of given questions.

<li>
   <code>echo-response</code> echoes a response to a Web page as a table.

<li>
   <code>inform</code> posts some information on a Web page and wait for a
   continue signal (a link) from the consumer; the first string plays the
   role of a title and the rest form a paragraph.

<li>
   <code>final-page</code> posts some information on a Web page and
   shuts down the script and all associated backtrack/clone points.
</ul>


<h3>Example</h3>

<p>The following example illustrates how the library enables programs to
interact with the consumer. In particular, note the properly recursive
calls to <code>login</code> in <code>inform-error</code>. Also note how the
primitives <code>make-password</code> and <code>make-number</code> deliver
strings and numbers, respectively.

<pre>
#| Login Site -----------------------------------------------------------------
   Author: Matthias Felleisen
   Language: Intermediate
   Teachpack: servlet2.ss

   Login Web Site:

   A login site consists of three pages:
    1. login: request user information: name, password, year-of-birth
       - if correct, respond with greeting for name
       - if incorrect, go to error page
    2. error: inform user of error, continue goes back to login page
    3. greetings: say hello to name
   The second and third are generated from user input, the first is
   generated by the program alone.

|#

;; Login Page -----------------------------------------------------------------

; ask three questions: one plain, one password, one numeric
(define login-page
  (list
   (list 'name "User Name")
   (list 'pass (make-password "Password"))
   (list 'yofb (make-number "Year-of-birth"))))

; Table -> true
; process the response for login-page
(define (login r)
  (let ([name (extract/single 'name r)])
    (if (string=? (lookup-password name) (extract/single 'pass r))
        (if (= (lookup-yofb name) (extract/single 'yofb r))
            (greetings name)
            (inform-error "bad data"))
        (inform-error "bad match"))))

; String -> (union false String)
; lookup the password for name in DB
(define (lookup-password name)
  (local ([define (x=? x) (string=? name x)]
          [define r (assf x=? DB)])
    (if (boolean? r) false (second r))))

; String -> (union false Number)
; lookup the year-of-birth for name in DB
(define (lookup-yofb name)
  (local ([define (x=? x) (string=? name x)]
          [define r (assf x=? DB)])
    (if (boolean? r) false (third r))))

(define DB
  '(("matthias" "okay" 1958)))

;; Error Page -----------------------------------------------------------------

; String -> true
; consume error message, print and enable return to login page
(define (inform-error s)
  (let ([_ (inform "Error" s)])
    (login (form-query login-page))))

;; Greetings Page -------------------------------------------------------------

; String -> true
; consume name and display good bye message
(define (greetings name)
  (inform "Welcome" "Welcome " name " and good bye."))

;; RUN ------------------------------------------------------------------------
(login (form-query login-page))
</pre>

<br>
<br>
</body>
</html>