A classic web application is a program that runs on the server and generates a character string which is valid HTML. It is hard to imagine a programming language that could not generate a string of characters, so any programming language should be able to do this.
Let's look at some examples.
The three languages of the web
But first a quick review. The invention of the web introduced three new languages, shown here in no particular order:
- a one-liner language for specifying resource locations: URL (Universal Resource Locator)
- a protocol (to run on top of TCP/IP) for request/response: HTTP (HyperText Transfer Protocol)
- a markup language for content: HTML (HyperText Markup Language)
Web page vs web application
Web application with HTML inside
CGI script written in bash
#!/bin/bash echo "Content-type: text/html" echo echo -n 1 >>../../tallies COUNT=`cat ../../tallies | wc -c` cat <<ENDMARKER <!doctype html> <html> <head> <title>Contrivance without conclusion</title> <meta name="format-detection" content="telephone=no"> </head> <body> <h1>Con without con</h1> <h2>Contrivance without conclusion</h2> <p>This page has been visited $COUNT times.</p> <p>Latest visit from IP $REMOTE_ADDR on $(date).</p> <p>Sample program from <a href="http://conwithoutcon.blogspot.com/2014/05/contrivance-without-conclusion.html" target="_BLANK">Contrivance without conclusion</a>.</p> </body> </html> ENDMARKER
Variations in the page require a web application
$COUNT
, a variable in the web application which holds a number (computed in line 5)$REMOTE_ADDR
, an environment variable from the web server which holds the browser's IP address$(date)
, a call out to a program provided by the operating system of the server running the web server which is the current date and time (in the time zone of the server)
Web application that looks like HTML
In the previous section we saw a program that had HTML embedded inside of it.
Here we consider a program that has the HTML outside.
<html> <head><title>First JSP</title></head> <body> <% double num = Math.random(); if (num > 0.95) { %> <h2>You'll have a lucky day!</h2><p>(<%= num %>)</p> <% } else { %> <h2>Well, life goes on ... </h2><p>(<%= num %>)</p> <% } %> <a href="<%= request.getRequestURI() %>"><h3>Try Again</h3></a> </body> </html>
This web application is written in a language known as JSP (Java Server Pages).
A superficial glance at the first few lines might remind one of HTML.
But it contains some things that look a bit like HTML tags but have a percent sign in them. Such as <% ... %>
and <%= ... %>
. What could this mean?
JSP is a language in a series of programming languages with a long tradition, dating back to PHP (which originally stood for Personal Home Page) in the early days of the web, quickly followed by ASP (Active Server Pages). The initiator of this concept, PHP, used question marks and exclamation marks where ASP and JSP use the percent sign.
This example comes from Getting Starting with JSP with Examples, with thanks to the author thereof.
How does it work?
When the web server sees a resource whose URL ends in ".jsp" it quickly converts it into an actual Java program (called a servlet) and runs that program to produce the page.
In essence, it turns it inside out***, in that the servlet is a web application with HTML inside.
For the curious, an excerpt of the servlet code is shown in the getting started page linked to above, in its section "Behind the Scene".
No comments:
Post a Comment