Chapter 29: ASP Syntax
1. What is “ASP Syntax” really about?
ASP Syntax = the rules that tell the server how to separate:
- normal HTML (which is sent to the browser unchanged)
- server-side code (which runs on the server and can generate dynamic HTML)
In Classic ASP, this separation is done with special tags:
| Tag | Purpose | When to use it |
|---|---|---|
| <% … %> | Server-side code block (VBScript / JScript) | For logic, variables, loops, conditions |
| <%= expression %> | Shortcut to output (print) a value | To insert dynamic values into HTML |
| <!– #include … –> | Include other .asp / .inc files | To reuse common code (header, footer) |
Everything outside <% %> is sent directly to the browser as HTML.
Everything inside <% %> is executed on the server — never visible to the browser.
2. The Golden Rule – Browser sees only HTML
No matter how much server code you write, the final response is pure HTML + CSS + JavaScript. The browser never sees <%, never sees VBScript, never sees Response.Write.
This is the #1 thing beginners misunderstand.
3. First Real Example – Hello World with Syntax
File name: hello.asp
|
0 1 2 3 4 5 6 |
<%@ Language=VBScript %> <% Option Explicit %> <!DOCTYPE html> <html> <head> <title>ASP Syntax Example</title> </head> <body> <h1>Hello from Classic ASP!</h1> <% ' This is a comment inside code block Dim name name = "Rahul from Hyderabad" ' Long way to output Response.Write "<p>Welcome, " & Server.HTMLEncode(name) & "!</p>" %> <!-- Shortcut way to output (most common) --> <p>Server time right now: <%= Now() %></p> <p>Today's date: <%= Date() %></p> <p>Current hour: <%= Hour(Now()) %></p> </body> </html> |
What the browser receives (View Source in browser):
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<!DOCTYPE html> <html> <head> <title>ASP Syntax Example</title> </head> <body> <h1>Hello from Classic ASP!</h1> <p>Welcome, Rahul from Hyderabad!</p> <p>Server time right now: 16/02/2026 14:03:22</p> <p>Today's date: 16/02/2026</p> <p>Current hour: 14</p> </body> </html> |
→ No trace of <%, Dim, Response.Write, Server.HTMLEncode — all gone!
4. Most Important Syntax Elements (with Examples)
A. <% … %> – Code block (logic, variables, loops, conditions)
|
0 1 2 3 4 5 6 |
<% Dim age age = 25 If age >= 18 Then Response.Write "<p>You are an adult.</p>" Else Response.Write "<p>You are a minor.</p>" End If Dim i For i = 1 To 5 Response.Write "<p>Loop number " & i & "</p>" Next %> |
|
0 1 2 3 4 5 6 |
B. <%= … %> – Shortcut to print a value (most common way to output) |
|
0 1 2 3 4 5 6 7 8 |
<p>Hello, <%= Request("username") %>!</p> <p>Your IP: <%= Request.ServerVariables("REMOTE_ADDR") %></p> <p>Random number: <%= Int(Rnd * 100) + 1 %></p> → <%= expression %> is exactly the same as <% Response.Write expression %> |
C. Mixing code and HTML (very typical pattern)
|
0 1 2 3 4 5 6 |
<h2>Shopping Cart</h2> <% Dim total total = 0 Dim items items = Array("Biryani", "Haleem", "Irani Chai") Dim prices prices = Array(399, 450, 80) %> <table border="1"> <tr><th>Item</th><th>Price</th></tr> <% For i = 0 To UBound(items) %> <tr> <td><%= items(i) %></td> <td>₹<%= prices(i) %></td> </tr> <% total = total + prices(i) %> <% Next %> </table> <p><strong>Grand Total: ₹<%= total %></strong></p> |
→ Notice how code blocks and HTML alternate — very common in Classic ASP.
D. <!– #include file=”…” –> – Include files
Very important for reuse (header, footer, database connection, functions)
|
0 1 2 3 4 5 6 |
<!-- #include file="header.inc" --> <!-- #include file="db_connection.asp" --> <h1>Main Content</h1> <!-- #include file="footer.inc" --> |
header.inc might contain:
|
0 1 2 3 4 5 6 |
<!DOCTYPE html> <html> <head> <title>My Site</title> </head> <body> |
5. Teacher Summary – ASP Syntax in One Page
Classic ASP Syntax (what W3Schools teaches in “ASP Syntax”):
- <% … %> → run VBScript code (variables, If, For, loops, Response.Write)
- <%= expression %> → print a value safely into HTML
- <%@ Language=VBScript %> → at very top (optional)
- Option Explicit → force variable declaration (very good habit)
- Server.HTMLEncode() → prevent XSS (almost always used with user input)
- Request.Form(“key”), Request.QueryString(“key”), Session(“key”) → get data
- <!– #include … –> → reuse code across pages
Browser sees only HTML — all <% %> code disappears.
This syntax powered millions of websites 1998–2010 and is still alive in many old Indian banking/government/ERP systems in 2026.
Questions for next class?
- Want to see a full login system in Classic ASP (very typical 2000s code)?
- Or database connection + recordset loop in detail?
- Or compare Classic ASP syntax vs Razor syntax side-by-side?
- Or shall we move back to Razor / modern .NET?
Just tell me — I’m here! 🚀🇮🇳 Happy learning, Webliance! 😊
