5/10/10
Engenharia de Aplicações – Sistemas Interactivos 2009/10!
Implementação
Mestrado em Informática – Universidade do Minho!
6!
Engenharia de Aplicações – Sistemas Interactivos 2009/10!
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!
Engenharia de Aplicações – Sistemas Interactivos 2009/10!
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
5/10/10
Engenharia de Aplicações – Sistemas Interactivos 2009/10!
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!
Engenharia de Aplicações – Sistemas Interactivos 2009/10!
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!
Engenharia de Aplicações – Sistemas Interactivos 2009/10!
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
$q = new CGI;
display"
$nome = $q->param('nome'); #!/usr/bin/perl
print
header("text/html");
print"<html>";
"Content-type: text/html\n\n";
print
print"Olá,
"Olá,$nome!";
Mundo!";
print
print "</html>";
Mestrado em Informática – Universidade do Minho!
14!
2
5/10/10
Engenharia de Aplicações – Sistemas Interactivos 2009/10!
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!
Engenharia de Aplicações – Sistemas Interactivos 2009/10!
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!
Engenharia de Aplicações – Sistemas Interactivos 2009/10!
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
5/10/10
Engenharia de Aplicações – Sistemas Interactivos 2009/10!
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!
Engenharia de Aplicações – Sistemas Interactivos 2009/10!
JSP: Expressões
<%= expressão Java %>"
<HTML>
<BODY>
Hello! The time is now <%= new java.util.Date() %>
</BODY>
</HTML>
Expressão avaliada em run time "
Mestrado em Informática – Universidade do Minho!
20!
Engenharia de Aplicações – Sistemas Interactivos 2009/10!
JSP: Scriptlets
<% bloco de código Java %>"
<HTML>
<BODY>
<% System.out.println( "Evaluating date now" );
java.util.Date date = new java.util.Date();
%>
Hello! The time is now <%= date %>
</BODY>
</HTML>
Por si só uma Scriptlet não produz HTML"
-
mas podemos programá-la para isso…"
Mestrado em Informática – Universidade do Minho!
21!
4
5/10/10
Engenharia de Aplicações – Sistemas Interactivos 2009/10!
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 )); %>
Mestrado em Informática – Universidade do Minho!
http://java.sun.com/products/servlet/2.2/javadoc/
22!
Engenharia de Aplicações – Sistemas Interactivos 2009/10!
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!
Engenharia de Aplicações – Sistemas Interactivos 2009/10!
JSP: Directivas
<%@ tipo atributo%>"
Directiva page: informação sobre a servlet a gerar"
-
-
tipo: page/include/taglib/…"
import/contentType/pageEncoding/session/errorPage/…"
<%@ page import="java.util.*" %>
<HTML>
<BODY>
Hello! The time is now <%= new Date() %>
</BODY>
</HTML>
Directiva include: "
<%@ include file="relativeURL" %>
Mestrado em Informática – Universidade do Minho!
24!
5
5/10/10
Engenharia de Aplicações – Sistemas Interactivos 2009/10!
JSP: Declarações
<%! declarações Java %>"
<%@ 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>
Mas agora não funciona! (class level scope)"
Mestrado em Informática – Universidade do Minho!
25!
Engenharia de Aplicações – Sistemas Interactivos 2009/10!
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!
Engenharia de Aplicações – Sistemas Interactivos 2009/10!
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
5/10/10
Engenharia de Aplicações – Sistemas Interactivos 2009/10!
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