Universidad de Chile - Tupper 2007, Santiago - Fono: 678 4888 - Fax: 698 8427 - Módulo 9: Desarrollo de Aplicaciones.

Slides:



Advertisements
Presentaciones similares
-go Verbs There is a small but very important group of verbs that we call the -go verbs. These verbs are: Conocer : to know (people) Hacer: to make/do.
Advertisements

WALT: talking about dates and saying when your birthday is WILF: To be able to write & understand months and dates in Spanish to get to a Level 2 To be.
Subject Pronouns Los Pronombres Sujetos Diane Brooks FL490.
VOCABULARIO #2.4 ¡Aprenda! Forming Questions Señora Sequin.
Starter: stars and wishes. Learning objectives: To use a writing frame to construct new language and memory strategies to remember it Outcome: Approximately.
Verbs Like Gustar GUSTAR Y VERBOS SIMILARES. Hoy vamos a hablar sobre los intereses, las preferencias y las actividades en la vida de… Señor Mock.
1 – 999,999,999.
Objective: I can recognize and accurately use gender agreement. Do Now: Match the following Spanish and English words: 1. Pelirroja a. Good-looking 2.
Los complementos directos Direct Object Pronouns.
IRREGULAR VERBS. Remember how regular verbs are formed? You drop the –ar, -er, or –ir and add the appropriate ending.
Formal Commands! ¡Atención! In this presentation, you will learn about making commands in Spanish.
Using Gustar Avancemos Level 1. Help me fill in the grid with Spanish pronouns.. Yo Tú Él, Ella, Usted Nosotros/as Ellos, Ellas, Ustedes X.
-AR Verbs In Spanish, there are three classes (or conjugations) of verbs: those that end in –AR, those that end in –ER, and those that end in –IR. This.
DIRECT OBJECT PRONOUNS. DIRECT OBJECTS The object that directly receives the action of the verb is called the direct object. Mary kicked the ball. "Ball"
How to Conjugate Regular –AR – Er - IR Verbs in the Present Tense.
Capítulo uno Gramática 1.2 Subject pronouns (Los pronombres)
NEBULOUS/NEBULOSA/ DESDIBUJADA Marcela T. Garcés Assistant Professor of Spanish Published in : Label Me Latina/o Spring 2012 Volume II.
What has to be done today? It can be done in any order. Make a new ALC form Do the ALC Get two popsicle sticks Get 16 feet of yarn. That is 4 arms width.
Page 114 En español 2 Reflexive Verbs Do you remember your Indirect Object Pronouns?
Los verbos reflexivos Objective:
DIRECT OBJECT PRONOUNS. DIRECT OBJECTS The object that directly receives the action of the verb is called the direct object. Mary kicked the ball. "Ball"
Study this picture for 1 minute. Try to remember as much as possible about it. Now tell your partner about the picture. Take it in turns to say something.
Parameters passed by client One of the most important features to make the web an interactive environment is the passing of parameters from client so the.
Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones.
Servlets Servlets son módulos que extienden a servidores orientados a request/response, como por ejemplo Servidores Web “java-enabled”. Un servlet puede.
ALC #7 Do the math problems and write the answer in Spanish.
Unit 2A: Lesson 2 How to Talk About Your Schedule Gramática- Present tense of –ar verbs.
Definition
Definite & indefinite articles
Digital Photography: Selfie Slides Liliana Martinez 10/27/14 4b.
English Lesson 3.
Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones.
Digital Photography: Selfie Slides By: Essence L. Thomas.
¿Qué haces en la escuela? Question words, objects, yo-go’s.
Indirect Object Pronouns
Formal Commands! Telling people what to do…… You speak, or you are speaking……Hablas, estás hablando This is different from telling (commanding) someone.
Digital Photography: Selfie Slides Your Name Date Class Period.
Photo Vocabulary Scavenger Hunt Rich Bradshaw. What is this? - A group activity for middle or high school students. - Highly interactive and very fun.
-go Verbs There is a small but very important group of verbs that we call the “-go” verbs. These verbs are: Conocer : to know (people) Hacer: to make/do.
1. Escribe la fecha y el objetivo. 2. Saca la tarea! 3. Translate the following sentences from Spanish to English. 1. Me gustan los deportes porque son.
Time Telling time is rather easy. You only need to know the numbers up to 59 to be able to tell the time.
Digital Photography: Selfie Slides Ana Hernandez 10/23/14 5th.
Digital Photography: Selfie Slides
Formal Commands! Ten-Hut! In this presentation, you will learn about making commands in Spanish.
Spanish Sentence Structure How can we make better sentences?
JUEVES, EL 10 DE SEPTIEMBRE LT: I WILL RECOGNIZE SOME NEW VOCABULARY WORDS. Go over tests & retake procedures Interpretive Assessment: numbers & alphabet.
ON YOUR DESK Documento Today’s notes Homework. ¡Hola! Conjugate the following verbs: 1. Yo / comer 2. Él / bailar 3. Nosotros / vivir 4. Ellos / abrir.
Indirect Object Pronouns Original PowerPoint was by Ms. Martin of Tri-Center Community Schools.
Double Object Pronouns What happens when we have both direct and indirect object pronouns in the same sentence? What goes where? Information for this lesson.
How would you spell the following word out loud in Spanish? Abuelito.
ECOM-6030 PASOS PARA LA INSTALACIÓN DE EASYPHP Prof. Nelliud D. Torres © - Derechos Reservados.
To be, or not to be? Let’s start out with one of the most important verbs in Spanish: ser, which means “to be.”
Antes de empezar – ¿Cómo se llaman estos quehaceres? (Necesito la tarea.)
LOS VERBOS REFLEXIVOS. WRITE: What is a reflexive verb? A reflexive verb describes when a person doing an action is also receiving the action.
Forming Questions ¡Aprenda! Forming Questions By Patricia Carl October 2013.
English Language II (2). English Language I (2) Warm-up.
17 de septiembre SER y Indirect Object Pronouns. QUIZ: TRANSLATE to Spanish He is Nicaraguan. They are American. We are Guatemalan. She’s Peruvian. Are.
Verbs like Gustar Notes/ Examples.
Saber Vs. Conocer Sra. Altamirano Español II. First, we need to learn the conjugation of the verb SABER Yo sé Tú Sabes él, ella,ud. Sabe Nosotros Sabemos.
Los verbos reflexivos Objective: To be able to talk about your daily routine. Getting ready for a special event.
Lunes, 5/10/15  What is the purpose of a subject pronoun?  Give at least one example of a subject pronoun in English.
A + el = al de + el = del Capítulo 1A. To be pleasing to or To please Gustar I like your shoes! Your shoes are pleasing to me! I like to watch TV. To.
Los Pronombres relativos Relative pronouns. Relative pronouns are words that:  Connect ideas within one sentence  Most frequently refer back to a noun.
Escribir *You can get creative. You can write in the first person which means you are the character, you can use the third person which means you are talking.
Question Words ¿Cómo estás? How are you? ¿De dónde eres? Where are you from? ¿Quién eres? Who are you? We are already familiar with several words used.
To be, or not to be? Let’s start out with one of the most important verbs in Spanish: ser, which means “to be.”
To be, or not to be? Let’s start out with one of the most important verbs in Spanish: ser, which means “to be.”
First Grade Dual High Frequency Words
Development of the concert programme
Transcripción de la presentación:

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 1 Java Spaces

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 2 Definition according to sun  JavaSpacesTM technology is a simple unified mechanism for dynamic communication, coordination, and sharing of objects between JavaTM technology-based network resources like clients and servers. In a distributed application, JavaSpaces technology acts as a virtual space between providers and requesters of network resources or objects. This allows participants in a distributed solution to exchange tasks, requests and information in the form of Java technology-based objects. JavaSpaces technology provides developers with the ability to create and store objects with persistence, which allows for process integrity.

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 3 JavaSpaces is part of the Jini spec.  Jini is a collection of service specifications  They help services to find each other on the network  The most common notion people have from Jini is that enables jini powered machines to be attached to the network and automatically find out which services are available, as well as post its own services to the rest  Jini based machines are servers and client at the same time

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 4 Example ? ?

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 5 Services provided by Jini Lookup Services (reggie) rmid HTTP-Server (tools.jar) Transaction Services (mahalo.jar) JavaSpace (outriggger) Fiddler Mercury Norm

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 6 What does JavaSpaces provides  A space in which objects can be stored in, retrieved from, copied from.  A set of methods: write, read, take, (readIfExists, takeIfExists)  A mechanism to ensure completeness of transactions  An event notify mechanism

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 7 The write and read write Space read A copy

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 8 The take X write Space take Exists only here

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 9 How can I create a JavaSpace in a host  A JavaSpace can be created by starting the outrigger service of Jini with a certain name as parameter. This will be the name of the space.  Before starting the outrigger it is necessary to start:  The http server tools.jar  An rmid  The lookup server rigger  The transaction server mahalo

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 10 Commands  java -jar c:\jini_1\tools.jar –dir C:\jini1_1  rmid -J-Dsun.rmi.activation.execPolicy=none.  java -Djava.security.policy=policy.txt -jar C:\jini1_1\reggie.jar policy.txt reggie_log public  java -Djava.security.policy=policy.txt -jar $JINI/mahalo.jar dl.jar policy.txt txn_log public  java -Djava.security.policy=policy.txt - Djava.rmi.server.codebase= /outrigger-dl.jar - Dcom.sun.jini.outrigger.spaceName=JavaSpaces -jar C:\jini1_1\transient-outrigger.jar public

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 11 How can I write objects into a Space  User defined classes must declare they implement the interface Entry.  This does not mean it has to implement any methods. It is just a tag to tell the system an object of this class can be written in a JavaSpace  Fields (variables) in this class should be objects. If primitives are nedded (such as integers) use wrappers  Integer i = new Integer(4)

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 12 Example of an Entry class import net.jini.core.entry.Entry; public class Message implements Entry { public String content; public Integer counter; public Message() { //this is mandatory !!!!! } public Message(String content, int initVal) { this.content = content; counter = new Integer(initVal); } public String toString() { return content + counter + " times."; } public void increment() { counter = new Integer(counter.intValue() + 1); }

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 13 Storing objects in a space import net.jini.core.lease.Lease; import net.jini.space.JavaSpace; import java.io.*; public class WriteMessages { public static void main(String[] args) { try { BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); JavaSpace space = SpaceAccessor.getSpace(); System.out.print("Message ? "); String m = in.readLine(); Message2 msg = new Message2(m,0); space.write(msg, null, Lease.FOREVER); } catch (Exception e) {e.printStackTrace(); } }

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 14 The write sentence  For running this program write:  java -Djava.security.policy=policy.txt -Doutrigger.spacename=JavaSpaces -Dcom.sun.jini.lookup.groups=public -cp c:\jini1_1\lib\space-examples.jar; c:\jini1_1\lib\jini-core.jar; c:\jini1_1\lib\jini-ext.jar: -Djava.rmi.server.codebase= examples-dl.jar WriteMessages  The Parameters: an Entry object (the Message), a transaction object (null for now) and a long (leasing time in milliseconds, Lease.FOREVER is the value to make it permanent).

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 15 Retrieving objects in a space import java.io.*; import net.jini.core.lease.Lease; import net.jini.space.JavaSpace; public class ReadMessage { public static void main(String[] args) { try { Message2 template; JavaSpace space = SpaceAccessor.getSpace(); BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); System.out.print("Message (**=null)? "); String m = in.readLine(); template = new Message2(m); Message2 result = (Message2)space.read(template,null,Long.MAX_VALUE); System.out.println("Got "+result.toString()); } catch (Exception e) {e.printStackTrace(); }

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 16 Rules for retrieving objects  The object must match the template according to the class and the content  Null references in the template act as wildcards  There is no rule for deciding which of all the matching objects in the space matching the template will be retrieved  The parameters: a template object, a transaction (null) and a waiting time before the read sentence gives up if it does not find an object which matches the template. Long.MAX_VALUE is for waiting forever

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 17 Taking objects  It works just like read but the object is deleted from the space  In order to avoid deadlocks or waiting, it is possible to use readIfExists and takeIfExists  Their structure is the same as read and take, but they will return immediately if they do not find any matching object in the space  The SpaceAccessor class with the getSpace method is not standard, see SpaceAccessor.java

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 18 A “distributed” array index content index content index content Write ReadUpdate

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 19 Synchronizing clients Ticket Dispenser number Customer - takes a number, - increments Ticket Disp. - takes the service object when it has this number - waits for being served -Increments service number A client will be served Only if it has the service number Service Number number

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 20 And of course…Chatting Tail number A Tail object indicates which is the number of the last message Every message has a content And a number The channel identifies a „chat.-thred“ Message number Message number Message number Channel

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 21 Distributed Events 1- Create a Listener object 2- Notify space about interest With a template 3- an object matching template Enters the space 4- the listener is nofied

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 22 How to write a listener import java.rmi.server.*; import java.rmi.RemoteException; import net.jini.core.event.*; import net.jini.space.JavaSpace; public class Listener implements RemoteEventListener { private JavaSpace space; public Listener(JavaSpace space) throws RemoteException { this.space = space; UnicastRemoteObject.exportObject(this); } public void notify(RemoteEvent ev) { Message template = new Message(); try { Message result = (Message)space.read(template, null, Long.MAX_VALUE); System.out.println(result.content); } catch (Exception e) { e.printStackTrace(); }

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 23 A program that listens import net.jini.core.lease.Lease; import net.jini.space.JavaSpace; public class HelloWorldNotify { public static void main(String[] args) { JavaSpace space = SpaceAccessor.getSpace(); try { Listener listener = new Listener(space); Message template = new Message(); space.notify(template, null, listener, Lease.FOREVER, null); } catch (Exception e) { e.printStackTrace(); }

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 24 Calling the notify method  After this, if any program writes a message (no matter the content) the Listener object of the HelloWorldNotify program will be „notified“, this means, the notify method will be called Message msg = new Message(); msg.content = "Hello World"; space.write(msg, null, Lease.FOREVER);

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 25 Transactions  A transaction is a set of instructions which should be all executed atomically or none at all.  In JavaSpaces this refers to write, read, and take instructions that can be scheduled to be performed in this way.  For this, a transaction object should be created and passed as an argument in every instruction which belongs to the transaction  After all write, read, take, etc.instructions with (the same) transaction are “executed”, a commit statement will either perform all the operations or no one.  In the latter case, an exception is thrown

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 26 An example JavaSpace space = SpaceAccessor.getSpace(); TransactionManager mgr = TransactionManagerAccessor.getManager(); //get a reference to the transaction manager service Try { Transaction.Created trc = TransactionFactory.create(mgr, 3000); Transaction txn = trc.transaction; SharedVar template = new SharedVar(url); SharedVar counter = (SharedVar) space.take(template, txn, Long.MAX_VALUE); counter.increment(); space.write(counter, txn, Lease.FOREVER); txn.commit(); } catch (Exception e) { System.err.println("Transaction failed"); return; }

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 27 Java Applets Java program running on the client Html Animator.class Applets are java programs which are downloaded with the HTML page. Animator.class

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 28 Java Script Java program running on the client Html & Script The code of the java program is written directly in the HTML page the code

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 29 Java Servlets The code of the java program which runs on the server and can dynamically produce HTML content according to the particular situation (the client of a bank) MyServlet.class HTML-page with a reference to a servlet HTML from page HTML from servlet

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 30 Java Server Pages (and...)  Like Java Script for applets, JSP is a script language programming for servlets  The code is written inside the HTML page but it is executed on the server  The server will dynamically generate HTML code, which will be “written” into the client’s browser

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 31 How can I explain what is an Applet ?  For people like you, who already master Java programming it is very easy: an applet is a Panel in an HTML page in which I can program (almost) everything I can program in a normal Panel. The html viewer The applet Bla Bla

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 32 How do I link an Applet in my HTML Page ? My first Applet Here is how you attach an applet to your html page!!!..... You must provide an Applet which is programmed in a file named MyApplet.java and compiled just as any other class

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 33 How do I Program an Applet ? import java.applet.*; import java.awt.*; public class MyApplet1 extends Applet { public void paint(Graphics g) { g.drawString(“Hello World”,50,25); } Let´s start with a very simple one This tells us the applet is a kind of frame !!

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 34 Remember the Jalisco Program ? import java.awt.*; import java.applet.*;import java.awt.event.*; public class Jalisco1 extends Applet { Label question = new Label("Enter a number :"); Label answer = new Label(" "); TextField number = new TextField(9); public void init() { add(question); add(number); add(answer); setBackground(Color.green); number.addActionListener( new ActionListener() { //this will make the thexfield react when an enter is typed public void actionPerformed(ActionEvent x) { String s = number.getText(); int n = Integer.parseInt(s); answer.setText("I win with the " + (n+1)); } }); }

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 35 The applet has a life cycle  When an applet is loaded  The applet initializes itself, running the init() method if provided  The applet starts running, executing the start() method if provided  When you leave and return to the page  when leaving the page (going to another page) the applet stops itself, executing the stop() method before if provided  when returning to the page it srarts again executing the start() method  When quitting the browser:  the applet can run the destroy() method if provided

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 36 Secutiry with applets  The applet is a running program in my computer which I just downloaded from the network: how can I be sure it will not harm my computer ?  Applets cannot read or write the disks of the host computer !  Because java has not pointer arithmetic a class generated by the compiler cannot read or write other memory than just the memory of the program (in C we have this problem !!)  In order to avoid Trojan Horses, every applet is “signed” by the compiler. This means, if a.class file was not generated by the compiler, it can be easily detected by the applet loader of the viewer and it doesn't allow it to run

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 37 What Applets Can't Do  It cannot ordinarily read or write files on the host that is executing it.  It cannot make network connections except to the host that it came from.  It cannot start any program on the host that's executing it.  It cannot read certain system properties.  Windows that an applet brings up look different than windows that an application brings up.

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 38 What Applets Can Do  Applets can usually make network connections to the host they came from.  Applets running within a Web browser can easily cause HTML documents to be displayed.  Applets can invoke public methods of other applets on the same page.  Applets that are loaded from the local file system (from a directory in the user's CLASSPATH) have none of the restrictions that applets loaded over the network do.  Although most applets stop running once you leave their page, they don't have to.

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 39 Servlets  Servlets are modules that extend request/response- oriented servers, such as Java-enabled web servers.  A servlet might be responsible for taking data in an HTML order-entry form and applying the business logic used to update a company's order database.  Servlets are an effective replacement for CGI scripts. They provide a way to generate dynamic documents that is both easier to write and faster to run.  Servlets also address the problem of doing server- side programming with platform-specific APIs: they are developed with the Java Servlet API, a standard Java extension.

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 40 Preliminary Work  The javax.servlet package provides interfaces and classes for writing servlets  This is not part of the standard language, it is necessary to download JSDK(2.1) package  Not all web servers can serve servlets, you should either have one with this capability(e.g. Tomcat), download a plug-in, or use the servletrunner  Each servlet server has it rules how to make servers accessible. In most cases, you should specify a certain path where servlets are located  In order to be able to compile servlets you will need to put the jsdk.jar file in the propper directory (c:\jdk1.3\jre\lib\ext)

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 41 The Anatomy of a Servlet  A new servlet class is defined by extending HttpServlet class (in most cases)  The most important pre-defined methods of a servlet are:  init() it is called when the servlet is called for the first time (uploaded)  doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException Called when servlet invoked by a GET Http request  doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException Called when servlet invoked by a POST Http request

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 42 Anatomy of a servlet  A GET request is always generated when an http request is typed into a browser ( means GET index.html Httpx.x )  When the server is called for the first time, it is initialized and 4-6 threads for attending clients are started, this means they may be served in parallel  In most cases, servlets are contacted by forms in html pages. In this cases, it is possible to specify some parameters and contact the servlet by the POST method.

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 43 A First example  The servlet will be contacted directly by an http expression in the browser:  duisburg.de/servlets/SimpleServlet duisburg.de/servlets/SimpleServlet  This will cause the doGet method to be called  The servlet will answer by writing in the client‘s browser a page with a simple message

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 44 A Second example  The servlet will be contacted by an action triggered from the web browser:  duisburg.de/servlets/Servlet1.htm  The different buttons will call the doPost and the doGet methods

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 45 A Third Example  We will now develop a web counter  It will keep track about how many instances of the servlet have been created and how many times these instances have reacted to client‘s requests  See Count.java

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 46 Passing parameters  It is possible to pass parameters to the doGet method in the command line   The servlet can ask for a value parameter value:  String value = req.getParametervalues(param1)  Parameters are only strings  See ServletParameter1.java

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 47 We can use the same with Forms  A form will be an html page in which we can write a string and send the request to the servlet:  We can use the same servlet !!!!  see ServletParameter1.html  Another example with more parameters  SurveyServlet with JdcSurvey

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 48 Obtaining more information about the client  We can get a lot of information about the request made by the client such like:  URL request  Data about the client’s host  Parameters names and values  Header of the request  etc  Try SnoopServlet?par1=val1&par2=val2

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 49 Session Tracking  Session tracking is a mechanism that servlets use to maintain state about a series of requests from the same user (that is, requests originating from the same browser) across some period of time.  See SessionServlet

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 50 Using Cookies  Cookies are a way for a servlet to send some information to a client to store, and for the server to later retrieve its data from that client.  Servlets send cookies to clients by adding fields to HTTP response headers.  Clients automatically return cookies by adding fields to HTTP request headers.  Cookies have a name and a value (additionally they can have some comments).  In addition to a name and a value, you can also provide optional attributes such as comments.  A server can provide one or more cookies to a client.

Universidad de Chile - Tupper 2007, Santiago - Fono: Fax: Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 51 Using Cookies  To send a cookie 1.Instantiate a Cookie object 2.Set any attributes 3.Send the cookie  To get information from a cookie, 1.Retrieve all the cookies from the user's request 2.Find the cookie by its name 3.Get the values of the cookies that you found