Web Programming Materials Class Notes PDF
Web Programming Materials Class Notes PDF
Raphael Finkel
1 Intro
• Class 1: 1/21/2022
• Handout 1 — My names
2 History
• ArpaNet: around 1973
1
CS316 Spring 2022 2
• The client sends requests to the web server using HTTP, and the web
server sends responses back to the client, again using HTTP.
CS316 Spring 2022 3
• The web server does not maintain state between requests; every re-
quest/response pair is self-contained.
4 A deeper look
• The web server can ask the client to store opaque data called cookies
and present them back to the web server on subsequent requests; the
web server can use the contents of the cookies to verify authentica-
tion and look up stored history.
• Browser requests are for pages.
• The web server interprets a typical page request as naming a file to
send back as the response, but if the file has the necessary permis-
sions, the web server treats the file as a program and packages its
output into the response.
• The web server generally doesn’t try to interpret the content of its
responses. It’s up to the client to interpret that content. In par-
ticular, web servers don’t understand Hypertext Markup Language
(HTML), but browsers do, and they parse the HTML content to for-
mat and present pages to the user.
• The user can explicitly specify a request by a Universal Resource Lo-
cator (URL) of this form:
protocol://hostName/file/path?parameters
8 HTML tags
• Full list: https://www.w3schools.com/tags/default.asp
• Example that has all the tags mentioned here: Example 3
• Required regions
• <html>
• <head> (within <html>)
• <title> (within <head>)
• <body> (within <html>)
• Class 3: 1/18/2022
• Regions inside <body>, generally nestable; closing tag is sometimes
optional.
• <h1> .. <h6> headings of various sizes
• <p> paragraph
• <a> anchor; typically a hyperlink
• <span> inline text region
• <div> vertically separate text region
• <pre> pre-formatted text
• <table> table
• <tr> table row (within <table>)
• <td> table entry (within <tr>)
• <ul> unnumbered list
• <ol> ordered (numbered) list
• <li> list element (within ol or ul)
• Non-regions inside <body>; nothing nests inside
• <img> image
• <br> line break
• We will see other tags later.
9 HTML attributes
• Attributes modify the way browsers present tags or provide essen-
tial additional information.
• Attributes are placed inside the brokets delimiting a tag and have
this syntax:
tagname='tag value'
• One may use single or double quotes; if the tag value is a single
word, quotes are optional. Don’t use fancy quotes; use only ASCII
single quote and double quote. Tag values must not contain any
embedded HTML tags.
• Each tag has a defined set of attributes it can accept, and each at-
tribute has a defined syntax for its tag values. (For instance, the
width attribute for <img> has a value like 20, not 20px.)
CS316 Spring 2022 8
10 Styles
• The philosophy is that HTML tags describe content; styles describe
presentation.
• Syntax: property:value; (Spaces are optional.)
• A single element may have multiple style components.
• There are hundreds of style properties; we only discuss a few.
• Text modifications
• Font modifications
• For each property, the browser uses the first rule it finds.
• Step 1: the style attribute in the element’s tag.
• Step 2: an entry in the <style> region of the document
targeting the element’s id (such as #myRed), class (such as
.myRed), or tag (such as p), and in that order.
• Step 3: repeat steps 1 and 2 for each surrounding tag, moving
from closest (local) to farthest (global).
• Any inherited property value that is marked !important
takes precedence over this usual order.
• You can discover the CSS for any element by using the inspector in
the browser’s developer tools.
• Example 4: Nested regions with styles.
• The web server invokes the CGI program after setting several
environment variables. Among them:
13 Input tags
• A full list of input tags for forms is available at www.w3schools.
com/html/html_form_input_types.asp.
• Example 6 uses many of them.
• wget:
• Both have many options, such as providing cookies, using GET in-
stead of POST, and logging.
• Since all accessible CGI programs can be invoked by these clients,
you cannot rely on the browser to constrain the values of fields such
as date. The CGI program must be cautious.
CS316 Spring 2022 13
2 if (!isset($_SERVER["HTTP_HOST"])) {
3 parse_str($argv[1], $_GET);
4 parse_str($argv[1], $_POST);
5 parse_str($argv[1], $_REQUEST);
6 }
CS316 Spring 2022 14
Then you can invoke the program from the command line this
way:
php FILENAME.php 'param1=value1¶m2=value2'
• Debugging
18 JavaScript
• Officially called ECMAScript (European Computer Manufacturers
Association): first released 1997.
• High-level, just-in-time compiled, looks like C or Java, but is in many
ways different from both.
• Advanced features: dynamic typing, prototype-based object orienta-
tion, first-class functions, anonymous functions.
• All standard browsers can run JavaScript programs.
• One can write standalone programs in JavaScript, too; the proces-
sor is called nodejs. See https://nodejs.org/docs/v8.10.0/
api/.
• JavaScript is quite error-prone, although more recent versions are
less so.
• Example 10: JavaScript
• Class 8: 2/3/2022
• Snow day; only a Zoom review session
• Class 9: 2/8/2022
• Differences between nodejs and JavaScript
• JavaScript runs inside the browser in a sandbox: no access to
the file system or to programs outside the browser. It
understands the DOM (Document Object Model; see Section 20
on page 17) and provides variables and methods specific to the
DOM.
• Nodejs runs on its own, with full access to the file system and to
external programs. It does not have the DOM, although one
can bring in a module (jsdom) that does have the DOM.
CS316 Spring 2022 16
The database connector makes sure that the actual values are stored
verbatim and are never treated as SQL statements themselves.
• See https://xkcd.com/327/ for a funny use of SQL injection.
• Example 11: SQL for Kratylos
CS316 Spring 2022 17
• Purposes
• Exercise (in class): Write a web page with a button that, when
clicked, causes an AJAX call to http://www.cs.uky.edu/
˜raphael/courses/CS316/examples/example.07.cgi,
extracts the date from the response, and displays it.
• See this solution.
• Class 13: 2/22/2022
CS316 Spring 2022 20
23 JQuery
• JQuery, first released in 2007, is a JavaScript framework, that is, a
library that abstracts the DOM, makes JavaScript programming eas-
ier and uniform across browsers, and acts as a basis for many other
JavaScript libraries. JQuery was especially important before JavaScript
introduced querySelector() and fetch().
• The current version (released 3/2021) is 3.6.0.
• Features
This code uses a “minified” version and checks that it hasn’t changed.
The “anonymous” crossorigin prevents sending cookies.
• JQuery provides a third way to access the DOM (the first way is di-
rect JavaScript, the second is XPath).
color of their content to green and their font to Arial. This statement
should not include any anonymous functions.
• See this solution.
• Exercise (in class): Write a web page using jQuery that shows a
picture of an orange of width 20px (see Example 6) that when you
click changes its size over 5 seconds to 200px. After that, one more
click should change its size over 1 second to 300px. After that, click
should have no effect. Use an <img> tag with src and width
attributes.
• Class 15: 3/01/2022
• See this solution.
25 Midterm exam
• Class 16: 3/03/2022 Exam
• Class 17: 3/08/2022 After-exam review
26 Fonts
• The fonts available to the browser depend on fonts installed on the
user’s computer. Sometimes the web-page designer wants to use a
different font.
• Class 18: 3/10/2022
• CSS can include a font and refer to it via a URL:
@font-face {
font-family: myName;
src: url(location.woff);
}
• Browsers generally accept fonts in several formats: woff, woff2, ttf,
otf.
• The @font-face style may also list font-style, font-weight,
unicode-range.
CS316 Spring 2022 23
• By default, the browser shrinks the entire page to fit the width
of the browser, often making the content too small on a smart-
phone.
• a <meta name=’viewport’> tag in the <head> can specify
more useful values for the viewport.
• width=device-width says that the page width should be the
device width.
• initial-scale=2.0 says that the browser should set the ini-
tial zoom to that value.
• syntax:
@media not|only mediatype and (mediafeature and|or|not mediafeature) {
CSS-Code;
}
• CSS display: none (removes element) |inline (like span) |block (like
p) |...
• CSS visibility: visible |hidden (still takes up space) |...
• Accessing media: <video> (only MP4, WebM, and Ogg) <audio>
(only MP3, Ogg, and WAV) Safari does not play Ogg.
• Spellcheck and autocomplete; not available in all browsers.
• Exercise (in class): Build a web page with a table whose elements all
have border shading (gray, 5px horizontal, 5px vertical, blurring in
5px), with a 4×4 grid of values, but the inner 4 grid cells contain a
single entry: the letter X. The letter should be centered vertically and
horizontally in each cell. Pad each cell with 10px on all sides. Extra:
the big middle cell should have a tooltip saying ”Y”.
• Class 20: 3/24/2022
• See this solution.
28 Bootstrap: CSS
• Bootstrap is both a CSS library and a JavaScript library. Some of its
facilities require other libraries. It used to require jQuery, but it no
longer does.
• The current version (Spring 2022) is Bootstrap 5.1.3; as with all li-
braries and languages, versions change and new features appear.
• Standard reference.
• Main features
• At the end of the page, before the closing </body>, include JavaScript
libraries for Bootstrap and Popper (a tooltip-positioning package);
jQuery used be be required, but both Bootstrap and Popper have
stopped using it.
1 <script
2 src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
3 integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
4 crossorigin="anonymous"></script>
5 <script
6 src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"
7 integrity="sha384-7+zCNj/IqJ95wo16oMtfsKbZ9ccEh31eOz1HGyDuCQ6wgnyJNSYdrPa03rtR1zdB"
8 crossorigin="anonymous"></script>
9 <script
10 src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"
11 integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13"
12 crossorigin="anonymous"></script>
• Bootstrap provides its own CSS for all standard elements to enforce
its own look and feel.
• See Example 20.
• It also provides classes that further enhance the CSS. Example 21.
• Class 21: 3/29/2022
• Responsive design has breakpoints:
name abbreviation dimension
extra small none < 576px
small sm ≥ 576px
medium md ≥ 768px
large lg ≥ 992px
extra large xl ≥ 1200px
extra extra large xxl ≥ 1400px
reference to any breakpoint refers to screens of that size unless there
is an explicit override for a bigger size.
CS316 Spring 2022 26
• The grid system: 12 grids across, but items can be configured to use
a different number of grids on differently sized devices
• Forms: no special classes needed.
• Buttons: Various options.
• Exercise (in class): Build a web page using Bootstrap that has a table
spanning columns 7 to 12 in the grid scheme, with the first and
third rows each containing X X X (in three table columns) and the
second row containing a single form. The form should have an
input text region and a submit button. Feel free to make it fancy.
• See this solution.
• Modals (requires JavaScript): floating interactive region that hides
the rest of the page.
• Class 22: 3/31/2022
• Navbars.
• Tooltips
• The tooltip can be placed on any side, but Popper overrides the
placement if the result would be invisible.
• The tooltip can itself have style, but then the element needs
data-bs-html="true".
• For keyboard and users of assistive technology (like screen
readers): Only put tooltips on interactive, focusable elements
like buttons.
29 DataTables
• DataTables is jQuery plugin (that means it depends on jQuery) to
provide interactive tables.
• The manual is here.
• The tables can be formatted with Bootstrap.
• There are many options, some of which are shown in the example
web page.
• This example also demonstrates favicon and scrollbar customization
and a shaking image.
• See how Kratylos uses DataTables: www.kratylos.org/˜kratylos/
project.cgi
• Class 24: 4/7/2022
• Exercise (in class): Build a web page using DataTables that shows
the contents of the Unix file /etc/services. You also need to
build a back-end CGI script that formats this file in JSON. (You may
CS316 Spring 2022 28
use https://www.cs.uky.edu/˜raphael/courses/CS316/
examples/services.cgi)
• See this solution.
30 Typescript
• Reference
• TypeScript is an extension of JavaScript that is careful about types.
interface Backpack<Type> {
add: (obj: Type) => void;
get: () => Type;
}
declare const backpack: Backpack<string>;
const object = backpack.get();
backpack.add(23); // erroneous
31 IndexedDB
• It is possible to avoid DB manipulation on the server by putting the
DB in the client. A good example is IndexedDB, “a low-level API for
client-side storage of significant amounts of structured data”.
• IndexedDB is not relational; it is indexed, which means that entries
are key-value pairs; the key is considered the index, and the value
can be any object.
• The programmer must build a database schema. Then a JavaScript
program can open an internal connection to the database and then
retrieve and update data via transactions.
• Most browsers other than Internet Explorer support IndexedDB.
• IndexedDB follows a same-origin policy: only the site that installs
data can access or modify it.
• All operations are bundled within a transaction, which has a well-
defined life span, and which is failure- and synchronization-atomic.
• Operations are asynchronous; their completion signals an event that
the program can bind to a callback function, typically onsuccess
and onerror.
• Class 25: 4/12/2022
• Queries on an index generally produce a cursor that can iterate
through the resulting entries.
• IndexedDB Reference.
• IndexedDB example.
32 HTML canvas
• Class 26: 4/14/2022
• A <canvas> element lets you draw text and graphics.
• It requires JavaScript to actually perform the drawing.
• The JavaScript can build arcs, circles, polygons, rectangles, text.
• The jCanvas jQuery plugin makes programming easier.
• The items can be bound to events like click and mouseenter.
• Canvas example.
CS316 Spring 2022 30
• Exercise (in class): Build a 300×300 canvas with a blue circle that
moves away from the cursor whenever the cursor tries to enter it
but always remains somewhere inside the canvas.
• Class 27: 4/19/2022
• See this solution.
33 HTML SVG
• SVG (Scalable Vector Graphics) is a more recent component of HTML
that has the same abilities as Canvas.
• Graphic elements are placed in <svg> regions, and the code is part
of those regions, part of the DOM, and not dependent on JavaScript.
• The graphic items are scalable and not dependent on screen resolu-
tion.
• Documentation: https://www.w3schools.com/graphics/svg_
intro.asp.
• SVG is far more complex than the HTML canvas; you typically want
to use an SVG editor (such as https://svgeditoronline.com/
editor/) instead of building your own.
• Inside of <svg> you can place drawing commands such as <circle>,
<line>, and <rect>, but most drawing is done by grouping ele-
ments with <g> and using the <path> tag.
• SVG examples.
• Continue examples
• In-class exercise: Use SVG to make a red square with a blue circle
inside.
• See this solution.
• SVG viewbox attribute to scale contents.
• Continue examples
34 REST
• Web programming is an example of REST: REpresentational State
Transfer.
CS316 Spring 2022 31
35 Security worries
• CGI vulnerabilities
36 Defense methods
• Class 28: 4/21/2022
• Use https instead of http as the protocol. This connection
provides authentication (you can be sure of the identity of the
server), privacy (intruders cannot decrypt the conversation) and
integrity (intruders cannot modify the conversation without
destroying it).
• The web site can include an .htaccess file that forces https,
and the web server can be configured to only allow https
connections.
• The web server must be configured to present the certificate
and to list the security protocols it accepts.
37 React
• There are many high-level frameworks available, such as Angular
and Vue.js. The most popular at the moment is React, a JavaScript
library developed by Jordan Walke at Facebook and first released in
2013. The current version is 16 (April 2022).
• React uses JSX (JavaScript XML), a JavaScript syntax extension that
looks like HTML. All JSX expressions must contain a single element,
which may contain sub-elements.
• The babel plugin translates JSX code to JavaScript as needed.
• React relies on some advanced features of JavaScript, enumerated at
https://www.w3schools.com/react/react_es6.asp.
• React renders pieces of HTML; those pieces can be strings, functions,
or classes.
• The easiest method is to build a JavaScript function that returns HTML.
Its name must start with a capital letter. You can then cause the
HTML returned by the function to be rendered as a new tag inside
a given element. You can render the same function more than once.
For very simple HTML, you can place it as the value of a string and
render the string.
• See examples/example.26.html