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.

Proposed 1 Modify the count servlet in order to generate an answer showing the IP addresses of ALL computer that have accessed it Use a vector or.

Similar presentations


Presentation on theme: "Proposed 1 Modify the count servlet in order to generate an answer showing the IP addresses of ALL computer that have accessed it Use a vector or."— Presentation transcript:

1 Proposed 1 Modify the count servlet in order to generate an answer showing the IP addresses of ALL computer that have accessed it Use a vector or an array to store the Addresses of the computers

2 Using an Array to Keep all IPs
Starts empty every time the servlet is deployed 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 Get IP of client and store it in the array
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(); } Get IP of client and store it in the array

4 Print every string to the browser separated with a horizontal bar
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(); } Print every string to the browser separated with a horizontal bar

5 The Vector class in Java
With an array we are limited to have a fixed number of elements If the number used to index an array exceeds the number of declares positions the program crashes and throws an ArrayIndexOutOfBoundsException A vector contains objects of any kind but it has a viarable number of elements New elements can be inserted, appended Existing elements can be deleted Most important methods of the class are Vector v = new Vector() //creates a new (empty) Vector v.add(“hola”) //adds the string “hola” at the end v.add( i, “hola) //adds “hola” at the i-th place String s = v.elementAt(i) //retrieves the element at the i-th place //(starting from 0) v.remove(i) //removes the element in the i-th place

6 Get IP and add it to the end
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

7 Using a File 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();

8 Proposed 2 Modify the count servlet in order to generate an answer showing all the IPs of the clients and number of times that computer has contacted the servlet Solution 1: use a vector containing object from a class which has a String (for storing the IP number) and an integer (for storing the number of times the client was here) public class Node { int count; String ip; Node(String x, int y) { ip = x; count = y; }

9 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(“added"); 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();

10 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”

11 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

12 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

13 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

14 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.

15 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

16 Exercise Modify the ShowCountsServlet in order to show only the IP and Counts from the client which has contacted the servlet most

17 Solution Most of the program remains the same. Only the output changes
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(); Most of the program remains the same. Only the output changes Instead of printing the content of all elemnts, we search for that one having the highest count … … and we print it

18 Lets do the same but with a Dictionary (Hashtable)
A hashtable is like a dictionary: it has an object serving as an index and another with the information associated. Examples: Word - explanation, RUT - name, IP - count As the vector, it does not have a fixed number of elements Most important methods of the class are Hashtable h = new Hashtable() creates a new (empty) dictionary h.put(“hola”, “hello”) adds the pair (“hola”,”hello”)being “hola” the key and “hello” the info associated String s = (String)h.get(“hola”) retrieves the information associated to the key “hola” (“hello” in this case) or null if it does not exits Enumeration e = h.keys(); Retrieves all the keys contained in the hashtable in an object of the class Enumeration. This object can be used to retrieve each key one-by-one h.remove(“hola”) removes the pair (key, info) which has “hola” as a key

19 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

20 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)

21 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

22 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

23 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

24 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 "Proposed 1 Modify the count servlet in order to generate an answer showing the IP addresses of ALL computer that have accessed it Use a vector or."

Similar presentations


Ads by Google