Chapter 13 Intro to Server-Side 13 Intro to Server-Side ... Client side or Server side? ... HTML style server-side scripting languages work

  • View
    221

  • Download
    6

Embed Size (px)

Text of Chapter 13 Intro to Server-Side 13 Intro to Server-Side ... Client side or Server side? ... HTML...

  • 1

    Chapter 13Intro to Server-Side

    ProgrammingPresented by Thomas Powell

    Slides adopted from HTML & XHTML: The Complete Reference, 4th Edition

    2003 Thomas A. Powell

    Web Programming Toolbox

    Server-side scripting* Active Server Pages (ASP)* ColdFusion* PHP

    Scripting Languages* JavaScript* VBScript

    Java ServletsJava Applets

    ActiveX Controls

    Server API Programs* ISAPI* NSAPI* Apache Modules

    Netscape Plug-ins

    CGI scripts and programsHelper Applications

    Server SideClient Side

    Web Programming in Context

  • 2

    Client-Server Overview

    Web is a client-server environment Web browser is typically the client Web server is obviously the server In some ways Web server is a file server, but it

    could be more of an application server when interactivity is added

    There is a question of where the computing in the Web should run. Client side or Server side?

    Client v.s. Server

    Client side programming can be problematic because you dont have a great deal of control over what the user has Screen size Java/JavaScript support Browser differences Hardware

    Server side programming can be problematic because it puts all the responsibility on the server and can cause performance problems

    The best approach is a balance with nothing being to heavily relied on particularly client side wise. Everything should be done in a defensive manner.

    CGI Basics

    The most basic server-side way to add interactivity to a Web site is through a CGI (Common Gateway Interface) program.

    CGI = Common Gateway Interface CGI is not the program it is the way things are interfaced CGI specifies how data should passed in from a Web page and back

    out CGI Process

    1) User submits form2) Form is sent to server and eventually CGI program3) CGI program processes data and responds back4) Web server passes CGI response to client

    The important part is step 2 and the end of step 3 where data goes back and forth.

  • 3

    CGI Up Close

    2.1 Server determines if request is a document or program 2.2 Server locates the program and determines if the program can

    be executed 2.3 Server starts program (expensive) and sends it the data from

    form and environment 2.4 Program runs 2.5 Server waits for program to finish and takes the result and

    passes it back to client

    Understanding CGI requires an understanding of HTTP and forms

    How the Web Works

    Pretend to be a Web browser Telnet www.yourfavoritesite.com 80 Eventually at the prompt type in

    GET / HTTP/1.0 Hit return twice

    Watch server respond back with headers and then HTML markup

    Key to interaction is the MIME type as indicated by the response header Content-type: text/html This header tells the browser what it is reading

    To dynamically make HTML from a server program just stamp the output with the appropriate MIME type.

    CGI Example

    Try http://www.htmlref.com/cgi-bin/firstcgi.pl

    This file is a short Perl program like this

    #!/usr/bin/perl

    print "Content-type: text/html\n\n";

    print"\nFrom CGI\n";

    print"\nWow! I was created from a CGI program!!\n

    More Stuff

    ";

    print"\n";

    The key to making this CGI was literally the first print statement and the fact it was run on the Web

  • 4

    Reading Data In

    The output half of CGI is just printing the appropriate HTML markup and headers, the input is slightly harder.

    There are two primary ways of getting data into a CGI program Environment variables Forms

    When a request takes place headers from the browser and server are combined to create the environmentThis is easily accessible from a programming language.

    Demo http://www.htmlref.com/cgi-bin/printenv.cgi

    CGI Form Example

    Registration Form

    Name:

    E-mail:

    CGI Form Example

    #!/usr/bin/perl

    require ("cgi-lib.pl");&ReadParse;

    $name = $in{"username"};$email = $in{"email"};

    print "Content-type: text/html\n\n";print "Hello $name. Nice to meet you!\n";print "Email: $email";

    On the server you have a program like this called formtest.pl

    Notice the use of a library to do much of the parsing work

  • 5

    Writing CGI

    What language do you write your CGI program in? Whatever you want C, Java, Pascal, Fortran, Cobol, PERL, Python, etc. PERL is common because portable and good at text

    handling Take a look at the numerous libraries and example

    CGI code online at sites like http://www.cgi-resources.com

    The Joy of Server Side Programming

    Your main problem with writing CGI programs will be 1) Parsing data sent in via forms and environment

    Let the Library do it!

    2) Dealing with state management This is not unique to CGI as the bane of all server-side

    development on the Web Why? HTTP is stateless! It doesnt know you from request to

    request. Three approaches to state problem1) HIDDEN form data2) Extended Path info3) Cookies!

    Again hopefully youll rely on libraries to deal with this pain

    CGI Speed Complaints

    CGI has speed issues Launch Language

    Solve some of this by simply writing in a compiled language or using a special CGI engine to speed up execution related to launch

    Real speed improvement comes with server modules

  • 6

    Server Modules

    Server modules are special programs written to the API of a particular Web server that generally run within the task space of the server ISAPI Microsoft IIS API NSAPI Netscape Server API Apache Module Apache Server API Java Servlets Trying to be cross server module

    format but you need an engine to deal with it Server modules are of course much more

    difficult to write than CGI programs and have server stability downsides if they crash

    Server Module Resources

    ISAPI http://msdn.microsoft.com/library/psdk/iisref/isgu6j5f.h

    tm

    NSAPI http://docs.iplanet.com/docs/manuals/enterprise/41/ns

    api/contents.htm

    Apache Modules http://www.apache.org

    Java Servlets http://java.sun.com/products/servlet/index.html

    Server Side Scripting Intro

    Server-side scripting often dubbed parsed HTML solutions offer a third solution to server-side programming

    Consider the idea of writing a special BozoScriptserver module that looks to see if a file is being requested with an extension .bozo If it sees this it parses the page, if not it passes it on. If you get this idea you understand how all parsed HTML style server-side scripting languages work as shown in the next slide

  • 7

    Parsed Script Intro

    Basic parsed HTML is Server Side Includes (SSI) .shtml files In the form of a comment

    Some basic ones are common to all servers, but some others are unique to a particular Web platform

    Common Server Side Script languages ColdFusion (www.allaire.com) [.cfm] Active Server Pages from Microsoft [.asp] PHP [.php] JSP [.jsp] On and on.

    ColdFusion Markup Overview

    ColdFusion (www.macromedia.com) is a simple server-parsed tag based scripting language that is very focused on connecting databases to Web pages ColdFusion requires a special Application Server and it runs on NT,

    Linux, and Solaris Like other server parsed scripting solutions the app server is

    engaged when a file of a particular extension (.cfm) is requested Cold Fusion markup looks like basic HTML tags and thus is

    popular with people who are new to programming

    Main purpose of CF is to connect to a database Either using ODBC or native database drivers

  • 8

    ColdFusion Markup Overview You need to specify basic SQL (Structured Query

    Language) statements to use CF Given a DB table called Positions with the fields Position-

    Num, JobTitle, Location, Description, Hiring Manager, and PostDate. It is easy to query the table using statements like

    SELECT * FROM Positions

    SELECT * FROM Positions WHERE Location="Austin"

    SELECT *FROM PositionsWHERE ((Location="Austin" OR

    (Location="Los Angeles") AND(Position="Game Tester"))

    SELECT * FROM Positions

    Note: The DATASOURCE attribute is set equal to CompanyDataBase, which is the ODBC data source that contains a database called Company, which contains the Positions table from which data is pulled.

    supports attributes like NAME, DATASOURCE, MAXROWS, USERNAME, PASSWORD, TIMEOUT, and DEBUG

    Once you have data for example from variables,

    calculations or a from a database query you can output it with

    Position Number: #PositionNum#Title: #JobTitle#Location: #Location#Description: #Description#

    Notice the use of # symbols to relate the fields of the database with output slots Also notice how HTML and database fields are intermixed.

  • 9

    SELECT * from Positions

    Demo Company Job Listings

    Job Listings

    Position Number: #PositionNum#Title: #JobTitle#Location: #Location#Description: #Description#

    Demo Company, Inc.

    Common CFML Elements

    CFM Summary

    Database specific Very high level (like a 4GL) Tag like so integrates well with HTML Some programmers find clumsy because

    not script like However, functionally equivalent to any

    other script language Still NT oriented despite appearing cross

    server

  • 10

    ASP Intro Microsofts Active Server Pages (ASP) is probably

    the most popular server parsed scripting technology used today and is indicated by files ending in .asp Likely because it is free and Microsoft oriented VB or JavaScript oriented Reality is script neutral

    You can use any script language in an ASP page but most people do not