Server-side TechnologiesCGI, PHP, Java Servlets, JSPDenis Helic
CGI Specification(3/4)HTTP method used by the client: GET or POSTGET method: external program reads environment variablesQUERY_STRING special environme
CGI Specification(4/4)CGI specification allows external programs to be written in any program-ming languageUNIX shell scripts, Perl scripts, C programs,
CGI Examples(1/7)Example 1:Hello World: CGI as UNIX shell scriptGET method, no parameters from clientWrite HTML to st dout#!/bin/sh# send http-header
CGI Examples(2/7)Example 1 (continued):# send html content:echo "<HTML>"echo " <HEAD>"echo " <TITLE>Hello W
CGI Examples(3/7)Example 2:Dump environment variables: CGI as Perl scriptGET method, no parameters from clientWrite HTML to st dout#!/usr/bin/perlrequ
CGI Examples(4/7)Example 2 (continued):Example:http://coronet.iicm.edu:8080/cgi-bin/mmis/p rint env. plSpecial CGI library in Perl: cgi-libProvides fu
CGI Examples(5/7)Example 3:Dump QUERY_STRING: CGI as Perl scriptGET method, with parameters from clientWrite HTML to st doutParameters encoded in Url:
CGI Examples(6/7)Example 4:Evaluate HTML forms: CGI as Perl scriptPOST method, with parameters from client, read from stdinWrite HTML to st dout#!/usr
CGI Examples(7/7)Example 4 (continued):Example:http://coronet.iicm.edu:8080/mmis/examples /cgi /for m.ht ml<form action ="/cgi-bin/mmis/handle
CGI Applications(1/2)Long list of different applications:Simple: Hit counters, current date, etc .Handling HTML forms, search engines, imagemaps, datab
Server-side Technologies: Historical Background(1/3)Server-side = Web server sideAt the beginning the Web was a static information systemWeb servers s
CGI Applications(2/2)Finger gateway:http://coronet.iicm.edu:8080/cgi-bin/mmis/f inge r.plSource:http://coronet.iicm.edu:8080/mmis/examples /cgi /finge
CGI SecurityCheck parameters carefully!!!if($email =~ /[^a-zA-Z0-9_\-\.@]/){$_ = "The email address should be ofthe form <i>user\@server<
CGI - PerlLarry Wall: Practical Extraction and Reporting LanguageString manipulations, r egular expressionsVery powerfulStrange syntax :-) (e .g. 1 wh
PHP: Hypertext Preprocessorhttp://www.php.net(NOT http://www.php .com = Parents helping Parents :-))General purpose scripting language, especially sui
PHP: Hello World(1/3)Embed PHP script into an HTML fileUpload the file onto a Web server using extension .phpEmbedding PHP in HTML:< ? ... ? ><
PHP: Hello World(2/3)Example:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd&quo
PHP: Hello World(3/3)Example:http://coronet.iicm.edu:8080/mmis/examples /php /hel lo/hello.phpSource:http://coronet.iicm.edu:8080/mmis/examples /php /
PHP: SyntaxPHP syntax close to C and JavaObject-oriented approachControl structuresWeakly-typed variables (prefix ’$’)Operators, etc.(27/95)
PHP: ApplicationsWide range of applications (similar to CGI)Forms handling, etc.Wide range of PHP librariesNetwork connectivity (e.g. access FTP, IMAP
PHP: Handling Forms(1/8)PHP interpreter initializes variables correpsonding to form fi elds<form action ="/mmis/examples/php/env_vars/printvar.
Server-side Technologies: Historical Background(2/3)There was a need for more interaction between users and the system (e.g.phone books)HTML formsServ
PHP: Handling Forms(2/8)PHP form variables: Alternative 1PHP variables have same names as form fields$name for name, $nr for nr, etc.<?phpecho "
PHP: Handling Forms(3/8)Example with GET:http://coronet.iicm.edu:8080/mmis/examples /php /env _var s/var_get.htmlExample with POST:http://coronet.iicm
PHP: Handling Forms(4/8)PHP form variables: Alternative 2Access form fields through PHP array$HTTP_POST_VARS for POST method$HTTP_GET_VARS for GET meth
PHP: Handling Forms(5/8)PHP form variables: Alternative 3Access form fields through PHP array$_POST for POST method (>=PHP4. 1.0)$_GET for GET metho
PHP: Handling Forms(6/8)Handling forms: Security issuesSimilar problems like with CGIWe need to check parameters sent by users very carefully!!!PHP fo
PHP: Handling Forms(7/8)Example of security problem with global form variables$tempfile = "1 2345 .tmp ";... han dle form variables ... d
PHP: Handling Forms(8/8)Example of security problem with global form variables (continued)Suppose a following HTML form:<input type = "hidden&
PHP: Database Manipulation(1/5)Huge advant age of PHP: great s upport for database connectivityAdabas-D, mSQL, MySQL, Oracle, Postgres, Slid, Sybase/S
PHP: Database Manipulation(2/5)Example: Inserting and retrieving data from MySQL databaseForm:http://coronet.iicm.edu:8080/mmis/examples /php /mys ql/
PHP: Database Manipulation(3/5)<?php$name = $HTTP_ POST _VAR S["n ame" ];$second_name = $HTTP_POST_VARS["second_name"];$nr = $H
Server-side Technologies: Historical Background(3/3)Need to extend the functionality of Web serversDon’t add the new functionality into Web servers di
PHP: Database Manipulation(4/5)Inserting data with PHP (source):http://coronet.iicm.edu:8080/mmis/examples /php /mys ql/register.phpsRetrieving data w
PHP: Database Manipulation(5/5)...while($i < $ro ws){$name = mysql_ resu lt($ resu lt, $i, "name");$second_name = mysql_result($result, $
PHP: XML Manipulation(1/3)Additional PHP library for manipulating XML dataPEAR library: http://pear.ph p.ne t/Packages for networking, scientific calcu
PHP: XML Manipulation(2/3)header("Content-Type: text/xml");include("XML/Tree.php");$tree = new X ML_T ree( );$root = & $tree-&
PHP: XML Manipulation(3/3)Retrieving data (as XML) with PHP:http://coronet.iicm.edu:8080/mmis/examples /php /xml /get _registered.phpRetrieving data (
PHP: Image Manipulation(1/3)Generate not only HTML, but digital images as well!PHP compiled with GD graphical libraryStandard installation c omes with
PHP: Image Manipulation(2/3)Header("Content-Type: image/png");...$im = ImageCre ateT rueC olor (400 , 300);...ImageFill($im, 0, 0, $ whit e)
PHP: Image Manipulation(3/3)Retrieving data (as PNG image) with PHP:http://coronet.iicm.edu:8080/mmis/examples /php /ima ge/get_stats.phpRetrieving da
PHP: Tutorials and ResourcesPHP Introductory Tutorial:http://www.php.net/tut.phpPHP/MySQL Tutorial:http://hotwired.lycos.com/webmonkey/progra mmin g/p
Java Servlets and Java Server Pages (JSP)Intro tutorial:http://www.apl.jhu.edu/~hall/java/Servlet-T utor ial/Book: Marty Hall, Core Servlets and JavaS
Server-side Technologies: TodayMore than just evaluating of HTML formsDynamic content neede d for:Sophisticated user interaction (e.g. search engines,
Java ServletsJava technology’s answer to CGI programmingJava programs that run on a Web serverJava servlet engine (container)Official Reference Implemen
Java Servlets: Advantages(1/4)EfficientWith traditional CGI: for e ach request a new OS process is startedJava VM, servlet container, and a particular s
Java Servlets: Advantages(2/4)ConvinientIf you already know Java (most probabaly you do ;))Huge Java software librariesLibraries for handling cookies,
Java Servlets: Advantages(3/4)PowerfulJava servlets can talk directly to the Web se rver (e .g. lookup forimages stored in standard places)Servlets ca
Java Servlets: Advantages(4/4)PortableWritten in Java with a standardized APIServlets written for Microsoft IIS will run on Apache and other Webserver
Installing Servlet Container(1/3)Servlet ContainerTomcathttp://jakarta.apache.org/tomcat/index.htm lApache software foundationhttp://www.apache.orgfor
Installing Servlet Container(2/3)installation tomcat# installation in verzeichnis ’/foo ’cd /foounzip <path-to-tomcat-archive>/jakarta-tomcat-4
Installing Servlet Container(3/3)Windows installation with Windows installerInstalled as a Windows serviceConnecting with a Web server (e.g. Apache)In
Java Servlets - Internal(1/2)Java class extending abstract classjavax.servlet.http.HttpServletImplement public void doGet(request, respons e) to handl
Java Servlets - Internal(2/2)servlet template:import java.io.*;import javax.servlet.*;import javax.servlet.http.*;public class SomeServlet extends Ht
Communication between Web server and external programsHow should Web server communicate with external programs?Passing parameters, getting response, e
Java Servlets: Hello World(1/5)Example: Hello World!...public void doGet(HttpServletRequest re ques t, HttpServletResponse response)throws ServletExce
Java Servlets: Hello World(2/5)Installing and running the HelloWorldServletTomcat web applications (in webapp directory)|-mmis-servlets| || |-WEB-INF|
Java Servlets: Hello World(3/5)web.xml declares all servlets in a particular Web application<?xml version="1.0" encoding="ISO-8859-1
Java Servlets: Hello World(4/5)Hello World:http://coronet.iicm.edu/mmis-servlets/Hello Worl dSource code:http://coronet.iicm.edu/mmis/examples/java /h
Java Servlets: Hello World(5/5)Element Construction Set (Apache project)http://jakarta.apache.org/ecs/Supports generation of HTML and XMLNo need for n
Java Servlets: HTTP and Environment Variables(1/2)Similar communication mechanism between a Java servlet and the WebserverAll communication wrapped in
Java Servlets: HTTP and Environment Variables(2/2)CGI Variables:http://coronet.iicm.edu/mmis-servlets/CGIVa rSource code:http://coronet.iicm.edu/mmis/
Java Servlets: Handling Forms(1/2)All form parsing done automaticallyInvoke a method on the instance of HttpServletRequest class to obtainparametersSt
Java Servlets: Handling Forms(2/2)Example with GET:http://coronet.iicm.edu/mmis/examples/java /for m/fo rm_get.htmlExample with POST:http://coronet.ii
Java Servlets: Database Manipulation(1/5)Advantage of Java: great support for database connectivitySimilar to PHPJava Database Connectivity - JDBChttp
Common Gateway Interface (CGI)CGI is a specification of communication between Web server and externalprogramsCurrent version CGI 1.1http://hoohoo.ncsa.
Java Servlets: Database Manipulation(2/5)Example: Inserting and retrieving data from MySQL databaseForm for inserting data:http://coronet.iicm.edu/mmi
Java Servlets: Database Manipulation(3/5)Connection connection = DriverManager.getConnection("jdbc:mysql://" + dbms_host_ + "/" +
Java Servlets: Database Manipulation(4/5)Retrieving data with JavaConnection connection = DriverManager.getConnection(...);Statement statement = conne
Java Servlets: Database Manipulation(5/5)Retrieving data with Javahttp://coronet.iicm.edu/mmis-servlets/Regis trat ionRetrieving data with Java (sourc
Java Servlets: XML Manipulation(1/2)Java SE 1.4+ includes library for manipulating XML dataElement root = documen t.cr eate Elem ent( "Cou rse&qu
Java Servlets: XML Manipulation(2/2)Retrieving data (as XML) with Java:http://coronet.iicm.edu/mmis-servlets/XMLRe gist rati onRetrieving data (as XML
Java Servlets: Tutorials and ResourcesJava Servlets Introductory Tutorial:http://www.apl.jhu.edu/~hall/java/Servlet-T utor ial/Book: Marty Hall, Core
Java Server Pages (JSP)Combine static HTML with Java Code<HTML><HEAD><TITLE>JSP-Hello World</TITLE></HEAD><BODY>St
Java Server Pages (JSP) - InternalJSP pages are converted to Java classes<tomcat-dir>/work/localhost/helloworld$jsp .jav aclassname:helloworld$j
JSP Elements(1/7)JSP expression<%= "H ello World <BR>" %>XML syntax: <jsp:expression>"HelloWorld<BR>"</
CGI Specification(1/4)Environment variablesSystem specific variables set by Web serverExternal program reads environment variables and obtains dataabout
JSP Elements(2/7)JSP Scriplet<% out .pri nt(" Hell o World <BR>"); %>XML syntax: <jsp:scriptlet>out.print("HelloWorld
JSP Elements(3/7)JSP Declaration<%! pr ivat e int access_count = 0; %>XML syntax: <jsp:declaration>privateintaccess_count=0</jsp:declar
JSP Elements(4/7)JSP Page Directive<%@ pa ge import = "java.util.*" %>XML syntax: <jsp:directive.page import=”java.util.∗”/>Dire
JSP Elements(5/7)JSP commentsJSP Include Directive (includes other files at run-time)JSP Elements to handle Java Beans(83/95)
JSP Elements(6/7)JSP predefined variablesrequest, responseoutsessionconfig, pageContext(84/95)
JSP Elements(7/7)Example:http://coronet.iicm.edu/mmis-servlets/jsp/e xamp le.j spExample:http://coronet.iicm.edu/mmis-servlets/jsp/e xamp le.j sp?data
Servlets, CGI, JSP, PHP, ... - Problems!(1/3)Common problems of all server-side generated Web applicationsMixing of content and presentationHard to de
Servlets, CGI, JSP, PHP, ... - Problems!(2/3)Servlets have this problem alsoPresentation designer needs to program in JavaPossible solutionDump conten
Servlets, CGI, JSP, PHP, ... - Problems!(3/3)Java Web Frameworks try to solve this problemCoocon (XML Publishing framework)http://xml.apache.org/cocoo
Servlets, CGI, JSP, PHP, ... - What to take?Depends on application requirements (e.g. database connectivity, perfor-mance, etc.)Depends on know-how, t
CGI Specification(2/4)Standard InputUsed by the server to send client data to external programStandard OutputUsed by external program to send response
Session Tracking(1/5)HTTP is connection-less: one connection per requestInformation about user/session is lost whenever the connection is closedOften
Session Tracking(2/5)Keep track with:CookiesHidden form fields:<INPUT type=”HIDDEN”name=”sessionInfo”value=”username”>Url-rewriting:e.g. http://c
Session Tracking(3/5)CookiesStrings sent from server to Web browserStored on a client side database, files or in memorySent back from browser to the We
Session Tracking(4/5)Used to store the state of communication between a client and the serverServer sets the read rigths for a cookie (i.e. who can re
Session Tracking(5/5)High level interfaces in PHP, Java Servlets APIJava servlets API manages sessions with cookies or url rewritingTransparent to pro
Distributed Programming on the WebVery hot topic right now.NET from MicrosoftWeb servicesMore on Web ser vices in MMIS 2(95/95)
Commenti su questo manuale