5/10/10 Implementação Mestrado em Informática Universidade do Minho! 6! Building web Apps How to create dynamic contents?" Client side" Code runs on the client (browser)" Code runs on a virtual machine (in the browser)" JavaScript (client side), DHTML (Dynamic HTML), AJAX, VBScript" Java applets, Adobe Flex, Microsoft Silverlight, JavaFX" Server side" Code runs on the server" CGI vs. direct execution" Perl, ASP, PHP, Servlets, JavaScript (server side), JSP, JSF" Mestrado em Informática Universidade do Minho! 8! Server vs. client side Server side" access to information and functions available on the server. " require interpreter on the server, " largely independent from browser, or other client details. " Client side" access to information and functions available on the browser" require interpreter on the browser " no additional software on the server (popular with authors who lack administrative access to their servers);" problems with lack of standardization of browsers " Mestrado em Informática Universidade do Minho! 9! 1
Common Gateway Interface (CGI) Early form of server side scripting" Protocol for interfacing external applications with a web server" When request received, corresponding program called" The protocol defines " - how information about the server and the request is passed to the command (arguments and environment variables) " - how the command can pass back extra information about the output (headers)" Mestrado em Informática Universidade do Minho! 12! CGI programming all output from CGI program must be preceded by a MIME-type header" output needs to be in HTML" - or some other format that a browser will be able to display" #!/usr/bin/perl print "Content-type: text/html\n\n"; print "Olá, Mundo!"; Mestrado em Informática Universidade do Minho! 13! CGI programming all output from CGI program must be preceded by a MIME-type #!/usr/bin/perl header" output needs to be in HTML" use CGI ':standard'; - or some other format that a browser will be able to display" $q = new CGI; $nome = $q->param('nome'); print #!/usr/bin/perl header("text/html"); print "<html>"; "Content-type: text/html\n\n"; print "Olá, $nome!"; Mundo!"; print "</html>"; Mestrado em Informática Universidade do Minho! 14! 2
CGI? Language independent approach" - Any program that the server can run can be used" - Programs only need to output text" Low-tech approach" - Inefficient from time and memory perspective" - Can quickly overwhelm web servers" Mestrado em Informática Universidade do Minho! 15! Direct execution Applications run on web server instead of OS" - Apache modules (PHP, )" - Apache Tomcat (JSP)" - ASP.NET" - Internet Server Application Programming Interface (ISAPI)" Application servers" - Move back towards mainframe computing" - Server stores business logic and user interface" - Client side runs on a browser" Mestrado em Informática Universidade do Minho! 16! Servlets Tecnologia Java" Geração de HTML on the fly! Nova vista (página) gerada a cada pedido" - Vista é programada, não desenhada" - Não encorajam separação entre apresentação e negócio/conteúdo" Mestrado em Informática Universidade do Minho! 18! 3
JSP (Java Server Pages) Permite incluir código Java nas páginas HTML" Ficheiros.jsp compilam para Java" - ou directamente para bytecode" - on interpretados on the fly " Código Java compilado e executado para gerar HTML" Um salto de abstracção em relação às Servlets" Mestrado em Informática Universidade do Minho! 19! JSP: Expressões <%= expressão Java %>" Hello! The time is now <%= new java.util.date() %> Expressão avaliada em run time " Mestrado em Informática Universidade do Minho! 20! JSP: Scriptlets <% bloco de código Java %>" <% System.out.println( "Evaluating date now" ); java.util.date date = new java.util.date(); %> Hello! The time is now <%= date %> Por si só uma Scriptlet não produz HTML" - mas podemos programá-la para isso " Mestrado em Informática Universidade do Minho! 21! 4
JSP: Algumas variáveis out: javax.servlet.jsp.jspwriter" - Para escrever na página" Hello! Your address is: <% out.println( request.getremotehost());%> request: javax.servlet.http.httpservletrequest" - informação sobre o pedido enviado pelo browser" response: javax.servlet.http.httpservletresponse" - resposta a enviar ao browser" Hello! The time is now <% out.println( String.valueOf( date )); %> http://java.sun.com/products/servlet/2.2/javadoc/ Mestrado em Informática Universidade do Minho! 22! JSP: Misturar HTML e Java Melhor que utilizar out é misturar HTML e Java" <table border=2> <% for ( int i = 0; i < n; i++ ) { %> <tr> <td>number</td> <td><%= i+1 %></td> </tr> <%}%> </table> Mestrado em Informática Universidade do Minho! 23! JSP: Directivas <%@ tipo atributo%>" - tipo: page/include/taglib/ " Directiva page: informação sobre a servlet a gerar" - import/contenttype/pageencoding/session/errorpage/ " <%@ page import="java.util.*" %> Hello! The time is now <%= new Date() %> Directiva include: " <%@ include file="relativeurl" %> Mestrado em Informática Universidade do Minho! 24! 5
JSP: Declarações <%! declarações Java %>" <%@ page import="java.util.*" %> <%! Date thedate = new Date(); Date getdate() { System.out.println( "In getdate() method" ); return thedate; } %> Hello! The time is now <%= getdate() %> Mas agora não funciona! (class level scope)" Mestrado em Informática Universidade do Minho! 25! JSP: tags tags pré-definidas" - <jsp:tag> </jsp:tag>" - text/include/forward/usebean/getproperty/ setproperty/plugin" tags de bibliotecas externas (taglibs)" - Directiva taglib" <%@ taglib uri="uriforlibrary" prefix="tagprefix" %> Mestrado em Informática Universidade do Minho! 26! JSP: Sessões Variável: session" - um(a espécie de) Map " - permite guardar informação de página para página" session.setattribute("thename", name); session.getattribute("thename ); Mestrado em Informática Universidade do Minho! 27! 6
Links Úteis http://java.sun.com/products/jsp/" http://java.sun.com/products/jsp/syntax/2.0/card20.pdf" http://www.jsptut.com/" Mestrado em Informática Universidade do Minho! 30! 7