Programming the Web Server

53
1 Programming the Web Server Robert M. Dondero, Ph.D. Princeton University

Transcript of Programming the Web Server

Page 1: Programming the Web Server

1

Programming the Web Server

Robert M. Dondero, Ph.D.Princeton University

Page 2: Programming the Web Server

2

Objectives

You will learn: How to "program the web server" using... PHP JSP

Page 3: Programming the Web Server

3

Browser Web ServerSocketHTTP

PipeCGI protocol

Previously

Application

Web Server

PipeCGI protocol

Browser

SocketHTTP

Application runs in distinctprocess from web server

CGI Programming

somefiledatabase

Page 4: Programming the Web Server

4

Browser

Web ServerSocketHTTP

Now

Application

Browser

SocketHTTP

Application runs (typically) asthread within web server process

Programming the Web Server

somefiledatabase

Page 5: Programming the Web Server

5

Part 1: PHP

Page 6: Programming the Web Server

6

PHP

Who: Rasmus Lerdorf When: 1995 Why: Create Web apps

that generate pagesdynamically

The name: Personal Home Page And later... PHP Hypertext Processor (recursive acronym)

Page 7: Programming the Web Server

7

PHP Good News and Bad News

Good news Apache web server handles PHP PHP is available on CS computers

Bad news...

Page 8: Programming the Web Server

8

PHP as a Pgmming Language

Yet another programming language! And (IMHO) an inelegant one No time (and inappropriate) to cover thoroughly

See my personal "Subset of PHP" Link on web pages

Some familiar examples that might help...

Page 9: Programming the Web Server

9

PHP Examples

See hello.php Program structure The echo command The build/run process

See square.php Function definitions Variables Parameters String concatenation

Page 10: Programming the Web Server

10

PHP Examples

See circle.php Interpreted strings The fscanf() function

As in C The printf() and fprintf() functions

As in C More powerful, less efficient, than echo

command

See euclid.php Control flow

Page 11: Programming the Web Server

11

PHP Examples

See intmath.php, testintmath.php Multi-file programs

Exec Continues File Included Upon Failure? Only Once?

include y ninclude_once y yrequire n nrequire_once n y

Page 12: Programming the Web Server

12

PHP Examples

See fraction.php, testfraction.php Object oriented programming

"$this" object reference "->" field selector operator

Exception handling

Page 13: Programming the Web Server

13

PHP Examples

See calls1.php Call-by-value and call-by-reference

See calls2.php Call-by-value and call-by-reference with objects

See testdivmod.php "Returning" multiple values Call-by-value and call-by-reference

See immutable.php Immutability of strings

Page 14: Programming the Web Server

14

PHP Examples

See arrays.php Arrays Arrays are not objects!!! "foreach" statement

See formatter.php Constants String manipulation; uninterpreted strings File handling Command-line arguments Regular expressions

Page 15: Programming the Web Server

15

PHP Examples

See linesort.php Arrays, sorting, file-handling Note CPU time consumed

See concord.php Regular expressions Associative arrays Note CPU time consumed

Page 16: Programming the Web Server

16

PHP for Database Programming

See selectrows.php Uses the mysqli library (not the mysql library)

See updaterows.php Transactions

See trans.php Transactions for recovery

See selectrowsprepared.php Prepared statements

Page 17: Programming the Web Server

17

PHP for Web Programming

The Pennypack sequence in PHP...

Page 18: Programming the Web Server

18

PennypackPhp1 Application

See PennypackPhp1 application book.php database.php index.html header.php, footer.php searchform.php, searchresults.php

Generalizing...

Page 19: Programming the Web Server

19

PHP Superglobal Variables

Superglobal Contents

$_GET Data passed to the current script via the HTTP GET method

$_POST Data passed to the script via the HTTP POST method

$_COOKIE Data passed to the script via cookies

$_SESSION Session data (see description later in this lecture)

$_REQUEST The union of %_GET, %_POST, and %_COOKIE

$_SERVER Headers, paths, script locations, etc.; varies with web server

... ...

Page 20: Programming the Web Server

20

Toward PennypackPhp2

Problem: Missing author name is not handled as I wish

Solution: PHP header() function...

Page 21: Programming the Web Server

21

PennypackPhp2 Application

See PennypackPhp2 Application book.php database.php index.html header.php, footer.php searchform.php, searchresults.php

Generalizing...

Page 22: Programming the Web Server

22

PHP header() Function

header() function Sends header to browser In this case

Sends "location" header to browser Header specifies web page Browser automatically visits specified web page

Page 23: Programming the Web Server

23

Toward PennypackPhp3

"Problem" Want "previous search" field in searchform page

"Solution" Sessions via the session implicit object

Page 24: Programming the Web Server

24

PennypackPhp3 Application

See PennypackPhp3 Application book.php database.php index.html header.php, footer.php searchform.php, searchresults.php

Generalizing...

Page 25: Programming the Web Server

25

PHP $_SESSION Superglobal

Another superglobal variable... $_SESSION

An associative array() Provides access to saved "name=value" state pairs

Page 26: Programming the Web Server

26

PHP session_start()

session_start() function Commands web server to:

Accept "PHPSESSID=key" cookie from browser, or create "PHPSESSID=key" cookie

Extract key from cookie Use key to populate $_SESSION with saved

"name=value" state pairs Allow PHP code to use/modify $_SESSION Pass "PHPSESSID=key" cookie to browser

Page 27: Programming the Web Server

27

Part 2: JSP

Page 28: Programming the Web Server

28

JSP

Who: Sun When: 1999 Why:

"Address the perception that the Java programming environment didn't provide developers with enough support for the Web" – Wikipedia

Sun's answer to PHP (and Microsoft's ASP)

The name: JavaServer Pages

Page 29: Programming the Web Server

29

Bad News

Bad news Apache web server does not handle JSP Not available on CS computers

Why study JSP? May be useful for projects Conceptual bridge between CGI Web programming

and "PHP-style" Web programming New language isn't necessary!

Page 30: Programming the Web Server

30

JSP Examples

Using the Apache Tomcat web server Running locally on my computer

http://localhost:8080/PennypackJsp1/index.html

Page 31: Programming the Web Server

31

PennypackJsp1 Application

See PennypackJsp1 Application DateTime.java, Book.java, Database.java

Cannot be in the anonymous package index.html searchform.jsp, searchresults.jsp header.jsp, footer.jsp

Generalizing...

Page 32: Programming the Web Server

JSP Hidden Comment

Hidden comment <%-- some text --%> Not sent to Web server Not sent to browser As opposed to...

Output comment (not illustrated) <!-- some text --> Ordinary HTML comment

Page 33: Programming the Web Server

33

JSP Page Directive Tag

"Page Directive" Tag <%@page language="java"

import="package.class, package.class" %> Specifies language

Java ... or JavaScript???

Specifies classes to import

Page 34: Programming the Web Server

34

JSP Scriptlet Tag

"Scriptlet" Tag <% JavaCodeFragment %> Contains arbitrary Java code Uses implicit objects...

Page 35: Programming the Web Server

35

JSP Implicit Objects

Implicit Objects request: getParameter(), getParameterNames(),

getParameterValues() out: print(), println()

Page 36: Programming the Web Server

36

JSP Expression Tag

"Expression" Tag <%= JavaExpression %> Shortcut for: <% out.print(JavaExpression) %>

Page 37: Programming the Web Server

37

JSP Include Tag

"Include" Tag <jsp:include page="{ relativeURL |

<%= JavaExpression %> }" flush="true" /> At run-time, output generated by relativeURL

replaces the tag

Page 38: Programming the Web Server

38

Toward PennypackJsp2

Problem: Missing author name is not handled as I wish

Solution: JSP forwarding...

Page 39: Programming the Web Server

39

PennypackJsp2 Application

See PennypackJsp2 Application DateTime.java, Book.java, Database.java index.html searchform.jsp, searchresults.jsp header.jsp, footer.jsp

Generalizing...

Page 40: Programming the Web Server

40

JSP Forward Tag

"Forward" Tag <jsp:forward page={ relativeURL |

<%= expression %> }" /> Forwards a client request to an HTML file or JSP

file for processing

Page 41: Programming the Web Server

41

Toward PennypackJsp3

"Problem" Want "previous search" field in searchform page Must make the application stateful

Solution Sessions via the session implicit object

Page 42: Programming the Web Server

42

PennypackJsp3 Application

See PennypackJsp3 Application DateTime.java, Book.java, Database.java index.html searchform.jsp, searchresults.jsp header.jsp, footer.jsp

Generalizing...

Page 43: Programming the Web Server

43

JSP: Another Implicit Object

Implicit objects (cont.): session

putValue() session.putValue() populates session implicit

object with "name=value" state pair Web server stores "name=value" state pairs on

server side; associates state with key; passes "JSESSIONID=key" to browser as cookie

getValue() Web server accepts "JSESSIONID=key" cookie

from browser; extracts key from cookie; populates session implicit object with "name=value" state pairs

session.getValue() accesses "name=value" state pair

Page 44: Programming the Web Server

44

JSP App Deployment

tomcat … webapps … PennypackJspX WEB-INF classes pennypack Book.java Book.class Database.java Database.class DateTime.java DateTime.class lib web.xml footer.jsp header.jsp index.html searchform.jsp searchresults.jsp

Using the Apache Tomcat web server:

Page 45: Programming the Web Server

45

PHP & JSP Parting Thoughts

JSP vs. PHP: JSP pro: Less eccentric syntax

JSP = Java + some tags JSP pro: More powerful

Java language, libraries, docs are available PHP pro: Apache web server support PHP pro: Simpler, "easier to learn"

Unless you already know Java!!!

Page 46: Programming the Web Server

46

PHP & JSP Parting Thoughts

PHP & JSP are CGI "turned inside out"

CGI Program generates HTML

PHP & JSP HTML contains program

Page 47: Programming the Web Server

47

PHP & JSP Parting Thoughts

Fixed template data only => Use HTML; JSP/PHP adds no value

Dynamically generated data only => Use CGI; JSP/PHP adds little value

Mixture (as is almost always the case) => PHP/JSP adds value

Page 48: Programming the Web Server

48

Summary

We have covered: How to "program the web server" using... PHP JSP

Page 49: Programming the Web Server

49

Appendix 1: PSP

Page 50: Programming the Web Server

50

PSP Overview

PSP (Python Server Pages) Java:JSP :: Python:PSP

Page 51: Programming the Web Server

PSP Overview

Good news Apache web server handles PSP... Via mod_python plug-in module

Bad news mod_python not installed into CS Apache web

server PSP not available on CS Apache web server

An example using Apache/mod_python running on my local computer...

Page 52: Programming the Web Server

52

PennypackPsp1 Application

See PennypackPsp1 application book.py, database.py index.html common.py searchform.psp, searchresults.psp

Page 53: Programming the Web Server

53

PSP Parting Thoughts

PSP parting thoughts A little awkward

Python indentation significant => Awkward merge into HTML

(IMHO) Not ready for prime time Fragile Poorly documented