EA-SI-0910p4jfc/ensino/EA-SI/0910-Aula07.pdf · 5/10/10 3 Engenharia de Aplicações –...
Transcript of EA-SI-0910p4jfc/ensino/EA-SI/0910-Aula07.pdf · 5/10/10 3 Engenharia de Aplicações –...
5/10/10
1
Engenharia de Aplicações – Sistemas Interactivos 2009/10!
Mestrado em Informática – Universidade do Minho! 6!
Implementação
Engenharia de Aplicações – Sistemas Interactivos 2009/10!
Mestrado em Informática – Universidade do Minho! 8!
Building web Apps
How to create dynamic contents?" Client side"
- Code runs on the client (browser)" JavaScript (client side), DHTML (Dynamic HTML), AJAX, VBScript"
- Code runs on a “virtual machine” (in the browser)" Java applets, Adobe Flex, Microsoft Silverlight, JavaFX"
Server side"- Code runs on the server"
Perl, ASP, PHP, Servlets, JavaScript (server side), JSP, JSF"- CGI vs. direct execution"
Engenharia de Aplicações – Sistemas Interactivos 2009/10!
Mestrado em Informática – Universidade do Minho! 9!
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 "
5/10/10
2
Engenharia de Aplicações – Sistemas Interactivos 2009/10!
Mestrado em Informática – Universidade do Minho! 12!
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)"
Engenharia de Aplicações – Sistemas Interactivos 2009/10!
Mestrado em Informática – Universidade do Minho! 13!
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!";
Engenharia de Aplicações – Sistemas Interactivos 2009/10!
Mestrado em Informática – Universidade do Minho! 14!
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!";
#!/usr/bin/perl
use CGI ':standard';
$q = new CGI; $nome = $q->param('nome');
print header("text/html");
print "<html>"; print "Olá, $nome!"; print "</html>";
5/10/10
3
Engenharia de Aplicações – Sistemas Interactivos 2009/10!
Mestrado em Informática – Universidade do Minho! 15!
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"
Engenharia de Aplicações – Sistemas Interactivos 2009/10!
Mestrado em Informática – Universidade do Minho! 16!
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"
Engenharia de Aplicações – Sistemas Interactivos 2009/10!
Mestrado em Informática – Universidade do Minho! 18!
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"
5/10/10
4
Engenharia de Aplicações – Sistemas Interactivos 2009/10!
Mestrado em Informática – Universidade do Minho! 19!
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"
Engenharia de Aplicações – Sistemas Interactivos 2009/10!
Mestrado em Informática – Universidade do Minho! 20!
JSP: Expressões
<%= expressão Java %>"
Expressão avaliada em run time "
<HTML> <BODY> Hello! The time is now <%= new java.util.Date() %> </BODY>
</HTML>
Engenharia de Aplicações – Sistemas Interactivos 2009/10!
Mestrado em Informática – Universidade do Minho! 21!
JSP: Scriptlets
<% bloco de código Java %>"
Por si só uma Scriptlet não produz HTML"- mas podemos programá-la para isso…"
<HTML> <BODY> <% System.out.println( "Evaluating date now" );
java.util.Date date = new java.util.Date(); %> Hello! The time is now <%= date %> </BODY>
</HTML>
5/10/10
5
Engenharia de Aplicações – Sistemas Interactivos 2009/10!
Mestrado em Informática – Universidade do Minho! 22!
JSP: Algumas variáveis
out: javax.servlet.jsp.JspWriter"- Para “escrever” na página"
request: javax.servlet.http.HttpServletRequest"- informação sobre o pedido enviado pelo browser"
response: javax.servlet.http.HttpServletResponse"- resposta a enviar ao browser"
Hello! Your address is: <% out.println( request.getRemoteHost());%>
http://java.sun.com/products/servlet/2.2/javadoc/
Hello! The time is now <% out.println( String.valueOf( date )); %>
Engenharia de Aplicações – Sistemas Interactivos 2009/10!
Mestrado em Informática – Universidade do Minho! 23!
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>
Engenharia de Aplicações – Sistemas Interactivos 2009/10!
Mestrado em Informática – Universidade do Minho! 24!
JSP: Directivas
<%@ tipo atributo%>"- tipo: page/include/taglib/…"
Directiva page: informação sobre a servlet a gerar"- import/contentType/pageEncoding/session/errorPage/…"
Directiva include: "
<%@ page import="java.util.*" %> <HTML>
<BODY> Hello! The time is now <%= new Date() %> </BODY>
</HTML>
<%@ include file="relativeURL" %>
5/10/10
6
Engenharia de Aplicações – Sistemas Interactivos 2009/10!
Mestrado em Informática – Universidade do Minho! 25!
JSP: Declarações
<%! declarações Java %>"
Mas agora não funciona! (class level scope)"
<%@ page import="java.util.*" %> <HTML>
<BODY> <%!
Date theDate = new Date(); Date getDate() { System.out.println( "In getDate() method" ); return theDate; }
%> Hello! The time is now <%= getDate() %> </BODY>
</HTML>
Engenharia de Aplicações – Sistemas Interactivos 2009/10!
Mestrado em Informática – Universidade do Minho! 26!
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" %>
Engenharia de Aplicações – Sistemas Interactivos 2009/10!
Mestrado em Informática – Universidade do Minho! 27!
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”);
5/10/10
7
Engenharia de Aplicações – Sistemas Interactivos 2009/10!
Mestrado em Informática – Universidade do Minho! 30!
Links Úteis
http://java.sun.com/products/jsp/" http://java.sun.com/products/jsp/syntax/2.0/card20.pdf" http://www.jsptut.com/"