Programming The Web Server: Robert M. Dondero, Ph.D. Princeton University
Programming The Web Server: Robert M. Dondero, Ph.D. Princeton University
1
Objectives
2
Previously
CGI Programming
Socket
HTTP
Browser Web Server database somefile
3
Now
Socket
HTTP
Browser
database somefile
4
Part 1: PHP
5
PHP
Good news
Apache web server handles PHP
PHP is available on CS computers
Bad news...
7
PHP as a Pgmming Language
8
PHP Examples
See hello.php
Program structure
The echo command
The build/run process
See square.php
Function definitions
Variables
Parameters
String concatenation
9
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
10
PHP Examples
include y n
include_once y y
require n n
require_once n y
11
PHP Examples
12
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
13
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 14
PHP Examples
See linesort.php
Arrays, sorting, file-handling
Note CPU time consumed
See concord.php
Regular expressions
Associative arrays
Note CPU time consumed
15
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
16
PHP for Web Programming
17
PennypackPhp1 Application
18
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
... ...
19
Toward PennypackPhp2
Problem:
Missing author name is not handled as I wish
Solution:
PHP header() function...
20
PennypackPhp2 Application
21
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
22
Toward PennypackPhp3
"Problem"
Want "previous search" field in searchform page
"Solution"
Sessions via the session implicit object
23
PennypackPhp3 Application
24
PHP $_SESSION Superglobal
25
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
26
Part 2: JSP
27
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
28
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!
29
JSP Examples
30
PennypackJsp1 Application
31
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
JSP Page Directive Tag
33
JSP Scriptlet Tag
"Scriptlet" Tag
<% JavaCodeFragment %>
Contains arbitrary Java code
Uses implicit objects...
34
JSP Implicit Objects
Implicit Objects
request: getParameter(), getParameterNames(),
getParameterValues()
out: print(), println()
35
JSP Expression Tag
"Expression" Tag
<%= JavaExpression %>
Shortcut for:
<% out.print(JavaExpression) %>
36
JSP Include Tag
"Include" Tag
<jsp:include page="{ relativeURL |
<%= JavaExpression %> }" flush="true" />
At run-time, output generated by relativeURL
replaces the tag
37
Toward PennypackJsp2
Problem:
Missing author name is not handled as I wish
Solution:
JSP forwarding...
38
PennypackJsp2 Application
39
JSP Forward Tag
"Forward" Tag
<jsp:forward page={ relativeURL |
<%= expression %> }" />
Forwards a client request to an HTML file or JSP
file for processing
40
Toward PennypackJsp3
"Problem"
Want "previous search" field in searchform page
Must make the application stateful
Solution
Sessions via the session implicit object
41
PennypackJsp3 Application
42
JSP: Another Implicit Object
45
PHP & JSP Parting Thoughts
CGI
Program generates HTML
PHP & JSP
HTML contains program
46
PHP & JSP Parting Thoughts
47
Summary
We have covered:
How to "program the web server" using...
PHP
JSP
48
Appendix 1: PSP
49
PSP Overview
50
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...
PennypackPsp1 Application
52
PSP Parting Thoughts
53