Professional Documents
Culture Documents
Tecnologas Web
Java y Servicios Web I
Master en Ingeniera Matemtica
Manuel Montenegro
Dpto. Sistemas Informticos y Computacin
Desp. 467 (Mat)
montenegro@fdi.ucm.es
Contenidos
Tecnologas web
HTML
CSS
Applets
Servicios web
Web scraping
Clientes y servidores
Protocolo
Puertos
Servidor
20
22
25
80
110
Puertos 1 1024
Reservados para el S.O. y para
protocolos conocidos.
Puertos 1024 49151
Pueden ser usados para cualquier
aplicacin.
Puertos 49152 65535
Dinmicos/privados.
Puertos
Servidor web
Cliente web
80
Servidor de
correo
Protocolo HTTP
Cliente de
correo
110
25
143
Protocolo POP3
Protocolo SMTP
Protocolo IMAP
Driver JDBC
Servidor
MySQL
3306
SQL
Contenidos
Tecnologas web
HTML
CSS
Applets
Servicios web
Web scraping
Servidor
Cliente
InputStream
OutputStream
OutputStream
InputStream
5555
Caso sencillo:
Servidor
Cliente
InputStream
OutputStream
7
5555
49
OutputStream
InputStream
La clase ServerSocket
Paquete java.net
Un objeto ServerSocket espera la conexin de
un cliente a travs de la red.
Constructores:
puerto
ServerSocket(int puerto)
ServerSocket(int puerto, int longCola)
Mtodos:
10
La clase Socket
Mtodos:
11
Ejemplo
public class ServidorCalculo implements Runnable {
public void run() {
ServerSocket server = null;
Socket s = null;
Scanner sc = null;
PrintWriter pw = null;
try {
server = new ServerSocket(5555);
System.out.println("Escuchando en el puerto 5555");
while (true) {
s = server.accept();
System.out.println("Recibida conexin desde " + s.getInetAddress());
sc = new Scanner(s.getInputStream());
int n = sc.nextInt();
System.out.println("He recibido un " + n);
pw = new PrintWriter(s.getOutputStream());
pw.print(n*n);
pw.flush();
pw.close();
sc.close();
}
} catch (IOException e) { e.printStackTrace(); }
finally {
if (s != null) try { s.close(); } catch (IOException e) {}
if (server != null) try {server.close();} catch (IOException e) {}
}
}
12
Ejemplo
public class ServidorCalculo implements Runnable {
13
Ejemplo
public class ClienteCalculo {
public static void main(String[] args) {
Socket s = null;
PrintWriter pw = null;
Scanner sc = null;
try {
s = new Socket("localhost", 5555);
pw = new PrintWriter(s.getOutputStream());
pw.println(7);
pw.flush();
sc = new Scanner(s.getInputStream());
int result = sc.nextInt();
System.out.println("He recibido un " + result);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
sc.close();
pw.close();
try {s.close();} catch (IOException e) {}
}
}
}
14
Ejemplo
15
ArrayList para
almacenar las
cadenas recibidas
16
17
Imprimir la
respuesta del
servidor
}
}
31 Mayo-1 Junio, 2012
18
Ejemplo
19
Y si...?
public class SingleClient {
public static void main(String[] args) throws Exception {
Socket s = new Socket("www.mat.ucm.es", 80);
...
}
}
Servidor web
Cliente web
80
Protocolo HTTP
20
Contenidos
Tecnologas web
HTML
CSS
Applets
Servicios web
Web scraping
21
Lenguaje HTML
22
23
24
Ttulo.
25
26
27
Estructurar contenido
28
Caracteres especiales
á
é
í
ó
ú
ñ
<
<
>
>
<body>
<p>Sencillo, no?</p>
<p>Esto es otro párrafo</p>
</body>
29
Encabezados
30
31
Hiperenlaces
<body>
<p>Haga click <a href=http://www.google.com>aquí</a>
para ir al buscador</p>
<p>Mediante <a href=Ej1.html>este enlace</a>
se salta al archivo Ej1.html contenido en el mismo
directorio que éste</p>
</body>
32
Imgenes
<img src=url imagen></img>
<img src=url imagen/>
33
<ol>
<li>Elemento</li>
<li>Otro elemento</li>
<li>Y otro más</li>
</ol>
34
Tablas
<table>
Fila
<tr>
<th>Nombre</th>
Celda de cabecera
<th>Apellidos</th>
<th>Fecha de Nacimiento</th>
</tr>
<tr>
<td>Ignacio</td>
Celda
<td>Domingo Villanueva</td>
<td>28-08-1970</td>
</tr>
<tr>
<td>Luis</td>
<td>Cid Gallego</td>
<td>20-10-1975</td>
</tr>
</table>
35
Contenidos
Tecnologas web
HTML
CSS
Applets
Servicios web
Web scraping
36
37
Ejemplo
Fuente: http://es.wikipedia.org/wiki/HTML
31 Mayo-1 Junio, 2012
38
Ejemplo
atributon: valorn;
}
Lista de atributos:
http://www.w3schools.com/css/default.asp
http://www.w3.org/Style/CSS/
39
Ejemplo
body {
font-family: sans-serif;
background-color: #E0FFE0;
}
h1 {
background-color: #0AFF0A;
text-align:right;
padding:8px;
color: #006000;
}
em {
font-style: normal;
color: #00A000;
}
p {
text-align:justify;
margin-left:20px;
margin-right:20px;
}
a {
color: #A0A000;
}
31 Mayo-1 Junio, 2012
Tipo de letra
Color de fondo verde claro
Color de fondo verde
Justificacin a la derecha
Mrgenes interiores
Color de texto
No utilizar cursiva
Color verde
Justificacin a mrgenes
Mrgenes de 20 pxeles
40
Ejemplo
41
</body>
</html>
42
Clases de elementos
atributon: valorn;
}
<etiqueta class=clase>
43
Clases de elementos
...
p.inicial {
border: 2px dashed green;
margin-left: 50px;
padding: 13px;
font-style: italic;
}
44
Contenidos
Tecnologas web
HTML
CSS
Applets
Servicios web
Web scraping
45
46
Tecnologas Web
Servidor web
Cliente
80
Protocolo HTTP
Javascript
Actionscript / Flash
Applets Java
PHP
ASP
Servlets / JSP
47
Applets de Java
48
Creacin de applets
public
public
public
public
void
void
void
void
init()
start()
stop()
destroy()
49
Ejemplo
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class AppletSaludo extends JApplet implements ActionListener {
public JTextField texto;
public void init() {
JPanel p = new JPanel();
texto = new JTextField(10);
p.setLayout(new FlowLayout());
p.add(new JLabel("Nombre:"));
p.add(texto);
JButton b = new JButton("Saludar");
b.addActionListener(this);
p.add(b);
Container content = getContentPane();
content.setLayout(new BorderLayout());
content.add(p);
}
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Hola " + texto.getText() + "!");
}
}
50
Ejemplo
51
Contenidos
Tecnologas web
HTML
CSS
Applets
Servicios web
Web scraping
52
Servicios web
53
Servicios web
interoperabilidad
Servidor web
Cliente
80
54
Lenguaje XML
Generalizacin/simplificacin de HTML.
55
Interfaz:
package fecha;
56
package fecha;
import javax.jws.*;
@WebService
public interface ServidorFecha {
@WebMethod Fecha getFechaHoy();
@WebMethod String getCadenaFecha();
}
Anotaciones
57
package fecha;
import java.util.*;
import javax.jws.*;
Nombre de
@WebService(endpointInterface = "fecha.ServidorFecha")
la interfaz
public class ServidorFechaImpl implements ServidorFecha {
public Fecha getFechaHoy() {
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(new Date());
return new Fecha(gc.get(GregorianCalendar.DAY_OF_MONTH),
gc.get(GregorianCalendar.MONTH) + 1,
gc.get(GregorianCalendar.YEAR));
}
public String getCadenaFecha() { return getFechaHoy().toString(); }
}
58
La utilidad wsgen
Nombre de la
clase que implementa
el servicio web (SIB)
59
La utilidad wsgen
60
BEA WebLogic
JBoss
GlassFish
WebSphere
61
62
63
WSDL
64
WSDL
65
66
67
68
Informacin en:
http://www.webservicex.net/ws/WSDetails.aspx?WSID=56&CATID=12
69
Acceso al WSDL
70
71
Formato XML
72
Intrpretes XML
73
74
Mtodo 2 - JAXP
NewDataSet
Table
Country
City
Vitoria
Table
Country
Table
City
Country
Vigo/Peinador
City
75
Mtodo 2 JAXP
import
import
import
import
tiempo.*;
javax.xml.parsers.*;
org.w3c.dom.*;
java.io.*;
76
Mtodo 2 - JAXP
documento.getElementsByTagName("City");
NewDataSet
Table
Country
City
Vitoria
Table
Country
Table
City
Country
Vigo/Peinador
City
77
Mtodo 2 - JAXP
elems.item(1)
NewDataSet
Table
Country
City
Vitoria
Table
Country
Table
City
Country
Vigo/Peinador
City
78
Mtodo 2 - JAXP
elems.item(1).firstChild()
NewDataSet
Table
Country
City
Vitoria
Table
Country
Table
City
Country
Vigo/Peinador
City
79
Mtodo 2 - JAXP
elems.item(1).firstChild().getNodeValue()
NewDataSet
Table
Country
City
Vitoria
Table
Country
Table
City
Country
Vigo/Peinador
City
Vigo/Peinador
31 Mayo-1 Junio, 2012
80
Contenidos
Tecnologas web
HTML
CSS
Applets
Servicios web
Web scraping
81
Web scraping
82
Ejemplo
83
Ejemplo
import java.net.*;
import java.util.*;
import java.util.regex.*;
public class LineasEMT {
public static void main(String[] args) throws Exception {
URL url = new URL("https://www.emtmadrid.es");
URLConnection con = url.openConnection();
Scanner sc = new Scanner(con.getInputStream());
Pattern p = Pattern.compile("<option value=\"[^\"]*\" linea=\"[^\"]*\" +
title=\"[^\"]*\">(.*)</option>");
while(sc.hasNextLine()) {
String linea = sc.nextLine();
Matcher m = p.matcher(linea);
if (m.find()) { System.out.println(m.group(1)); }
}
}
}
31 Mayo-1 Junio, 2012
84
Ejemplo
85
Referencias
P. Deitel, H. Deitel
Java. How to Program (9th Edition)
Cap. 23, 27
M. Kalin
Java web services: up and running
O'Reilly Media
http://www.w3schools.com/html/
http://www.w3schools.com/css/
86