Han visitado este sitio
"); for (int i = 0; i < nIP; i++) out.println("
"+ IPs[i]); out.close(); }"> Han visitado este sitio
"); for (int i = 0; i < nIP; i++) out.println("
"+ IPs[i]); out.close(); }">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ejemplo 4 Modificar el servlet contador de modo de generar una respuesta que muestre la IP de todos los computadores que lo han contactado hasta ahora.

Similar presentations


Presentation on theme: "Ejemplo 4 Modificar el servlet contador de modo de generar una respuesta que muestre la IP de todos los computadores que lo han contactado hasta ahora."— Presentation transcript:

1 Ejemplo 4 Modificar el servlet contador de modo de generar una respuesta que muestre la IP de todos los computadores que lo han contactado hasta ahora Use un vector o un arreglo para ir guardándolas

2 (se reinicializa vacío cada vez que parte el servidor)
Usandor un arreglo (se reinicializa vacío cada vez que parte el servidor) import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class AddressesServlet extends HttpServlet { int nIP = 0; String[] IPs = new String[1000]; public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String ip = request.getRemoteHost(); IPs[nIP++] = ip; response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<h1> Han visitado este sitio </h1><br>"); for (int i = 0; i < nIP; i++) out.println("<br> "+ IPs[i]); out.close(); }

3 Optener Ip del cliente y guardarla en el arreglo
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class AddressesServlet extends HttpServlet { int nIP = 0; String[] IPs = new String[1000]; public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String ip = request.getRemoteHost(); IPs[nIP++] = ip; response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<h1> Han visitado este sitio </h1><br>"); for (int i = 0; i < nIP; i++) out.println("<br> "+ IPs[i]); out.close(); } Optener Ip del cliente y guardarla en el arreglo

4 Imprimir cada String separado por una línea horizontal
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class AddressesServlet extends HttpServlet { int nIP = 0; String[] IPs = new String[1000]; public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String ip = request.getRemoteHost(); IPs[nIP++] = ip; response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<h1> Han visitado este sitio </h1><br>"); for (int i = 0; i < nIP; i++) out.println("<hr> "+ IPs[i]); out.close(); } Imprimir cada String separado por una línea horizontal

5 Solución con vector que contiene las IP
Import util package import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; public class AddressesServlet extends HttpServlet { Vector IPs = new Vector(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String ip = request.getRemoteHost(); IPs.add(ip); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<h1> Han visitado este sitio </h1><br>"); for (int i = 0; i < IPs.size(); i++) out.println("<hr> "+ IPs.elementAt(i)); out.close(); } Create empty vector Get IP and add it to the end Print every string to the browser separated with a horizontal bar

6 Usando un Archivo import java.io.*; import javax.servlet.*;
import javax.servlet.http.*; public class AddressesServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String ip = request.getRemoteHost(); PrintWriter file = new PrintWriter( new FileWriter(“ips.txt”,true)); file.println(ip); file.close(); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<h1> Han visitado este sitio </h1><br>"); BufferedReade infile = new BufferedReader( new FileReader(“ips.txt”)); for (int i = 0; i < nIP; i++){ String linea = infile.readLine(); out.println("<br> "+ IPs[i]); } out.close();

7 Otro ejemplo Modificar el ejemplo anterior para que en vez de repetir la ip muestre cuantas veces ha visitado ese cliente al servlet Solución 1: use un vector que contenga objetos de la clase Node, el cual contiene un String (para guardar el número IP) y un entero (para contar las visitas del cliente) public class Node { int count; String ip; Node(String x, int y) { ip = x; count = y; }

8 public class ShowCountsServlet extends HttpServlet {
Vector v = new Vector(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String ip = request.getRemoteHost(); Node aux; int p =0; while(p < v.size()) { aux = (Node)v.elementAt(p); if (ip.equals(aux.ip)) break; p++; } if (p == v.size()) { aux = new Node(ip,0); v.add(aux); out.println("agregado"); aux.count++; out.println("<h1> Following clients visited this site </h1><br>"); for (int i = 0; i < v.size(); i++) { aux = (Node)v.elementAt(i); out.print("<hr> "+ aux.ip+" has been "+aux.count+" times here"); out.close();

9 Vector containing the “nodes”
public class ShowCountsServlet extends HttpServlet { Vector v = new Vector(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String ip = request.getRemoteHost(); Node aux; int p =0; while(p < v.size()) { aux = (Node)v.elementAt(p); if (ip.equals(aux.ip)) break; p++; } if (p == v.size()) { aux = new Node(ip,0); v.add(aux); out.println("agregado"); aux.count++; out.println("<h1> Following clients visited this site </h1><br>"); for (int i = 0; i < v.size(); i++) { aux = (Node)v.elementAt(i); out.print("<hr> "+ aux.ip+" has been "+aux.count+" times here"); out.close(); Vector containing the “nodes”

10 initialize response header get writer to browser get IP of client
public class ShowCountsServlet extends HttpServlet { Vector v = new Vector(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String ip = request.getRemoteHost(); Node aux; int p =0; while(p < v.size()) { aux = (Node)v.elementAt(p); if (ip.equals(aux.ip)) break; p++; } if (p == v.size()) { aux = new Node(ip,0); v.add(aux); out.println("agregado"); aux.count++; out.println("<h1> Following clients visited this site </h1><br>"); for (int i = 0; i < v.size(); i++) { aux = (Node)v.elementAt(i); out.print("<hr> "+ aux.ip+" has been "+aux.count+" times here"); out.close(); initialize response header get writer to browser get IP of client

11 Search for a Node in the vector containing a string with the ip
public class ShowCountsServlet extends HttpServlet { Vector v = new Vector(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String ip = request.getRemoteHost(); Node aux; int p =0; while(p < v.size()) { aux = (Node)v.elementAt(p); if (ip.equals(aux.ip)) break; p++; } if (p == v.size()) { aux = new Node(ip,0); v.add(aux); out.println("agregado"); aux.count++; out.println("<h1> Following clients visited this site </h1><br>"); for (int i = 0; i < v.size(); i++) { aux = (Node)v.elementAt(i); out.print("<hr> "+ aux.ip+" has been "+aux.count+" times here"); out.close(); Search for a Node in the vector containing a string with the ip of the client

12 If the ip was not there, create a new Node with that ip and a
public class ShowCountsServlet extends HttpServlet { Vector v = new Vector(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String ip = request.getRemoteHost(); Node aux; int p =0; while(p < v.size()) { aux = (Node)v.elementAt(p); if (ip.equals(aux.ip)) break; p++; } if (p == v.size()) { aux = new Node(ip,0); v.add(aux); out.println("agregado"); aux.count++; out.println("<h1> Following clients visited this site </h1><br>"); for (int i = 0; i < v.size(); i++) { aux = (Node)v.elementAt(i); out.print("<hr> "+ aux.ip+" has been "+aux.count+" times here"); out.close(); If the ip was not there, create a new Node with that ip and a count = 0

13 In any case, the Node containing the ip
public class ShowCountsServlet extends HttpServlet { Vector v = new Vector(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String ip = request.getRemoteHost(); Node aux; int p =0; while(p < v.size()) { aux = (Node)v.elementAt(p); if (ip.equals(aux.ip)) break; p++; } if (p == v.size()) { aux = new Node(ip,0); v.add(aux); out.println("agregado"); aux.count++; out.println("<h1> Following clients visited this site </h1><br>"); for (int i = 0; i < v.size(); i++) { aux = (Node)v.elementAt(i); out.print("<hr> "+ aux.ip+" has been "+aux.count+" times here"); out.close(); In any case, the Node containing the ip of the client will be at position p. We use it for retrieving the Node and incrementing the count variable.

14 Fina Finally, retrieve each elemento of the
public class ShowCountsServlet extends HttpServlet { Vector v = new Vector(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String ip = request.getRemoteHost(); Node aux; int p =0; while(p < v.size()) { aux = (Node)v.elementAt(p); if (ip.equals(aux.ip)) break; p++; } if (p == v.size()) { aux = new Node(ip,0); v.add(aux); out.println("agregado"); aux.count++; out.println("<h1> Following clients visited this site </h1><br>"); for (int i = 0; i < v.size(); i++) { aux = (Node)v.elementAt(i); out.print("<hr> "+ aux.ip+" has been "+aux.count+" times here"); out.close(); Finally, retrieve each elemento of the Vector printing the ip and count variables Fina

15 Exercise Modificar el ShowCountsServlet para que muestre el que lo ha visitado más

16 Solución Casi todo el programa se mantiene igual Solo cambia el output
public class ShowCountsServlet extends HttpServlet { Vector v = new Vector(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { …. Node nmax = (Node)v.elementAt(0); for (int i = 1; i < v.size(); i++) { Node aux = (Node)v.elementAt(i); if (aux.count > nmax.count) nmax = aux; } out.print(“ the client with highest number of visits is “+nmax.ip out.println(“and has been "+nmax.count+" times here"); out.close(); Casi todo el programa se mantiene igual Solo cambia el output En vez de imprimir todos, buscamos el mayor … y lo imprimimos

17 Create a new empty hastable
public class HashExampleServlet extends HttpServlet { Hashtable h = new Hashtable(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String ip = request.getRemoteHost(); Integer i = (Integer)h.get(ip); if (i == null) { i = new Integer(0); h.put(ip,i); } int ival = i.intValue(); h.put(ip,new Integer(ival+1)); //replaces the older pair out.println("<h1> Han visitado este sitio </h1><br>"); Enumeration e = h.keys(); while (e.hasMoreElements()) { ip = (String)e.nextElement(); i = (Integer)h.get(ip); ival = i.intValue(); out.print("<hr> "+ ip+" has been "+ival+" times here"); out.close(); Create a new empty hastable

18 Get client’s IP and use it to retrieve the associated
public class HashExampleServlet extends HttpServlet { Hashtable h = new Hashtable(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String ip = request.getRemoteHost(); Integer i = (Integer)h.get(ip); if (i == null) { i = new Integer(0); h.put(ip,i); } int ival = i.intValue(); h.put(ip,new Integer(ival+1)); //replaces the older pair out.println("<h1> Han visitado este sitio </h1><br>"); Enumeration e = h.keys(); while (e.hasMoreElements()) { ip = (String)e.nextElement(); i = (Integer)h.get(ip); ival = i.intValue(); out.print("<hr> "+ ip+" has been "+ival+" times here"); out.close(); Get client’s IP and use it to retrieve the associated information (count)

19 If there was no pair (ip, count) having this
public class HashExampleServlet extends HttpServlet { Hashtable h = new Hashtable(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String ip = request.getRemoteHost(); Integer i = (Integer)h.get(ip); if (i == null) { i = new Integer(0); h.put(ip,i); } int ival = i.intValue(); h.put(ip,new Integer(ival+1)); //replaces the older pair out.println("<h1> Han visitado este sitio </h1><br>"); Enumeration e = h.keys(); while (e.hasMoreElements()) { ip = (String)e.nextElement(); i = (Integer)h.get(ip); ival = i.intValue(); out.print("<hr> "+ ip+" has been "+ival+" times here"); out.close(); If there was no pair (ip, count) having this IP has key then we put a new one

20 At this point, i will contain the
public class HashExampleServlet extends HttpServlet { Hashtable h = new Hashtable(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String ip = request.getRemoteHost(); Integer i = (Integer)h.get(ip); if (i == null) { i = new Integer(0); h.put(ip,i); } int ival = i.intValue(); h.put(ip,new Integer(ival+1)); //replaces the older pair out.println("<h1> Han visitado este sitio </h1><br>"); Enumeration e = h.keys(); while (e.hasMoreElements()) { ip = (String)e.nextElement(); i = (Integer)h.get(ip); ival = i.intValue(); out.print("<hr> "+ ip+" has been "+ival+" times here"); out.close(); At this point, i will contain the Integer containing the count associated to this ip. We get the int value We use it for incrementing the count value and replacing the older pair whith this new one

21 Get Enumeration object containing keys
public class HashExampleServlet extends HttpServlet { Hashtable h = new Hashtable(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String ip = request.getRemoteHost(); Integer i = (Integer)h.get(ip); if (i == null) { i = new Integer(0); h.put(ip,i); } int ival = i.intValue(); h.put(ip,new Integer(ival+1)); //replaces the older pair out.println("<h1> Han visitado este sitio </h1><br>"); Enumeration e = h.keys(); while (e.hasMoreElements()) { ip = (String)e.nextElement(); i = (Integer)h.get(ip); ival = i.intValue(); out.print("<hr> "+ ip+" has been "+ival+" times here"); out.close(); Get Enumeration object containing keys As long as there are elements Get the next key element (IP) Get the associates info Get int value in order to print it

22 Get Enumeration object containing keys
public class HashExampleServlet extends HttpServlet { Hashtable h = new Hashtable(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String ip = request.getRemoteHost(); Integer i = (Integer)h.get(ip); if (i == null) { i = new Integer(0); h.put(ip,i); } int ival = i.intValue(); h.put(ip,new Integer(ival+1)); //replaces the older pair out.println("<h1> Han visitado este sitio </h1><br>"); Enumeration e = h.keys(); while (e.hasMoreElements()) { ip = (String)e.nextElement(); i = (Integer)h.get(ip); ival = i.intValue(); out.print("<hr> "+ ip+" has been "+ival+" times here"); out.close(); Get Enumeration object containing keys As long as there are elements Get the next key element (IP) Get the associates info Get int value in order to print it


Download ppt "Ejemplo 4 Modificar el servlet contador de modo de generar una respuesta que muestre la IP de todos los computadores que lo han contactado hasta ahora."

Similar presentations


Ads by Google