Dynamically generated web pages
Background
- Using HTTP, a web client asks the web server for an HTML document.
- The web server sends the requested document back to the user-agent (web browser).
- The browser displays the document as defined by the HTML and CSS to the client.
- The most basic documents are static HTML documents stored in specific, publicly accessible directories on the server.
- HTML and CSS provide the basic framework for Web documents, but they are limited to structuring and presenting fixed content.
- That is, they don't control what the content will be, only how to organize and display it.
Dynamic Content
- Many sites have dynamically changing and interactive content.
- Examples are as simple as the current date and time, or as complex as a shopping cart system.
- If you look at the page source code, it is still HTML.
- But the HTML is not entirely written using a program editor.
- Instead it is generated as the output of a program.
Client-side Scripting
- Some programs are executed within the web browser.
- It can react to events on the document, such as the page loading, and can modify the displayed document.
- A client-side program can be used to validate (check for completeness and accuracy) a form before it is sent to the server for further processing.
- It can use a scripting language such as JavaScript or VBscript.
- Client-side scripting has some limitations with the scope of the languages.
- Its efffectiveness also depends on the user's browser being able to use the language, something that cannot be guaranteed.
Server-side Programming
- Basic HTML provides forms for getting information from the user.
- All browsers, whether GUI or line-mode or audio or other, support using forms
- The data collected in the forms can be sent back to the server for processing by a server-side program.
- The kind of programming is possible depends on the server rather than on the client.
- Documents that use server-side programming MUST be called through a server.
- You cannot just open them locally in your browser.
Server-side programming capabilities
With server-side applications, you can:
- Dynamically edit, change, or add any content of a Web page, customizing it for individual users
- Customize output for different types of browsers
- Respond to user queries or data submitted from HTML forms
- Access any data or databases and return the results to a browser
- Access files or execute other programs on the server and return the results to a browser
Server-side languages
HTTP server software can be extended to incorporate a number of languages.
- Microsoft Active Server Pages
- The Microsoft IIS server supports Active Server pages (ASP). ASP files are HTML documents with special embedded script tags. The scripts written in the tags are executed by the server's ASP engine before the dpcument is returned to the client. The ASP scripts are most commonly written in VBScript or JScript (A Microsoft version of JavaScript), but other languages can be added. Similarly, JSP documents incorporate programs in the language Java that are executed by the server.
- Common Gateway Interface
- Web servers can also execute programs on the host computer through the Common Gateway Interface (CGI). This approach is very versatile, in that any program that can be run on the host computer can be used, but it is slower than if the processing is an integral part of the server software. CGI programs were often written in the languages C and Perl.
- Server Side Includes
- The Server Side Include (SSI) capability provided on most servers handles some commands directly by the server and others externally, including commands that will execute system commands and programs.
- PHP
- PHP is a language that was developed especially for use on the Web. The interpreter is integrated into the server, and the script code is embedded into the HTML page.
- LAMP
- The Apache server, the most commonly used server on the Web, supports SSI and CGI, and can be extended to handle server-executed scripting in a number of languages including the three Ps - Perl, PHP, and Python. The most common, and powerful, web server environment is referred to as "LAMP" - Linux, Apache, MySQL (a database server), and Perl/PHP/Python - all open source software products.
How many ways can we say Hello World?
Static HTML
<html> <head><title>Saying Hello</title></head> <body> <h1>Hello World!</h1> </body> </html>
client-side JavaScript
<html> <head> <title>Saying Hello</title> <script language="Javascript"> <!-- function message () { document.write("<h1>Hello World!</h1>"); document.close(); } // --> </script> </head> <body onload="message(); return true"> </body> </html>
Server Side Include
Requires that the server be appropriately configured. On wyrd.hood.edu, files must have the extension .shtml
<html> <head> <title><!--#include file="title.txt" --></title> </head> <body> <h1><!--#include file="message.txt" --></h1> </body> </html>
where title.txt contains "Saying Hello" and message.txt contains "Hello !"
Server-side CGI script:
Requires that the server be appropriately configured. On wyrd.hood.edu, files must have the extension .cgi
and must be executable. There are many language choices, including C, C++, Python, BASIC, etc. Any programming language can be used as long as it can read from standard input and write to standard output.
Unix Shell script
#!/bin/sh echo -e "Content-type: text/html\n\n" title="Saying Hello" message="Hello World!" echo "<html>" echo "<head><title>$title</title></head>" echo "<body>" echo "<h1>$message</h1>" echo "</body>" echo "</html>" echo -e "\n"
Perl program
#!/usr/bin/perl $title="Saying Hello"; $message="Hello World!"; print "Content-type: text/html\n\n"; print " <html> <head> <title>$title</title> </head> <body> <h1>$message</h1> </body> </html> ";
Perl and Python applications can be made much faster by adding the mod_perl and mod_python modules to Apache. These embed the interpreters in the server, saving the step of externally running the program.
Java servlet
Requires that the server be appropriately configured
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorldServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Saying Hello"; String message = "Hello World!"; out.println("<html>"); out.println("<head><title>" + title + "</title></head>"); out.println("<body>") out.println("<h1>" + message + "</h1>") out.println("</body>"); out.println("</html>"); out.close(); } }