Session Tracking.

Slides:



Advertisements
Presentaciones similares
Game Cluedo: How to Play 1.Your group should have the 21 cards containing 6 cards of suspects, 9 rooms and 6 weapons, a tally card for each member and.
Advertisements

Time Expression with Hacer
English Version El objetivo de esta guía es orientar al proveedor acerca de la realización de consultas de los pagos. Por favor haga uso de las flechas.
 Making complete sentences How to make complete sentences in Spanish. The following presentation is designed to help you learn how to do the following:
Sesión en Servlets 31 de Marzo de Índice ¿Por qué seguir la trayectoria de usuarios? Formas de seguir la trayectoria de usuarios –Cookies –Reescritura.
JSP COOKIES. cookies  Las cookies son datos guardados por los navegadores a instancias del servidor.  Los sitios Web las utilizan con propósitos diversos:
Programación orientada a objetos
Servlets, JSP, JavaBEANS,JDBC
Telling Time.
Servlets 18 de Marzo de Índice ¿Qué es un Servlet? ¿Cuándo y por qué usar Servlets? Estructura básica de un HttpServlet Servlet simple que genera.
TEMA 6. Applets y servlets
Fernando Alonso Blázquez Nicolás Serrano Bárcena Servlets 8 de Abril de 2008.
Time Expression with Hacer Grammar Essential #106.
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.
Desarrollo con Servlets. Servlets: Introducción  Módulos que amplían los servidores orientados a petición/respuesta.  La respuesta en el lenguaje Java.
Servlets Servlets son módulos que extienden a servidores “java- enabled” orientados a request/response, como por ejemplo Servidores Web Un servlet puede.
Servlets Servlets son módulos que extienden a servidores “java-enabled” orientados a request/response, como por ejemplo Servidores Web Un servlet puede.
Servlets Servlets son módulos que extienden a servidores orientados a request/response, como por ejemplo Servidores Web “java-enabled”. Un servlet puede.
Servlets 10 de Mayo de 2006.
Las Preguntas (the questions) Tengo una pregunta… Sí, Juan habla mucho con el profesor en clase. No, Juan no habla mucho en clase. s vo s vo Forming.
Objetivo: to identify phrases that express someone must do something. Ahora: List 3 adverbs and where are they placed in a sentence.
Servlets Servlets son módulos que extienden a servidores “java- enabled” orientados a request/response, como por ejemplo Servidores Web Un servlet puede.
Un juego de adivinanzas: ¿Dónde está el tesoro? A1B1C1D1E1F1 A4B4C4D4E4F4 A2B2C2D2E2F2 A5B5C5D5E5F5 A3B3C3D3E3F3 A6B6C6D6E6F6 Inténtalo de nuevo Inténtalo.
Temario Java 2 Enterprise Edition Arquitectura Especificación Servlets Contenedores Cookies Sesiones.
¿Cuánto tiempo hace que…? You can ask when something happened in Spanish by using: ¿Cuándo + [preterit verb]…? ¿Cuándo llegaste a la clínica? When did.
Haz ahora el 27 de octubre de 2015 Complete each sentence with the correct form of ser and estar: 1.Mi cuarto ________ al lado de la sala. 2.Manuel y Fernando.
Trabajar con SERVLETS. Índice Qué son los Servlets Para qué sirven Cómo se utilizan Ejemplo Cómo probar un Servlet.
Stamps to be traced. 2 and 3 ALC 21 Hoy es miércoles el 23 de octubre. match the columns writing out the words. 1.Fat 2.Handsome 3.Long 4.Old 5.Pretty.
EQUILIBRIUM OF A PARTICLE IN 2-D Today’s Objectives: Students will be able to : a) Draw a free body diagram (FBD), and, b) Apply equations of equilibrium.
Question Formation Español Uno.
Gustar V. Encantar.
Español 2 2 de mayo de 2017.
Las clases de Sra. Schwarz Realidades 1
PREGUNTAS: Questions and Question Words
Time Expression with Hacer
Día número 3 Español 1—Acelerado
Base de Datos II Almacenamiento.
First Grade Dual High Frequency Words
Sesiones 06 de Mayo de 2004 Fernando Alonso Blázquez.
El Imperfecto Español 2.
Telling time to the minute, using “y” and “menos”.
How to Log in to RosettaStone and Install the Speech Component
Hoy vamos a .. aprender a describir vacaciones con más detalles
Page 86 Realidades 2 Ser Vs. Estar.
PREGUNTAS: Questions and Question Words
Recetas 3 Objetivo: Hacer preguntas Hablar sobre la comida
Quasimodo: Tienes que hacer parte D de la tarea..
Hoy vamos a .. aprender a describir vacaciones con más detalles
Hace = For/Since.
How to Conjugate… SPANISH VERBS.
Servlets Servlets Son modulos que extienden las funcionalidades de un servidor “java-enabled” Normalmente generan código HTML dinámicamente, el cual se.
Asking Questions P. 184 Realidades 1.
¿Cuánto tiempo hace que canta esa nota?
Parametros enviados por el cliente
A PowerPoint Template Your Presentation Name. This text is a placeholder Main Content Page Layout 2 Copyright 2009.
Question Formation Spanish I Page 52.
Fundamentals of Web Development - 2 nd Ed.Randy Connolly and Ricardo Hoar Fundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar © 2017 Pearson.
Parametros multivalores
Los adjetivos demostrativos Notes #16 What is a demonstrative adjective in English? Demonstrative adjectives in English are simply the words: THISTHESE.
Development of the concert programme
Forming questions in Spanish
Servlets Servlets son módulos que extienden a servidores “java-enabled” orientados a request/response, como por ejemplo Servidores Web Un servlet puede.
Forming questions in Spanish
Forming questions in Spanish
Welcome to PowerPoint gdskcgdskfcbskjc. Designer helps you get your point across PowerPoint Designer suggests professional designs for your presentation,
Servlets Servlets son módulos que extienden a servidores “java-enabled” orientados a request/response, como por ejemplo Servidores Web Un servlet puede.
The causative is a common structure in English. It is used when one thing or person causes another thing or person to do something.
Globalization Politics and the preservation of nation state.
Las Preguntas (the questions) Tengo una pregunta… Sí, Juan habla mucho con el profesor en clase. No, Juan no habla mucho en clase. s vo s vo Forming.
Transcripción de la presentación:

Session Tracking

Servlet que propone un problema (suma dos numeros) y chequea si la respuesta del usuario es correcta public class CalculaSession extends HttpServlet { int op1, op2; protected void doGet(request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); out.println("<h1> Calcula </h1>"); op1 = (int)(Math.random()*100)+1; op2 = (int)(Math.random()*100)+1; out.println("<form method=post>"); out.println("<h2>"+op1+" + "+op2+" = "); out.println("<input type=text size=4 name=resultado>"); out.println("<input type=submit value=corregir>"); out.close(); }

Por qué esto no funcionaría ? protected void doPost( request, response) throws Exception { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String res = request.getParameter("resultado"); int nres = Integer.parseInt(res); out.println("<h2>"); if (nres == op1+op2) out.println("Excelente "); else out.println(" El resultado era "+(op1+op2)); }

Un servlet es concurrente !! Op1 y op 2 son variables globales del servlet Cada vez que un usuario visita el servlet con un GET el servlet genera dos numeros distintos y reemplaza los antiguos Durante el tiempo que un usuario recibe respuesta de doGet y va a doPost otro usuario puede estar llamado a doGet y cambiar los valores de op1 y op2 El doPost va a chequear la respuesta con los últimos valores generados por doGet (que pueden no ser los originales) La respuesta buena podría ser considerada como mala en este caso porque entre medio se cambiaron los valores Para evitar esto el servlet debe recordar cuales valores generó para que usuario y usar ellos cuando el usuario esté de vuelta Esto se hace con la técnica llamada “Session tracking”

Session Tracking Session tracking es un mecanismo que los servlets pueden usar para mantener información acerca del estado de la conversación con el cliente Una session es un dialogo entre unsa instancia de un browser y el servidor`por un cierto período de tiempo (por defecto, hasta 30 minutos de imactividad). Es posible asociar información a una sesión para poder llevar a cabo esto La sesión no es administrada por el servlet sino por el servidor. Ver SessionServlet

Algunos métodos HttpSession sesion = request.getSession(true) crea un objeto sesión si no existía antes, si existía lo recupera. sesion.isNew()retorna verdadero si el objeto sesión es nuevo sesion.putAttribute/Value(String nombre, Object valor) asocia al parámetro nombre el valor valor (value se usa hasta v2.2) Object o = sesion.getAttribute/Value(“nombre”)retorna el objecto asociado al nombre sesion.removeAttribute/Value(“nombre”)borra el par nombre,valor asociado a esa sesión Enumeration[]valores = sesion.getAttributeNames() String[]valores = sesion.ValueNames() retorna un arreglo/ennumeration de los attributes/values que se han guardado en la sesión long l = sesion.getCreationTime()retorna el tiempo (en milisegundos a partir de 1.1.70 0:0:0 ) de cuando el objeto sesión fue creado Long l = sesion.lastAccessedTime() retorna el tiemp en milisegundos de cuando fue accesado por última vez el objeto sesion.setMaxInactiveInterval(int seconds)pone (modifica) el timeout de la sesión

El mismo servlet CON sessions public class CalculaSession extends HttpServlet { int op1, op2; protected void doGet(request, response) throws Exception { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); HttpSession session = request.getSession(false); out.println("<h1> Calcula </h1>"); op1 = (int)(Math.random()*100)+1; op2 = (int)(Math.random()*100)+1; session.setAttribute("op1",""+op1); session.setAttribute("op2",""+op2); out.println("<form method=post>"); out.println("<h2>"+op1+" + "+op2+" = "); out.println("<input type=text size=4 name=resultado>"); out.println("<input type=submit value=corregir>"); out.close(); }

El mismo servlet CON sessions protected void doPost( request, response) throws Exception { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); HttpSession session = request.getSession(true); String res = request.getParameter("resultado"); int nres = Integer.parseInt(res); op1 = Integer.parseInt((String)session.getAttribute("op1")); op2 = Integer.parseInt((String)session.getAttribute("op2")); out.println("<h2>"); if (nres == op1+op2) out.println("Excelente "); else out.println(" El resultado era "+(op1+op2)); }

¿ Como acumular productos en varias visitas ? La idea es que en cada visita se vayan seleccionando productos Estos se van acumulando para cada usuario Se puede usar un objeto Session para guardad pares <codigo de producto><cantidad acumulada> Despues con cada visita se actualiza el estado de estos pares según lo que se seleccionó

Veamos de nuevo la selección de productos public void doGet( .. request, ... response) throws . . . { Hashtable items = Item.getItems(); . . . . out.print("<form action=ProcessPage method='POST'>"); while(enum.hasMoreElements()) { Product e = (Product)enum.nextElement(); out.print("<TR>"); out.print("<TD>" + e.number); out.print("<TD>" + e.name ); out.print("<TD>" + e.price+"<TD>"); out.print("<input type=textarea SIZE=3 "+ out.print(" name="+e.number+" value=0 >"); } out.println("</TABLE>"); out.println("<INPUT TYPE='SUBMIT' VALUE='Process'>");

Incorporemos la memorización public void doGet( .. request, ... response) throws . . . { Hashtable items = Item.getItems(); HttpSession s = request.getSession(true); . . . Enumeration en = request.getParameterNames(); int total = 0; out.print("<form action=ProcessPayment method='POST'>"); while(en.hasMoreElements()) { String number = (String)en.nextElement(); String qtty = request.getParameter(number); int nqqty = Integer.parceInt(qtty); if (nqqty == 0) continue; String qttyAntigua = (String)s.getAttribute(number); if (qttyAntigua == null) s.setAttribute(codigo,cantidad+""); else { int qttyNueva = Integer.parseInt(qttyAntigua)+nqtty; s.setAttribute(codigo,nuevaCantidad+""); } Product e = (Product)item.get(number); out.print("<TR> <TD>" + e.number+"<TD>" + e.name ); out.print("<TD>" + e.price+"<TD>“+e.price*nqtty); out.println("</TABLE>");

Mostremos lo que llevamos out.println("</TABLE>\n</BODY></HTML>"); out.println("<br><br><h2> So far you have chosen</h2>"+ "<TABLE BORDER=1 ALIGN=CENTER>\n" + "<TR BGCOLOR=\"#FFAD00\">\n" + "<TH>codigo<TH>cantidad<TH> subtotal"); int total = 0; Enumeration e = s.getAttributeNames(); while(e.hasMoreElements()) { String codigo =(String)e.nextElement(); out.print("<TR><TD>" + codigo + "<TD>"); String scantidad =s.getAttribute(codigo); int ncantidad = Integer.parseInt(scantidad); out.println(ncantidad); Product e = (Product)item.get(number); out.println(“<TD>”+(ncantidad*e.price)); total = total + ncantidad*e.price; } out.println("</TABLE><br>"); out.println("<a href='OrderPage'> return to order </a> <br>"); out.println("<a href='CuentaPage'> make order </a>");

Usando Cookies Cookies son otra manera de hacer “sesion tracking” Por medio de una cookie el servlet puede enviar información al cliente que la guarda y la devuelve cada vez que el browser contacta al mismo servidor de nuevo. Servlets mandan cookies al cliente por medio de la información que va en el header, así mismo devuelven los clientes esta información al servidor. Los clientes devuelven automáticamente las cookies cada vz que contactan al servidor que se las envió (las use o no) en el header. Cookies tienen un nombre y un valor (ambos strings) Adicionalmente pueden guardar un comentario (otro string) Un servidor puede mandar más de una cookie

Usando Cookies Para mandar cookies Recuperando las cookies, 1. Instanciar (crear) un objeto Cookie Cookie c = new Cokie(string, string); 2. Mandar la cookie response.addCookie(c); Recuperando las cookies, 1. Recuperar toda las cookies Cookie[] c = request.getCookies(); 2. Recuperar el nombre y el valor String name = c[i].getName(); String value = c[i].getValue();

Ejemplo Cookies void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); out.println("<h1> Agregue una cookie </h1> <h2>"); out.println("<form method=POST>"); out.println("Nombre : <input type=text name=cnombre>"); out.println("Valor : <input type=text name=cvalor>"); out.println("<br> <input type=submit value=enviar>"); out.println("</html>"); out.close(); }

protected void doPost( … response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); Cookie[] c = request.getCookies(); String cn = request.getParameter("cnombre"); String cv = request.getParameter("cvalor"); if (cn != null && cv != null) { Cookie nuevacookie = new Cookie(cn,cv); response.addCookie(nuevacookie); out.println("<h1>La cookie con nombre "+cn+ " y valor "+cv+" sera mandada</h1>"); } if (c != null) { out.println("<h2> ademas las siguientes cookies fueron recibidas </h2>"); for (int i = 0; i < c.length; i++) out.println("<br>Nombre : "+c[i].getName()+" valor : "+c[i].getValue()); out.print("<form><input type=submit value=retornar>"); out.close();

Cookie Methods int getMaxAge() String getName() String getValue() Returns the maximum age of the cookie, specified in seconds, By default, -1 indicating the cookie will persist until browser shutdown.  String getName() Returns the name of the cookie.  String getValue() Returns the value of the cookie.  void setComment(String comment) Specifies a comment that describes a cookie's purpose.  voidsetMaxAge(int expiry) Sets the maximum age of the cookie in seconds.  void setValue(String newValue) Assigns a new value to a cookie after the cookie is created.

Examples of Cookies The first example (Cookies.java) shows the times when the client contacted the servlet for the first time (via doGet method) and the time when it contacted the server by pressing the button The second example (CookieExample) shows how to retrieve all the cookies The third example (SetCookie and ShowCookies) shows how to put time-out values for a cookie

¿ Cookies or Sessions ? With sessions the information is stored on the server, this means, there is a state which has to be administrated carefully With cookies it is the client which has the information, this means the information travels back and forth every time the client contacts the server The client can prohibit the use of cookies Sessions can store much more (and better) information Sessions are implemented with cookies !!!!!!!!

The headers of request and response Provide high level information from the client and to the client The request allows the servlet to obtain interesting characteristics of the client The response allows the servlet to define how the information will be delivered to the browser In general, they help make the dialog with the client more effective For the request, there are methods called getXXX or getHeader(xxx) to obtain information For the response, there are methods called setHeader(xxx) or setXXX for defining the form of the response data. Often both are required to be used in combination to generate an adequate response

Some get for the request getCookies(): received the cookies which the client browser may have sent getAuthType(): is used for clients trying to access a page for which a password is required getRemoteHost(): to obtain the hostname of the client getMethod(): to get the name of the method with which the browser contacted the servlet (GET, POST, etc..) getProtocol(): version of the HTTP protocol the client is using getHeaderNames(): the name of all the headers the client has sent (is variable depending on the HTTP and browser version

Some xxx for the getHeader(xxx) “Accept”: which MIME types the client “understands” “Accept-Charset”: which character set the client is using “Accept-Encoding”: encoding algorithms the client accepts “Accept-Language”: language (en-us, sp, ge, ..) “Authorization”: to identify clients with a protected page “Host”: the client’s computer name “Referer”: the URL of the page that generated the contact “Cookie”: to obtain the cookies

Some set for the response setContentType(xxx): for informing the MIME type of the response setContentLength(xxx): for informing the length of the response (used when transmitting bytes) addCookie(c): to add cookies with information to the client sendRedirect(xxx): to redirect the request to another URL setHeader(xxx,xxx) a general form setIntHeader(xxx,xxx) when the second argument is an integer (no need to convert to string)

Some xxx for the setHeader(xxx,xxx) Content-Type: some MIME type like “image/gif” Content-Length: length (para bytes) Content-Encoding: codification Content-Language: language Cache: como se debe manejar el cache en el cliente (ej, no- cache, no-store, must-revalidate, max-age=xxxx, Refresh: informs the browser how often the page should be refreshed www-Authenticate: for managing pages protected with passwords

Some more elaborated exemples showing the use of these methods ShowRequestHeaders: just shows the headers of the request ProtectedPage: shows how to ask for a password (run PasswordBuilder first) Ping & Pong: shows redirection