proposed 1 modify the count servlet in order to generate an answer showing the ip addresses of all...

24
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

Post on 21-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 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

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

Page 2: 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

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(); }}

Using an Array to Keep all IPsStarts empty every time the servlet is deployed

Page 3: 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

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

Page 4: 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

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

Page 5: 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

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 = (String)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

– int i = v.size() //number of elements the vector has

Page 6: 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

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(); }}

Print every string to the browser separated with a horizontal bar

Get IP and add it to the end

Create empty vector

Import util package

Page 7: 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

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(); }}

Using a File

Page 8: 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

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; }

}

Page 9: 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

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 = (Node)v.elementAt(p); 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(); }}

Page 10: 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

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 = (Node)v.elementAt(p); 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”

Page 11: 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

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 = (Node)v.elementAt(p); 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

Page 12: 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

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 = (Node)v.elementAt(p); 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

Page 13: 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

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 = (Node)v.elementAt(p); 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

Page 14: 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

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 = (Node)v.elementAt(p); 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 ipof the client will be at position p.We use it for retrieving the Node and incrementing the count variable.

Page 15: 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

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 = (Node)v.elementAt(p); 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(); }}

Fina

Finally, retrieve each elemento of the Vector printing the ip and count variables

Page 16: 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

Exercise

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

Page 17: 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

Solutionpublic 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

Page 18: 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

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

Page 19: 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

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

Page 20: 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

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)

Page 21: 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

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

Page 22: 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

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

Page 23: 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

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 associates info

Get the next key element (IP)

Get int value in order to print it

Page 24: 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

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 associates info

Get the next key element (IP)

Get int value in order to print it