Forms, CGI Objectives DD1335 (Lecture 5) Basic Internet - - PowerPoint PPT Presentation

forms cgi
SMART_READER_LITE
LIVE PREVIEW

Forms, CGI Objectives DD1335 (Lecture 5) Basic Internet - - PowerPoint PPT Presentation

Forms, CGI Forms, CGI Objectives DD1335 (Lecture 5) Basic Internet Programming Spring 2010 1 / 19 Forms, CGI Forms, CGI Objectives The basics of HTML forms DD1335 (Lecture 5) Basic Internet Programming Spring 2010 1 / 19 Forms, CGI


slide-1
SLIDE 1

Forms, CGI

Forms, CGI

Objectives

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 1 / 19

slide-2
SLIDE 2

Forms, CGI

Forms, CGI

Objectives

◮ The basics of HTML forms

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 1 / 19

slide-3
SLIDE 3

Forms, CGI

Forms, CGI

Objectives

◮ The basics of HTML forms ◮ How form content is submitted

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 1 / 19

slide-4
SLIDE 4

Forms, CGI

Forms, CGI

Objectives

◮ The basics of HTML forms ◮ How form content is submitted

◮ GET, POST DD1335 (Lecture 5) Basic Internet Programming Spring 2010 1 / 19

slide-5
SLIDE 5

Forms, CGI

Forms, CGI

Objectives

◮ The basics of HTML forms ◮ How form content is submitted

◮ GET, POST

◮ Elements that you can have in forms

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 1 / 19

slide-6
SLIDE 6

Forms, CGI

Forms, CGI

Objectives

◮ The basics of HTML forms ◮ How form content is submitted

◮ GET, POST

◮ Elements that you can have in forms ◮ Responding to forms

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 1 / 19

slide-7
SLIDE 7

Forms, CGI

Forms, CGI

Objectives

◮ The basics of HTML forms ◮ How form content is submitted

◮ GET, POST

◮ Elements that you can have in forms ◮ Responding to forms

◮ CGI – the Common Gateway Interface DD1335 (Lecture 5) Basic Internet Programming Spring 2010 1 / 19

slide-8
SLIDE 8

Forms, CGI

Forms, CGI

Objectives

◮ The basics of HTML forms ◮ How form content is submitted

◮ GET, POST

◮ Elements that you can have in forms ◮ Responding to forms

◮ CGI – the Common Gateway Interface ◮ Later: Servlets DD1335 (Lecture 5) Basic Internet Programming Spring 2010 1 / 19

slide-9
SLIDE 9

Forms, CGI

Forms, CGI

Objectives

◮ The basics of HTML forms ◮ How form content is submitted

◮ GET, POST

◮ Elements that you can have in forms ◮ Responding to forms

◮ CGI – the Common Gateway Interface ◮ Later: Servlets

◮ Generation of dynamic Web content

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 1 / 19

slide-10
SLIDE 10

Forms, CGI

HTML forms

◮ In most internet programming, you need the user to enter data

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 2 / 19

slide-11
SLIDE 11

Forms, CGI

HTML forms

◮ In most internet programming, you need the user to enter data ◮ HTML forms offer the basic user interface elements inside HTML

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 2 / 19

slide-12
SLIDE 12

Forms, CGI

HTML forms

◮ In most internet programming, you need the user to enter data ◮ HTML forms offer the basic user interface elements inside HTML ◮ Forms have a method which corresponds to the HTTP command that will

be sent when the form is submitted

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 2 / 19

slide-13
SLIDE 13

Forms, CGI

HTML forms

◮ In most internet programming, you need the user to enter data ◮ HTML forms offer the basic user interface elements inside HTML ◮ Forms have a method which corresponds to the HTTP command that will

be sent when the form is submitted

◮ Forms have an action which denotes the URL loaded when the form is

sent.

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 2 / 19

slide-14
SLIDE 14

Forms, CGI

HTML forms

◮ In most internet programming, you need the user to enter data ◮ HTML forms offer the basic user interface elements inside HTML ◮ Forms have a method which corresponds to the HTTP command that will

be sent when the form is submitted

◮ Forms have an action which denotes the URL loaded when the form is

sent.

◮ The action URL is typically a CGI or a servlet DD1335 (Lecture 5) Basic Internet Programming Spring 2010 2 / 19

slide-15
SLIDE 15

Forms, CGI

HTML forms

◮ In most internet programming, you need the user to enter data ◮ HTML forms offer the basic user interface elements inside HTML ◮ Forms have a method which corresponds to the HTTP command that will

be sent when the form is submitted

◮ Forms have an action which denotes the URL loaded when the form is

sent.

◮ The action URL is typically a CGI or a servlet

◮ Inside the form you can have normal HTML and inputs (user interface

elements)

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 2 / 19

slide-16
SLIDE 16

Forms, CGI

Form example

<html> <body> <form action="http://localhost/response.html" method="get"> your name: <input name="someText" type="text" value="change me!" /> <br /> your password: <input name="somePass" type="password" /> <br /> <input name="theButton" type="submit" value="click me" /> <br /> </form> </body> </html>

We submit the form to the SimpleHttpServer that we wrote last time (an improved version to also accommodate POST)

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 3 / 19

slide-17
SLIDE 17

Forms, CGI

Form example . . .

your name: change me! your password: click me!

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 4 / 19

slide-18
SLIDE 18

Forms, CGI

Form submission

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 5 / 19

slide-19
SLIDE 19

Forms, CGI

Form submission

◮ Upon submission, the form will generate the following HTTP:

GET/response.html?someText=change+me%21&somePass= sddsfs&theButton=click+me%21 HTTP/1.1 Host: localhost Connection: Keep-Alive . . . and other headers

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 5 / 19

slide-20
SLIDE 20

Forms, CGI

Form submission

◮ Upon submission, the form will generate the following HTTP:

GET/response.html?someText=change+me%21&somePass= sddsfs&theButton=click+me%21 HTTP/1.1 Host: localhost Connection: Keep-Alive . . . and other headers

◮ The data of the form is thus sent in the HTTP command, after form’s

action and ?

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 5 / 19

slide-21
SLIDE 21

Forms, CGI

Form submission

◮ Upon submission, the form will generate the following HTTP:

GET/response.html?someText=change+me%21&somePass= sddsfs&theButton=click+me%21 HTTP/1.1 Host: localhost Connection: Keep-Alive . . . and other headers

◮ The data of the form is thus sent in the HTTP command, after form’s

action and ?

◮ The format of the data (inputName=value&...) is called a query string

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 5 / 19

slide-22
SLIDE 22

Forms, CGI

Form submission

◮ Upon submission, the form will generate the following HTTP:

GET/response.html?someText=change+me%21&somePass= sddsfs&theButton=click+me%21 HTTP/1.1 Host: localhost Connection: Keep-Alive . . . and other headers

◮ The data of the form is thus sent in the HTTP command, after form’s

action and ?

◮ The format of the data (inputName=value&...) is called a query string ◮ In the GET method the query string is limited to 65535 chars

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 5 / 19

slide-23
SLIDE 23

Forms, CGI

Form submission

◮ Upon submission, the form will generate the following HTTP:

GET/response.html?someText=change+me%21&somePass= sddsfs&theButton=click+me%21 HTTP/1.1 Host: localhost Connection: Keep-Alive . . . and other headers

◮ The data of the form is thus sent in the HTTP command, after form’s

action and ?

◮ The format of the data (inputName=value&...) is called a query string ◮ In the GET method the query string is limited to 65535 chars ◮ The GET query string is visible in the browser. Beware of passwords!

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 5 / 19

slide-24
SLIDE 24

Forms, CGI

POST form

Simply indicate the method POST

<html> <body> <form action="http://localhost/response.html" method="post"> your name: <input name="someText" type="text" value="change me!" /> <br /> your password: <input name="somePass" type="password" /> <br /> <input name="theButton" type="submit" value="click me!" /> <br /> </form> </body> </html>

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 6 / 19

slide-25
SLIDE 25

Forms, CGI

POST form submission

POST /response.html HTTP/1.1 Content-Type: application/x-www-form-urlencoded Content-Length: 59 ... Host: localhost someText=change+me%21&somePass=sdfdsf&theButton=click+me%21

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 7 / 19

slide-26
SLIDE 26

Forms, CGI

POST form submission

POST /response.html HTTP/1.1 Content-Type: application/x-www-form-urlencoded Content-Length: 59 ... Host: localhost someText=change+me%21&somePass=sdfdsf&theButton=click+me%21

◮ When sending data with the POST method, the query string is sent after the

HTTP empty line marking the end of the HTTP header. – So the query string is HTTP content

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 7 / 19

slide-27
SLIDE 27

Forms, CGI

POST form submission

POST /response.html HTTP/1.1 Content-Type: application/x-www-form-urlencoded Content-Length: 59 ... Host: localhost someText=change+me%21&somePass=sdfdsf&theButton=click+me%21

◮ When sending data with the POST method, the query string is sent after the

HTTP empty line marking the end of the HTTP header. – So the query string is HTTP content

◮ By doing that, the POST method lets you send content with any length (e.g.

upload large files)

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 7 / 19

slide-28
SLIDE 28

Forms, CGI

POST form submission

POST /response.html HTTP/1.1 Content-Type: application/x-www-form-urlencoded Content-Length: 59 ... Host: localhost someText=change+me%21&somePass=sdfdsf&theButton=click+me%21

◮ When sending data with the POST method, the query string is sent after the

HTTP empty line marking the end of the HTTP header. – So the query string is HTTP content

◮ By doing that, the POST method lets you send content with any length (e.g.

upload large files)

◮ The POST query string is not visible in the browser!

– You can have both GET-style and POST query strings by <form action="someScript?p1=v1&p2=v2" method="post">

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 7 / 19

slide-29
SLIDE 29

Forms, CGI

Form <input>

◮ For all HTML inputs you can indicate CSS styles, etc

– http://www.htmlhelp.com/reference/html40/forms/input.html

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 8 / 19

slide-30
SLIDE 30

Forms, CGI

Form <input>

◮ For all HTML inputs you can indicate CSS styles, etc

– http://www.htmlhelp.com/reference/html40/forms/input.html

◮ type="text" and type="password" was demonstrated

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 8 / 19

slide-31
SLIDE 31

Forms, CGI

Form <input>

◮ For all HTML inputs you can indicate CSS styles, etc

– http://www.htmlhelp.com/reference/html40/forms/input.html

◮ type="text" and type="password" was demonstrated ◮ type="submit" creates a submit button.

If you don’t set any value, it will be "submit query"

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 8 / 19

slide-32
SLIDE 32

Forms, CGI

Form <input>

◮ For all HTML inputs you can indicate CSS styles, etc

– http://www.htmlhelp.com/reference/html40/forms/input.html

◮ type="text" and type="password" was demonstrated ◮ type="submit" creates a submit button.

If you don’t set any value, it will be "submit query"

◮ Most inputs have a name= (not necessarily needed for type=submit)

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 8 / 19

slide-33
SLIDE 33

Forms, CGI

Form <input>

◮ For all HTML inputs you can indicate CSS styles, etc

– http://www.htmlhelp.com/reference/html40/forms/input.html

◮ type="text" and type="password" was demonstrated ◮ type="submit" creates a submit button.

If you don’t set any value, it will be "submit query"

◮ Most inputs have a name= (not necessarily needed for type=submit) ◮ Most inputs have a type= that determines the user interface element type

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 8 / 19

slide-34
SLIDE 34

Forms, CGI

Form <input>

◮ For all HTML inputs you can indicate CSS styles, etc

– http://www.htmlhelp.com/reference/html40/forms/input.html

◮ type="text" and type="password" was demonstrated ◮ type="submit" creates a submit button.

If you don’t set any value, it will be "submit query"

◮ Most inputs have a name= (not necessarily needed for type=submit) ◮ Most inputs have a type= that determines the user interface element type ◮ Most inputs have a value= to indicate initial value

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 8 / 19

slide-35
SLIDE 35

Forms, CGI

Form <input>

◮ For all HTML inputs you can indicate CSS styles, etc

– http://www.htmlhelp.com/reference/html40/forms/input.html

◮ type="text" and type="password" was demonstrated ◮ type="submit" creates a submit button.

If you don’t set any value, it will be "submit query"

◮ Most inputs have a name= (not necessarily needed for type=submit) ◮ Most inputs have a type= that determines the user interface element type ◮ Most inputs have a value= to indicate initial value ◮ type="reset" creates a button that brings all inputs to their initial values

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 8 / 19

slide-36
SLIDE 36

Forms, CGI

Form <textarea> and <select>

Dedicated input elements:

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 9 / 19

slide-37
SLIDE 37

Forms, CGI

Form <textarea> and <select>

Dedicated input elements:

◮ <textarea name="aText">

initial text multiline </textarea> http://www.htmlhelp.com/reference/html40/forms/textarea.html

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 9 / 19

slide-38
SLIDE 38

Forms, CGI

Form <textarea> and <select>

Dedicated input elements:

◮ <textarea name="aText">

initial text multiline </textarea> http://www.htmlhelp.com/reference/html40/forms/textarea.html

◮ <select name="aChoice" >

<option value="1">option title</option> <option value="two">second</option> </select> http://www.htmlhelp.com/reference/html40/forms/select.html

To indicate an initial value, options can be declared <option selected ...> If the select is declared <select multiple ...>, multiple options can be sent

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 9 / 19

slide-39
SLIDE 39

Forms, CGI

Form <textarea> and <select>

Dedicated input elements:

◮ <textarea name="aText">

initial text multiline </textarea> http://www.htmlhelp.com/reference/html40/forms/textarea.html

◮ <select name="aChoice" >

<option value="1">option title</option> <option value="two">second</option> </select> http://www.htmlhelp.com/reference/html40/forms/select.html

To indicate an initial value, options can be declared <option selected ...> If the select is declared <select multiple ...>, multiple options can be sent

◮ The query string looks like aChoice=1&aChoice=two etc, i.e. the name repeats

for each value

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 9 / 19

slide-40
SLIDE 40

Forms, CGI

Checkboxes and radio buttons

◮ <input type="checkbox" name="x" value="y" />

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 10 / 19

slide-41
SLIDE 41

Forms, CGI

Checkboxes and radio buttons

◮ <input type="checkbox" name="x" value="y" />

◮ Typically you will have more checkboxes with the same name DD1335 (Lecture 5) Basic Internet Programming Spring 2010 10 / 19

slide-42
SLIDE 42

Forms, CGI

Checkboxes and radio buttons

◮ <input type="checkbox" name="x" value="y" />

◮ Typically you will have more checkboxes with the same name ◮ All of the checked boxes will be sent in the query string, with the same name

and the respective values, as for <select multiple >

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 10 / 19

slide-43
SLIDE 43

Forms, CGI

Checkboxes and radio buttons

◮ <input type="checkbox" name="x" value="y" />

◮ Typically you will have more checkboxes with the same name ◮ All of the checked boxes will be sent in the query string, with the same name

and the respective values, as for <select multiple >

◮ <input type="radio" name="x" value="y"/>

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 10 / 19

slide-44
SLIDE 44

Forms, CGI

Checkboxes and radio buttons

◮ <input type="checkbox" name="x" value="y" />

◮ Typically you will have more checkboxes with the same name ◮ All of the checked boxes will be sent in the query string, with the same name

and the respective values, as for <select multiple >

◮ <input type="radio" name="x" value="y"/>

◮ Typically you will have more radio buttons with the same name DD1335 (Lecture 5) Basic Internet Programming Spring 2010 10 / 19

slide-45
SLIDE 45

Forms, CGI

Checkboxes and radio buttons

◮ <input type="checkbox" name="x" value="y" />

◮ Typically you will have more checkboxes with the same name ◮ All of the checked boxes will be sent in the query string, with the same name

and the respective values, as for <select multiple >

◮ <input type="radio" name="x" value="y"/>

◮ Typically you will have more radio buttons with the same name ◮ Normally only one radio button can be checked DD1335 (Lecture 5) Basic Internet Programming Spring 2010 10 / 19

slide-46
SLIDE 46

Forms, CGI

Common Gateway Interface (CGI)

◮ CGI is a standard that allows us to write programs that respond to forms

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 11 / 19

slide-47
SLIDE 47

Forms, CGI

Common Gateway Interface (CGI)

◮ CGI is a standard that allows us to write programs that respond to forms ◮ A standard HTTP server responds to every request

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 11 / 19

slide-48
SLIDE 48

Forms, CGI

Common Gateway Interface (CGI)

◮ CGI is a standard that allows us to write programs that respond to forms ◮ A standard HTTP server responds to every request ◮ For some requests (typically starting with /cgi-bin/ ) the server will start a program

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 11 / 19

slide-49
SLIDE 49

Forms, CGI

Common Gateway Interface (CGI)

◮ CGI is a standard that allows us to write programs that respond to forms ◮ A standard HTTP server responds to every request ◮ For some requests (typically starting with /cgi-bin/ ) the server will start a program ◮ CGI is the interface between the HTTP server and our program

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 11 / 19

slide-50
SLIDE 50

Forms, CGI

Common Gateway Interface (CGI)

◮ CGI is a standard that allows us to write programs that respond to forms ◮ A standard HTTP server responds to every request ◮ For some requests (typically starting with /cgi-bin/ ) the server will start a program ◮ CGI is the interface between the HTTP server and our program ◮ CGI lets us to find out what has been in the HTTP request that the server got

http://hoohoo.ncsa.uiuc.edu/cgi/overview.html http://www.cgi-resources.com

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 11 / 19

slide-51
SLIDE 51

Forms, CGI

The input/output paradigm

◮ Normally in DOS or Unix a program reads an input stream (so-called

standard input) and writes to an output stream (so-called standard output)

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 12 / 19

slide-52
SLIDE 52

Forms, CGI

The input/output paradigm

◮ Normally in DOS or Unix a program reads an input stream (so-called

standard input) and writes to an output stream (so-called standard output)

◮ A DOS or Unix program also reads its command line arguments, and its

environment variables

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 12 / 19

slide-53
SLIDE 53

Forms, CGI

The input/output paradigm

◮ Normally in DOS or Unix a program reads an input stream (so-called

standard input) and writes to an output stream (so-called standard output)

◮ A DOS or Unix program also reads its command line arguments, and its

environment variables

◮ In DOS you can set an env variable like set varName=value DD1335 (Lecture 5) Basic Internet Programming Spring 2010 12 / 19

slide-54
SLIDE 54

Forms, CGI

The input/output paradigm

◮ Normally in DOS or Unix a program reads an input stream (so-called

standard input) and writes to an output stream (so-called standard output)

◮ A DOS or Unix program also reads its command line arguments, and its

environment variables

◮ In DOS you can set an env variable like set varName=value ◮ In Unix, it depends on your shell (command line interpreter), DD1335 (Lecture 5) Basic Internet Programming Spring 2010 12 / 19

slide-55
SLIDE 55

Forms, CGI

The input/output paradigm

◮ Normally in DOS or Unix a program reads an input stream (so-called

standard input) and writes to an output stream (so-called standard output)

◮ A DOS or Unix program also reads its command line arguments, and its

environment variables

◮ In DOS you can set an env variable like set varName=value ◮ In Unix, it depends on your shell (command line interpreter), ◮ In bash export varName=value DD1335 (Lecture 5) Basic Internet Programming Spring 2010 12 / 19

slide-56
SLIDE 56

Forms, CGI

The input/output paradigm

◮ Normally in DOS or Unix a program reads an input stream (so-called

standard input) and writes to an output stream (so-called standard output)

◮ A DOS or Unix program also reads its command line arguments, and its

environment variables

◮ In DOS you can set an env variable like set varName=value ◮ In Unix, it depends on your shell (command line interpreter), ◮ In bash export varName=value ◮ In csh setenv varName value DD1335 (Lecture 5) Basic Internet Programming Spring 2010 12 / 19

slide-57
SLIDE 57

Forms, CGI

The input/output paradigm

◮ Normally in DOS or Unix a program reads an input stream (so-called

standard input) and writes to an output stream (so-called standard output)

◮ A DOS or Unix program also reads its command line arguments, and its

environment variables

◮ In DOS you can set an env variable like set varName=value ◮ In Unix, it depends on your shell (command line interpreter), ◮ In bash export varName=value ◮ In csh setenv varName value ◮ For example the PATH environment variable tells the system where to find

programs

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 12 / 19

slide-58
SLIDE 58

Forms, CGI

The input/output paradigm

◮ Normally in DOS or Unix a program reads an input stream (so-called

standard input) and writes to an output stream (so-called standard output)

◮ A DOS or Unix program also reads its command line arguments, and its

environment variables

◮ In DOS you can set an env variable like set varName=value ◮ In Unix, it depends on your shell (command line interpreter), ◮ In bash export varName=value ◮ In csh setenv varName value ◮ For example the PATH environment variable tells the system where to find

programs

◮ So for input there are: standard input, command line arguments and

environment variables

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 12 / 19

slide-59
SLIDE 59

Forms, CGI

The input/output paradigm

◮ Normally in DOS or Unix a program reads an input stream (so-called

standard input) and writes to an output stream (so-called standard output)

◮ A DOS or Unix program also reads its command line arguments, and its

environment variables

◮ In DOS you can set an env variable like set varName=value ◮ In Unix, it depends on your shell (command line interpreter), ◮ In bash export varName=value ◮ In csh setenv varName value ◮ For example the PATH environment variable tells the system where to find

programs

◮ So for input there are: standard input, command line arguments and

environment variables

◮ The standard output is the only place for program output

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 12 / 19

slide-60
SLIDE 60

Forms, CGI

CGI program input/output

◮ Input: a number of environment variables set by the WWW server

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 13 / 19

slide-61
SLIDE 61

Forms, CGI

CGI program input/output

◮ Input: a number of environment variables set by the WWW server ◮ One of the variables (the QUERY_STRING) contains arguments in the

form arg1=value1&arg2=value2&...

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 13 / 19

slide-62
SLIDE 62

Forms, CGI

CGI program input/output

◮ Input: a number of environment variables set by the WWW server ◮ One of the variables (the QUERY_STRING) contains arguments in the

form arg1=value1&arg2=value2&...

◮ In the GET method the query string is read from the URL, after the ’?’ sign

http://yourServer:port/cgi-bin/scriptName?arg1=value1&arg2=value2

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 13 / 19

slide-63
SLIDE 63

Forms, CGI

CGI program input/output

◮ Input: a number of environment variables set by the WWW server ◮ One of the variables (the QUERY_STRING) contains arguments in the

form arg1=value1&arg2=value2&...

◮ In the GET method the query string is read from the URL, after the ’?’ sign

http://yourServer:port/cgi-bin/scriptName?arg1=value1&arg2=value2

◮ In the POST method the standard input gives the query string DD1335 (Lecture 5) Basic Internet Programming Spring 2010 13 / 19

slide-64
SLIDE 64

Forms, CGI

CGI program input/output

◮ Input: a number of environment variables set by the WWW server ◮ One of the variables (the QUERY_STRING) contains arguments in the

form arg1=value1&arg2=value2&...

◮ In the GET method the query string is read from the URL, after the ’?’ sign

http://yourServer:port/cgi-bin/scriptName?arg1=value1&arg2=value2

◮ In the POST method the standard input gives the query string

◮ Output: the standard output of the CGI program will be sent back to the

browser!

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 13 / 19

slide-65
SLIDE 65

Forms, CGI

CGI program input/output

◮ Input: a number of environment variables set by the WWW server ◮ One of the variables (the QUERY_STRING) contains arguments in the

form arg1=value1&arg2=value2&...

◮ In the GET method the query string is read from the URL, after the ’?’ sign

http://yourServer:port/cgi-bin/scriptName?arg1=value1&arg2=value2

◮ In the POST method the standard input gives the query string

◮ Output: the standard output of the CGI program will be sent back to the

browser!

◮ Both the HTTP headers and content DD1335 (Lecture 5) Basic Internet Programming Spring 2010 13 / 19

slide-66
SLIDE 66

Forms, CGI

CGI program input/output

◮ Input: a number of environment variables set by the WWW server ◮ One of the variables (the QUERY_STRING) contains arguments in the

form arg1=value1&arg2=value2&...

◮ In the GET method the query string is read from the URL, after the ’?’ sign

http://yourServer:port/cgi-bin/scriptName?arg1=value1&arg2=value2

◮ In the POST method the standard input gives the query string

◮ Output: the standard output of the CGI program will be sent back to the

browser!

◮ Both the HTTP headers and content ◮ Headers, empty line, content DD1335 (Lecture 5) Basic Internet Programming Spring 2010 13 / 19

slide-67
SLIDE 67

Forms, CGI

CGI program input/output

◮ Input: a number of environment variables set by the WWW server ◮ One of the variables (the QUERY_STRING) contains arguments in the

form arg1=value1&arg2=value2&...

◮ In the GET method the query string is read from the URL, after the ’?’ sign

http://yourServer:port/cgi-bin/scriptName?arg1=value1&arg2=value2

◮ In the POST method the standard input gives the query string

◮ Output: the standard output of the CGI program will be sent back to the

browser!

◮ Both the HTTP headers and content ◮ Headers, empty line, content ◮ Content is typically HTML but not necessarily DD1335 (Lecture 5) Basic Internet Programming Spring 2010 13 / 19

slide-68
SLIDE 68

Forms, CGI

CGI environment variables

◮ SERVER_SOFTWARE: type of the server

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 14 / 19

slide-69
SLIDE 69

Forms, CGI

CGI environment variables

◮ SERVER_SOFTWARE: type of the server ◮ SERVER_NAME: e.g. www.nada.kth.se

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 14 / 19

slide-70
SLIDE 70

Forms, CGI

CGI environment variables

◮ SERVER_SOFTWARE: type of the server ◮ SERVER_NAME: e.g. www.nada.kth.se ◮ SERVER_PORT: e.g. 80

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 14 / 19

slide-71
SLIDE 71

Forms, CGI

CGI environment variables

◮ SERVER_SOFTWARE: type of the server ◮ SERVER_NAME: e.g. www.nada.kth.se ◮ SERVER_PORT: e.g. 80 ◮ REQUEST_METHOD: GET or POST

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 14 / 19

slide-72
SLIDE 72

Forms, CGI

CGI environment variables

◮ SERVER_SOFTWARE: type of the server ◮ SERVER_NAME: e.g. www.nada.kth.se ◮ SERVER_PORT: e.g. 80 ◮ REQUEST_METHOD: GET or POST ◮ PATH_INFO: path to your program in the URL, like /cgi-bin/prog

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 14 / 19

slide-73
SLIDE 73

Forms, CGI

CGI environment variables

◮ SERVER_SOFTWARE: type of the server ◮ SERVER_NAME: e.g. www.nada.kth.se ◮ SERVER_PORT: e.g. 80 ◮ REQUEST_METHOD: GET or POST ◮ PATH_INFO: path to your program in the URL, like /cgi-bin/prog ◮ PATH_TRANSLATED: path of the program on disk

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 14 / 19

slide-74
SLIDE 74

Forms, CGI

CGI environment variables

◮ SERVER_SOFTWARE: type of the server ◮ SERVER_NAME: e.g. www.nada.kth.se ◮ SERVER_PORT: e.g. 80 ◮ REQUEST_METHOD: GET or POST ◮ PATH_INFO: path to your program in the URL, like /cgi-bin/prog ◮ PATH_TRANSLATED: path of the program on disk ◮ SCRIPT_NAME: name of the CGI program

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 14 / 19

slide-75
SLIDE 75

Forms, CGI

CGI environment variables

◮ SERVER_SOFTWARE: type of the server ◮ SERVER_NAME: e.g. www.nada.kth.se ◮ SERVER_PORT: e.g. 80 ◮ REQUEST_METHOD: GET or POST ◮ PATH_INFO: path to your program in the URL, like /cgi-bin/prog ◮ PATH_TRANSLATED: path of the program on disk ◮ SCRIPT_NAME: name of the CGI program ◮ QUERY_STRING: actual path of the program

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 14 / 19

slide-76
SLIDE 76

Forms, CGI

CGI environment variables

◮ SERVER_SOFTWARE: type of the server ◮ SERVER_NAME: e.g. www.nada.kth.se ◮ SERVER_PORT: e.g. 80 ◮ REQUEST_METHOD: GET or POST ◮ PATH_INFO: path to your program in the URL, like /cgi-bin/prog ◮ PATH_TRANSLATED: path of the program on disk ◮ SCRIPT_NAME: name of the CGI program ◮ QUERY_STRING: actual path of the program ◮ REMOTE_HOST: host where the request comes from

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 14 / 19

slide-77
SLIDE 77

Forms, CGI

CGI environment variables

◮ SERVER_SOFTWARE: type of the server ◮ SERVER_NAME: e.g. www.nada.kth.se ◮ SERVER_PORT: e.g. 80 ◮ REQUEST_METHOD: GET or POST ◮ PATH_INFO: path to your program in the URL, like /cgi-bin/prog ◮ PATH_TRANSLATED: path of the program on disk ◮ SCRIPT_NAME: name of the CGI program ◮ QUERY_STRING: actual path of the program ◮ REMOTE_HOST: host where the request comes from ◮ AUTH_TYPE: authentication if the user logged-in (e.g. BASIC)

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 14 / 19

slide-78
SLIDE 78

Forms, CGI

CGI environment variables

◮ SERVER_SOFTWARE: type of the server ◮ SERVER_NAME: e.g. www.nada.kth.se ◮ SERVER_PORT: e.g. 80 ◮ REQUEST_METHOD: GET or POST ◮ PATH_INFO: path to your program in the URL, like /cgi-bin/prog ◮ PATH_TRANSLATED: path of the program on disk ◮ SCRIPT_NAME: name of the CGI program ◮ QUERY_STRING: actual path of the program ◮ REMOTE_HOST: host where the request comes from ◮ AUTH_TYPE: authentication if the user logged-in (e.g. BASIC) ◮ REMOTE_USER: username if the user logged-in

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 14 / 19

slide-79
SLIDE 79

Forms, CGI

CGI environment variables

◮ SERVER_SOFTWARE: type of the server ◮ SERVER_NAME: e.g. www.nada.kth.se ◮ SERVER_PORT: e.g. 80 ◮ REQUEST_METHOD: GET or POST ◮ PATH_INFO: path to your program in the URL, like /cgi-bin/prog ◮ PATH_TRANSLATED: path of the program on disk ◮ SCRIPT_NAME: name of the CGI program ◮ QUERY_STRING: actual path of the program ◮ REMOTE_HOST: host where the request comes from ◮ AUTH_TYPE: authentication if the user logged-in (e.g. BASIC) ◮ REMOTE_USER: username if the user logged-in ◮ CONTENT_TYPE: the content-type HTTP header

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 14 / 19

slide-80
SLIDE 80

Forms, CGI

CGI environment variables

◮ SERVER_SOFTWARE: type of the server ◮ SERVER_NAME: e.g. www.nada.kth.se ◮ SERVER_PORT: e.g. 80 ◮ REQUEST_METHOD: GET or POST ◮ PATH_INFO: path to your program in the URL, like /cgi-bin/prog ◮ PATH_TRANSLATED: path of the program on disk ◮ SCRIPT_NAME: name of the CGI program ◮ QUERY_STRING: actual path of the program ◮ REMOTE_HOST: host where the request comes from ◮ AUTH_TYPE: authentication if the user logged-in (e.g. BASIC) ◮ REMOTE_USER: username if the user logged-in ◮ CONTENT_TYPE: the content-type HTTP header ◮ CONTENT_LENGTH: the content-length HTTP header (useful in POST)

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 14 / 19

slide-81
SLIDE 81

Forms, CGI

CGI at NADA

◮ Put your CGI program in your CGI dir at NADA (if it’s activated)

/afs/nada.kth.se/public/www.student/cgibin/yourUserName/yourProgram

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 15 / 19

slide-82
SLIDE 82

Forms, CGI

CGI at NADA

◮ Put your CGI program in your CGI dir at NADA (if it’s activated)

/afs/nada.kth.se/public/www.student/cgibin/yourUserName/yourProgram

◮ Make sure that the file has execution rights

chmod uo+x yourProgram cd /afs/nada.kth.se/public/www.student/cgi-bin/yourUserName/ fs setacl . system:anyuser rl

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 15 / 19

slide-83
SLIDE 83

Forms, CGI

CGI at NADA

◮ Put your CGI program in your CGI dir at NADA (if it’s activated)

/afs/nada.kth.se/public/www.student/cgibin/yourUserName/yourProgram

◮ Make sure that the file has execution rights

chmod uo+x yourProgram cd /afs/nada.kth.se/public/www.student/cgi-bin/yourUserName/ fs setacl . system:anyuser rl

◮ You should first test your program without a browser

Set the CGI variables by hand using setenv (csh) or export (bash) setenv QUERY_STRING a=b&c=d call yourProgram

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 15 / 19

slide-84
SLIDE 84

Forms, CGI

CGI at NADA

◮ Put your CGI program in your CGI dir at NADA (if it’s activated)

/afs/nada.kth.se/public/www.student/cgibin/yourUserName/yourProgram

◮ Make sure that the file has execution rights

chmod uo+x yourProgram cd /afs/nada.kth.se/public/www.student/cgi-bin/yourUserName/ fs setacl . system:anyuser rl

◮ You should first test your program without a browser

Set the CGI variables by hand using setenv (csh) or export (bash) setenv QUERY_STRING a=b&c=d call yourProgram

◮ When it works, test it with a browser

http://cgi.student.nada.kth.se/cgibin/yourUserName/yourProgram

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 15 / 19

slide-85
SLIDE 85

Forms, CGI

CGI at NADA

◮ Put your CGI program in your CGI dir at NADA (if it’s activated)

/afs/nada.kth.se/public/www.student/cgibin/yourUserName/yourProgram

◮ Make sure that the file has execution rights

chmod uo+x yourProgram cd /afs/nada.kth.se/public/www.student/cgi-bin/yourUserName/ fs setacl . system:anyuser rl

◮ You should first test your program without a browser

Set the CGI variables by hand using setenv (csh) or export (bash) setenv QUERY_STRING a=b&c=d call yourProgram

◮ When it works, test it with a browser

http://cgi.student.nada.kth.se/cgibin/yourUserName/yourProgram

◮ You can check the server error log and try to find your error between other

people’s errors

http://cgi.student.nada.kth.se/cgi-bin/get-errlog

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 15 / 19

slide-86
SLIDE 86

Forms, CGI

A CGI example in PERL

◮ PERL = the Practical Extraction and Report Language

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 16 / 19

slide-87
SLIDE 87

Forms, CGI

A CGI example in PERL

◮ PERL = the Practical Extraction and Report Language

◮ www.perl.com ◮ http://broadcast.oreilly.com/2008/09/

a-beginners-introduction-to-pe.html

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 16 / 19

slide-88
SLIDE 88

Forms, CGI

A CGI example in PERL

◮ PERL = the Practical Extraction and Report Language

◮ www.perl.com ◮ http://broadcast.oreilly.com/2008/09/

a-beginners-introduction-to-pe.html

◮ An interpreted programming language inspired by C and shellscript (bash, csh)

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 16 / 19

slide-89
SLIDE 89

Forms, CGI

A CGI example in PERL

◮ PERL = the Practical Extraction and Report Language

◮ www.perl.com ◮ http://broadcast.oreilly.com/2008/09/

a-beginners-introduction-to-pe.html

◮ An interpreted programming language inspired by C and shellscript (bash, csh) ◮ Available on many platforms but inspired by and started on Unix

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 16 / 19

slide-90
SLIDE 90

Forms, CGI

A CGI example in PERL

◮ PERL = the Practical Extraction and Report Language

◮ www.perl.com ◮ http://broadcast.oreilly.com/2008/09/

a-beginners-introduction-to-pe.html

◮ An interpreted programming language inspired by C and shellscript (bash, csh) ◮ Available on many platforms but inspired by and started on Unix ◮ Very strong pattern matching

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 16 / 19

slide-91
SLIDE 91

Forms, CGI

A CGI example in PERL

◮ PERL = the Practical Extraction and Report Language

◮ www.perl.com ◮ http://broadcast.oreilly.com/2008/09/

a-beginners-introduction-to-pe.html

◮ An interpreted programming language inspired by C and shellscript (bash, csh) ◮ Available on many platforms but inspired by and started on Unix ◮ Very strong pattern matching ◮ Easy to use e.g. to make a simple CGI

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 16 / 19

slide-92
SLIDE 92

Forms, CGI

A CGI example in PERL

◮ PERL = the Practical Extraction and Report Language

◮ www.perl.com ◮ http://broadcast.oreilly.com/2008/09/

a-beginners-introduction-to-pe.html

◮ An interpreted programming language inspired by C and shellscript (bash, csh) ◮ Available on many platforms but inspired by and started on Unix ◮ Very strong pattern matching ◮ Easy to use e.g. to make a simple CGI ◮ But not for larger applications

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 16 / 19

slide-93
SLIDE 93

Forms, CGI

A CGI example in PERL

◮ PERL = the Practical Extraction and Report Language

◮ www.perl.com ◮ http://broadcast.oreilly.com/2008/09/

a-beginners-introduction-to-pe.html

◮ An interpreted programming language inspired by C and shellscript (bash, csh) ◮ Available on many platforms but inspired by and started on Unix ◮ Very strong pattern matching ◮ Easy to use e.g. to make a simple CGI ◮ But not for larger applications ◮ We just illustrate the CGI principle with PERL

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 16 / 19

slide-94
SLIDE 94

Forms, CGI

A CGI example in PERL

◮ PERL = the Practical Extraction and Report Language

◮ www.perl.com ◮ http://broadcast.oreilly.com/2008/09/

a-beginners-introduction-to-pe.html

◮ An interpreted programming language inspired by C and shellscript (bash, csh) ◮ Available on many platforms but inspired by and started on Unix ◮ Very strong pattern matching ◮ Easy to use e.g. to make a simple CGI ◮ But not for larger applications ◮ We just illustrate the CGI principle with PERL ◮ Java is not a good language to write CGI in, because CGI makes one

process/HTTP access and a Java Virtual Machine has a large footprint (30 Meg)

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 16 / 19

slide-95
SLIDE 95

Forms, CGI

A CGI example in PERL

◮ PERL = the Practical Extraction and Report Language

◮ www.perl.com ◮ http://broadcast.oreilly.com/2008/09/

a-beginners-introduction-to-pe.html

◮ An interpreted programming language inspired by C and shellscript (bash, csh) ◮ Available on many platforms but inspired by and started on Unix ◮ Very strong pattern matching ◮ Easy to use e.g. to make a simple CGI ◮ But not for larger applications ◮ We just illustrate the CGI principle with PERL ◮ Java is not a good language to write CGI in, because CGI makes one

process/HTTP access and a Java Virtual Machine has a large footprint (30 Meg)

◮ Servlets are the solution in Java

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 16 / 19

slide-96
SLIDE 96

Forms, CGI

A form to respond to

<FORM ACTION="/cgi-bin/test.pl" METHOD="GET"> Write a message: <INPUT TYPE="text" NAME="message" SIZE=20 MAXLENGTH=40 VALUE=""> <INPUT TYPE = "submit" VALUE= "Send it!"> <INPUT TYPE= "reset" VALUE= "Remove it!" > </FORM>

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 17 / 19

slide-97
SLIDE 97

Forms, CGI

Responding to a form in a PERL CGI

#!/usr/local/bin/perl print "Content-type: text/html\\n\\n"; ## CGIs must print HTTP headers AND empty line! $REQUEST_METHOD = $ENV\{’REQUEST_METHOD’\}; $QUERY_STRING = $ENV\{’QUERY_STRING’\}; ## Reading environment variables if($REQUEST_METHOD ne "GET") \{ print"Sorry, i can only do <code>GET</code><br />Bye!"; exit(0); \} ($COMMAND, $MESSAGE) = split(/=/, $QUERY_STRING); ## Split the query string via PERL pattern matching. if($COMMAND eq "message") \{ print "<h1>You sent:</h1>"; print "Message: $MESSAGE"; exit(0); \} exit(0);

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 18 / 19

slide-98
SLIDE 98

Forms, CGI

Dynamic Web content

◮ Content generated by CGI is different from normal HTTP serving

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 19 / 19

slide-99
SLIDE 99

Forms, CGI

Dynamic Web content

◮ Content generated by CGI is different from normal HTTP serving ◮ It’s not a static file or image that’s being served

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 19 / 19

slide-100
SLIDE 100

Forms, CGI

Dynamic Web content

◮ Content generated by CGI is different from normal HTTP serving ◮ It’s not a static file or image that’s being served ◮ Instead, a dynamic content is generated

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 19 / 19

slide-101
SLIDE 101

Forms, CGI

Dynamic Web content

◮ Content generated by CGI is different from normal HTTP serving ◮ It’s not a static file or image that’s being served ◮ Instead, a dynamic content is generated ◮ You can use CGI to generate dynamic content even if you don’t respond

to a form

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 19 / 19

slide-102
SLIDE 102

Forms, CGI

Dynamic Web content

◮ Content generated by CGI is different from normal HTTP serving ◮ It’s not a static file or image that’s being served ◮ Instead, a dynamic content is generated ◮ You can use CGI to generate dynamic content even if you don’t respond

to a form

◮ Or you can use Java servlets for the same purpose

DD1335 (Lecture 5) Basic Internet Programming Spring 2010 19 / 19