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.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Introduction to Servlets Based on: Hall, Brown, Core Servlets and JavaServer Pages.
J.Sant Servlets Joseph Sant Sheridan Institute of Technology.
Servlets. A form The HTML source Chapter 1 Please enter your name and password then press start Name: Password: In Netbeans you can graphically create.
Objectives Ch. D - 1 At the end of this chapter students will: Know the general architecture and purpose of servlets Understand how to create a basic servlet.
Servlets Stoney Jackson
An introduction to Java Servlet Programming
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
JSP Java Server Pages Reference:
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 10 Object Oriented Programming in Java Advanced Topics Servlets.
Servlets. A form The HTML source Chapter 1 Please enter your name and password then press start Name: Password: In Netbeans you can graphically create.
2/16/2004 Dynamic Content February 16, /16/2004 Assignments Due – Message of the Day Part 1 Due – Reading and Warmup Work on Message of the Day.
1 CS6320 – Servlet Structure and Lifecycle L. Grewe.
Servlets Replace Common Gateway Interface Scripts Extend Server Functionality Modules (software components) Like applets to browsers No GUI.
Introduction to Servlet & JSP
Servlet Session Tracking II Session API All material and examples are from
Servlets Compiled by Dr. Billy B. L. Lim. Servlets Servlets are Java programs which are invoked to service client requests on a Web server. Servlets extend.
Servlets. Our Project 3-tier application Develop our own multi-threaded server Socket level communication.
A Servlet’s Job Read explicit data sent by client (form data) Read implicit data sent by client (request headers) Generate the results Send the explicit.
CSCI 6962: Server-side Design and Programming History and Background.
Java Servlets and JSP.
Java Servlets. What Are Servlets? Basically, a java program that runs on the server Basically, a java program that runs on the server Creates dynamic.
Servlet and JSP Programming: An Introduction Spiros Papadimitriou
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
Servlets, Sessions, and Cookies Lecture 8 cs193i – Internet Technologies Summer 2004 Kelly Shaw, Stanford University.
Servlets. - Java technology for Common Gateway Interface (CGI) programming. - It is a Java class that dynamically extends the function of a web server.
CSC 2720 Building Web Applications
Java Servlets and Java Server Pages Carol Wolf Computer Science.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
SKT-SSU IT Training Center Servlet and JSP. Chapter Three: Servlet Basics.
CMPUT 391 – Database Management Systems Department of Computing Science University of Alberta CMPUT 391 Database Management Systems Web based Applications,
Servlet Lifecycle Lec 28. Servlet Life Cycle  Initialize  Service  Destroy Time.
Chapter 3 Servlet Basics. 1.Recall the Servlet Role 2.Basic Servlet Structure 3.A simple servlet that generates plain text 4.A servlet that generates.
Servlets Database Access. Agenda:  Setup Java Environment  Install Database  Install Database Drivers  Create Table and add records  Accessing a.
Java Servlets & Java Server Pages Lecture July 2013.
Java Servlets Lec 27. Creating a Simple Web Application in Tomcat.
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
Chapter 2 Web app architecture. High-level web app architecture  When a client request coming in and needs servlet to serve dynamic web content, what.
Saving Client State Session Tracking: Maintain state about series of requests from same client over time Using Cookies: Clients hold small amount of their.
20-Nov-15introServlets.ppt Intro to servlets. 20-Nov-15introServlets.ppt typical web page – source Hello Hello.
S ERVLETS Hits Counter 21-Nov-15. S ERVLETS - H ITS C OUNTER Many times you would be interested in knowing total number of hits on a particular page of.
Java Servlet API CGI / HTTP Concepts Java Servlet API.
JSP Pages. What and Why of JSP? JSP = Java code imbedded in HTML or XML –Static portion of the page is HTML –Dynamic portion is Java Easy way to develop.
CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, Responds oriented other.
1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.
CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles.
Configuration Web Server Tomcat - Install JDK Install Tom cat Configure Tom cat for running Servlet C:\Program Files\Apache Software Foundation\Tomcat.
©SoftMoore ConsultingSlide 1 Overview of Servlets and JavaServer Pages (JSP)
Java and the Web CSE 3330 Southern Methodist University.
Java Servlets and Java Server Pages
HTTP protocol Java Servlets. HTTP protocol Web system communicates with end-user via HTTP protocol HTTP protocol methods: GET, POST, HEAD, PUT, OPTIONS,
Introduction To HTML Dr. Magdi AMER. HTML elements.
ClaRA web services V. Gyurjyan Clas12 Software Meeting
How CGI and Java Servlets are Run By David Stein 14 November 2006.
J2EE/Java EE Apache Tomcat v9 IBM Websphere v8 Redhat JBoss EAP v6 Oracle Weblogic v12 Fusion Web Tier ServletsJSPJSF Business Tier JMXJMSEJB.
S ERVLETS Form Data 19-Mar-16. F ORM P ROCESSING You must have come across many situations when you need to pass some information from your browser to.
Servlet Fudamentals.
Servlets Hits Counter 20-Jul-18.
HTTP Servlet Overview Servlets are modules that extend request/response-oriented servers, such as Java-enabled web servers. For example, a servlet might.
HTTP request message two types of HTTP messages: request, response
Servlet.
Jagdish Gangolly State University of New York at Albany
Servlets and Java Server Pages
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
Java Servlets Servlet Overview Servlets and HTML Forms Servlet Basics
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
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.
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.
Basic servlet structure
Presentation transcript:

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

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(" Han visitado este sitio "); for (int i = 0; i < nIP; i++) out.println(" "+ IPs[i]); out.close(); } 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(" Han visitado este sitio "); for (int i = 0; i < nIP; i++) out.println(" "+ IPs[i]); out.close(); } 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(" Han visitado este sitio "); for (int i = 0; i < nIP; i++) out.println(" "+ IPs[i]); out.close(); } Print every string to the browser separated with a horizontal bar

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

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(" Han visitado este sitio "); for (int i = 0; i < IPs.size(); i++) out.println(" "+ 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

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(" Han visitado este sitio "); BufferedReade infile = new BufferedReader( new FileReader(“ips.txt”)); for (int i = 0; i < nIP; i++){ String linea = infile.readLine(); out.println(" "+ IPs[i]); } out.close(); } Using a File

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

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(" Following clients visited this site "); for (int i = 0; i < v.size(); i++) { aux = (Node)v.elementAt(i); out.print(" "+ aux.ip+" has been "+aux.count+" times here"); } out.close(); }

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(" Following clients visited this site "); for (int i = 0; i < v.size(); i++) { aux = (Node)v.elementAt(i); out.print(" "+ aux.ip+" has been "+aux.count+" times here"); } out.close(); } 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 = (Node)v.elementAt(p); aux.count++; out.println(" Following clients visited this site "); for (int i = 0; i < v.size(); i++) { aux = (Node)v.elementAt(i); out.print(" "+ aux.ip+" has been "+aux.count+" times here"); } out.close(); } - 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 = (Node)v.elementAt(p); aux.count++; out.println(" Following clients visited this site "); for (int i = 0; i < v.size(); i++) { aux = (Node)v.elementAt(i); out.print(" "+ 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

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(" Following clients visited this site "); for (int i = 0; i < v.size(); i++) { aux = (Node)v.elementAt(i); out.print(" "+ 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

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(" Following clients visited this site "); for (int i = 0; i < v.size(); i++) { aux = (Node)v.elementAt(i); out.print(" "+ 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.

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(" Following clients visited this site "); for (int i = 0; i < v.size(); i++) { aux = (Node)v.elementAt(i); out.print(" "+ aux.ip+" has been "+aux.count+" times here"); } out.close(); } Fina Finally, retrieve each elemento of the Vector printing the ip and count variables

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

Solution 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

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

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(" Han visitado este sitio "); Enumeration e = h.keys(); while (e.hasMoreElements()) { ip = (String)e.nextElement(); i = (Integer)h.get(ip); ival = i.intValue(); out.print(" "+ ip+" has been "+ival+" times here"); } out.close(); } 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(" Han visitado este sitio "); Enumeration e = h.keys(); while (e.hasMoreElements()) { ip = (String)e.nextElement(); i = (Integer)h.get(ip); ival = i.intValue(); out.print(" "+ ip+" has been "+ival+" times here"); } out.close(); } Get client’s IP and use it to retrieve the associated information (count)

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(" Han visitado este sitio "); Enumeration e = h.keys(); while (e.hasMoreElements()) { ip = (String)e.nextElement(); i = (Integer)h.get(ip); ival = i.intValue(); out.print(" "+ 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

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(" Han visitado este sitio "); Enumeration e = h.keys(); while (e.hasMoreElements()) { ip = (String)e.nextElement(); i = (Integer)h.get(ip); ival = i.intValue(); out.print(" "+ 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

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(" Han visitado este sitio "); Enumeration e = h.keys(); while (e.hasMoreElements()) { ip = (String)e.nextElement(); i = (Integer)h.get(ip); ival = i.intValue(); out.print(" "+ 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

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(" Han visitado este sitio "); Enumeration e = h.keys(); while (e.hasMoreElements()) { ip = (String)e.nextElement(); i = (Integer)h.get(ip); ival = i.intValue(); out.print(" "+ 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