Programming Interactive Web Scripts

32
6 Feb 2002 PL Day 1 Programming Interactive Web Scripts Matthias Felleisen PLT

description

Programming Interactive Web Scripts. Matthias Felleisen PLT. PLT. principles of program design introductory script development principles of programming languages design analysis correctness programming environments & tools. PLT: First Session. principles of program design - PowerPoint PPT Presentation

Transcript of Programming Interactive Web Scripts

Page 1: Programming Interactive Web Scripts

6 Feb 2002

PL Day 1

Programming Interactive Web Scripts

Matthias FelleisenPLT

Page 2: Programming Interactive Web Scripts

6 Feb 2002

PL Day 2

PLT

• principles of program design– introductory– script development

• principles of programming languages– design – analysis – correctness

• programming environments & tools

Page 3: Programming Interactive Web Scripts

6 Feb 2002

PL Day 3

PLT: First Session

• principles of program design– introductory– script development: dynamic Web content

• principles of programming languages– design – analysis – correctness

• programming environments & tools

Page 4: Programming Interactive Web Scripts

6 Feb 2002

PL Day 4

Dynamic Web Content

• USA Today says: “… more than half the pages on the Web are generated on demand.” (July 2000)

• Why? – connection to up-to-date database – determining consumer information – on-line experiments– games

Page 5: Programming Interactive Web Scripts

6 Feb 2002

PL Day 5

Dynamic Web Content

• Technology: Programming, Contents

• Programming first: – CGI– Java Servlets

• Contents first– Active Server Pages– Java Server Pages

Page 6: Programming Interactive Web Scripts

6 Feb 2002

PL Day 6

Dynamic Web Content

• What is a CGI script? a servlet? How do they work? Why is it difficult to design interactive ones?

• Can we automate their development?

• What does this teach us about GUI dialogues in general?

Page 7: Programming Interactive Web Scripts

6 Feb 2002

PL Day 7

The Common Gateway Interface

Page 8: Programming Interactive Web Scripts

6 Feb 2002

PL Day 8

http://www.ccs.neu.edu/cgi-bin/goto.pl

Page 9: Programming Interactive Web Scripts

6 Feb 2002

PL Day 9

Interactive Web Scripts

• Some URLs point to programs.

• The server invokes the program, which writes a page to the standard output port and then terminates.

• The server sents the page to the client that requested the URL.

Page 10: Programming Interactive Web Scripts

6 Feb 2002

PL Day 10

Interactive Web Scripts: Hello World

(printf "My First CGI Page~n”)(printf "Hello World")

Plain Program

(output-http-headers)

(write-xml/content (xexpr->xml '(html (title "My First CGI Page") (body

"Hello World"))))

Web Script

Page 11: Programming Interactive Web Scripts

6 Feb 2002

PL Day 11

Interactive Web Scripts: Server Status

(output-http-headers)

(write-xml/content (xexpr->xml `(html (title "Server Status") (body ,(read-line (first (process "uptime")))))))

(printf "Server Status: ~a~n" (read-line (first (process "uptime")))))))

Page 12: Programming Interactive Web Scripts

6 Feb 2002

PL Day 12

Interactive Web Scripts: Multiply-by-10.com

(output-http-headers)

(define (get-number) (get-binding ‘NUM (bindings)))

(write-xml/content (xexpr->xml `(html (title "Multiply-by-10.com") (body

"the result is: " ,(* (get-number) 10)))))

(define (get-number) (printf "Enter a number ") (flush-output) (read))

(printf "Multiply-by-10.com")(printf "the result is: ~a" (* (get-number) 10) )

Page 13: Programming Interactive Web Scripts

6 Feb 2002

PL Day 13

Interactive Web Scripts: Multiply-by-10.com

<html> <head><title>Multiply-by-10.com</title></head> <body> <h1>Multiply</h1>

<form method="get" action="http://www. .../mult-by-10.ss"> Enter a number <input type="text" name= "NUM" value="0"> </form> </body></html>

Where do babies come from?

Page 14: Programming Interactive Web Scripts

6 Feb 2002

PL Day 14

Interactive Web Scripts: Multiply-by-10.com

Page 15: Programming Interactive Web Scripts

6 Feb 2002

PL Day 15

Interactive Web Scripts: Multiply.com

(output-http-headers)… (write-xml/content (xexpr->xml `(html (title "Multiply.com") (body

"the result is: " ,(* (get-number)

(get-number))))))

(define (get-number) (printf "Enter a number ") (flush-output) (read))

(printf "Multiply.com")(printf "the result is: ~a" (* (get-number) (get-number)) )

Page 16: Programming Interactive Web Scripts

6 Feb 2002

PL Day 16

Interactive Web Scripts: DontMultiply.com

• A CGI script terminates after producing a page.

• To query the consumer, the script must produce a page with a form.

• Ergo: To interact, a CGI program must terminate.

Page 17: Programming Interactive Web Scripts

6 Feb 2002

PL Day 17

Interactive Web Scripts: DontMultipy.com

The natural structure of an interactiveWeb program doesn’t work naturallywith the Common Gateway Interface.

Page 18: Programming Interactive Web Scripts

6 Feb 2002

PL Day 18

Interactive Web Scripts: Interaction

• Current solution: programmers invert program by hand.

• A program with N interaction points becomes a set of N programs.

• The programs communicate all necessary data “manually”.

Page 19: Programming Interactive Web Scripts

6 Feb 2002

PL Day 19

Interacting with Web Scripts

Page 20: Programming Interactive Web Scripts

6 Feb 2002

PL Day 20

Interacting with Web Scripts

Page 21: Programming Interactive Web Scripts

6 Feb 2002

PL Day 21

Interacting with Web Scripts

Page 22: Programming Interactive Web Scripts

6 Feb 2002

PL Day 22

Interacting with Web Scripts

And now we go BACK:

Page 23: Programming Interactive Web Scripts

6 Feb 2002

PL Day 23

Interacting with Web Scripts

Page 24: Programming Interactive Web Scripts

6 Feb 2002

PL Day 24

Interactive Web Scripts: Multiply.com

<html> <head><title>Multiply</title></head> <body> <h1>Multiply</h1>

<form method="get" action="http://www. .../multiply.ss"> Enter a number <input type="text" name= " NUM" value="0"> </form> </body></html>

Page 25: Programming Interactive Web Scripts

6 Feb 2002

PL Day 25

Interactive Web Scripts: cgi-first.ss

(output-http-headers)

(write-xml/content (xexpr->xml `(html (title "The Multiply Page") (body `(form ([method "get"][action "http://.../cgi-second.ss"])

(p "Enter the second number") (input ([type "hidden"][name "first"] [value ,(get-number)])) (input ([type "text"] [name "second"][value "1"])))))))

Page 26: Programming Interactive Web Scripts

6 Feb 2002

PL Day 26

Interactive Web Scripts: Multiply.com

<html> <head><title>Multiply</title></head> <body> <h1>Multiply</h1>

<form method="get" action="http://www. .../multiply.ss"> Enter the second number <input type="text" name= "second" value="0"> <input type= "hidden" name= "first" value= ”XXXXXX."> </form> </body></html>

Page 27: Programming Interactive Web Scripts

6 Feb 2002

PL Day 27

Interactive Web Scripts: cgi-second.ss

(output-http-headers)

(write-xml/content (xexpr->xml `(html (title "The Multiply Page") (body

"the result is: ",(* (get-number 'first) (get-number ’NUM))))))))

Page 28: Programming Interactive Web Scripts

6 Feb 2002

PL Day 28

Interactive Web Scripts and Continuations

(define multiply (lambda (x y) (* x y)))

(define multiply (lambda (x) (lambda (y) (* x y))))

(define multiply-by-22 (lambda (y) (* 22 y)))

Page 29: Programming Interactive Web Scripts

6 Feb 2002

PL Day 29

Interactive Web Scripts and Continuations

cgi script consumer cgi script consumer

the back button

Page 30: Programming Interactive Web Scripts

6 Feb 2002

PL Day 30

Interactive Web Scripts and Continuations

cgi script consumer cgi script consumer

the back button

cloning

Page 31: Programming Interactive Web Scripts

6 Feb 2002

PL Day 31

PLT’s Interactive Web Script

• Solution 1: Design Web server that supports coroutining. Use continuation objects. Graunke et al. ESOP 2001

• Solution 2: Design transformation that automatically inverts programs with N interaction points into N programs. Graunke et al. ASE 2001 & Jacob Matthews PL Day 2002

Page 32: Programming Interactive Web Scripts

6 Feb 2002

PL Day 32

PLT’s Interactive GUI Scripts

• Generalize this technique to certain GUI programs.

• Graunke and Krishnamurthi, ICSE 2002. Graunke PL Day 2002.