Parameters passed by client One of the most important features to make the web an interactive...

16
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 server The client can pass parameters with the request according to the following format http://host:port/servlet?param1=value1&param2=value2 This means the server will receive 2 parameters: one with name param1 and value value1 and the other with name param2 and value value2 The servlet can ask for those values in the following way: String valueOfParam1 = request.getParameter(“param1”); String valueOfParam2 = request.getParameter(“param2”); Parameter names and values are strings Names of parameters are case sensitive (Param1 != param1)

Transcript of Parameters passed by client One of the most important features to make the web an interactive...

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

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 server

• The client can pass parameters with the request according to the following format

– http://host:port/servlet?param1=value1&param2=value2

– This means the server will receive 2 parameters: one with name param1 and value value1 and the other with name param2 and value value2

• The servlet can ask for those values in the following way:– String valueOfParam1 = request.getParameter(“param1”);– String valueOfParam2 = request.getParameter(“param2”);

• Parameter names and values are strings • Names of parameters are case sensitive (Param1 != param1)

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

import javax.servlet.http.*;import javax.servlet.*;import java.io.*; public class ServletParameter1 extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, java.io.IOException { PrintWriter out = null; response.setContentType("text/html");

// obtaining parameter value for parameter named “firstname" String fname = request.getParameter(“firstname");

// obtaining parameter value for parameter named “lastname" String lname = request.getParameter(“lastname");

out = response.getWriter(); out.println(<h1> "Hello "+fname+" “+lname</h1");

out.close(); }

}

http://host:port/ServletParameter1?firstname=Nelson&lastname=Baloian

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

The normal way is to gather parameters with forms

• A Form is an HTML page which may contain graphical objects to gather information which is sent to the server automatically in an URL

<HTML><H1> Collecting parameters</H1><FORM ACTION=“ServletParameter1”>Nombre: <INPUT TYPE=TEXT NAME=fistname><BR>Apellido: <INPUT TYPE=TEXT NAME=lastname><BR><INPUT TYPE=SUBMIT VALUE=“MANDAR”></HTML>

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

<HTML><H1> Collecting parameters</H1>

<FORM ACTION=“ServletParameter1”>Nombre: <INPUT TYPE=TEXT NAME=fistname><BR>Apellido: <INPUT TYPE=TEXT NAME=lastname><BR><INPUT TYPE=SUBMIT VALUE=“MANDAR”></FORM></HTML>

<FORM> y </FORM> Definen el comienzo y fin de un “formulario” que se llena para transferir los datos al servidor

ACTION= “…” define que acción se tomará, en este caso, a dónde se mandarán los datos ingresados una vez que se oprima el botón de submit

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

<HTML><H1> Collecting parameters</H1>

<FORM ACTION=“ServletParameter1”>Nombre: <INPUT TYPE=TEXT NAME=fistname> <BR>Apellido: <INPUT TYPE=TEXT NAME=lastname> <BR><INPUT TYPE=SUBMIT VALUE=“MANDAR”></FORM></HTML>

<INPUT … es para definir un elemento de entrada de datos o de interacción Este elemento será transferido como parámetro con la URL

TYPE define el tipo, en este caso se trata de un texto (TEXT)

NAME es el nombre del elemento de input y también será el nombre del parámetro

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

<HTML><H1> Collecting parameters</H1>

<FORM ACTION=“ServletParameter1”>Nombre: <INPUT TYPE=TEXT NAME=fistname> <BR>Apellido: <INPUT TYPE=TEXT NAME=lastname> <BR><INPUT TYPE=SUBMIT VALUE=“MANDAR”></FORM></HTML>

TYPE=SUBMIT define un elemento con forma de botón que al oprimirlo (click) contacta la URL definida en ACTION y envia como parámetros los contenidos de Los elementos definidos como INPUT

VALUE=“……” define la leyenda que tendrá el botón

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

Al oprimir el botón se obtiene el Resultado que muestra la figura de abajoFijarse en la URL que se generó Automáticamente con los parámetros

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

<HTML><H1> Este es el programa Jalisco, nunca pierde</H1>

<FORM ACTION=“Jalisco”>Ingresa un número cualquiera y luego oprime el botón: <INPUT TYPE=TEXT NAME=numero><BR><INPUT TYPE=SUBMIT VALUE=“jugar”></FORM></HTML>

¿ Qué página genera esto ?

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

import javax.servlet.http.*;import javax.servlet.*;import java.io.*; public class Jañisco extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, java.io.IOException { PrintWriter out = null; response.setContentType("text/html");

// obtaining parameter value for parameter named “numero" String snum = request.getParameter(“numero");

// converting to integer valus int num = Integer.parseInt(snum);

out = response.getWriter(); out.println(<h1> “Te gano con el "+(num+1)+"</h1");

out.close(); }

}

http://host:port/Jalisco?numero=47

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

:

El siguiente código HTML <HTML><H1> Servlet Calculadora </H1><H2> Ingrese ambos operandos de una suma </H2> <BR><FORM ACTION="ServletCalculadora"><INPUT TYPE=TEXT SIZE=5 NAME=op1> + <INPUT TYPE=TEXT SIZE=5 NAME=op2><BR> <BR><INPUT TYPE=SUBMIT VALUE="Sumar"></FORM></HTML>

Genera la siguiente página :

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

1 (Muy Fácil) Escriba el servlet ServletCalculadora que alser contactadoResponda con la suma de ambos números

2 (Fácil) modifique el HTML de modo que la operación también sea ingresada por el usuario y el servlet haga la operación adecuada (solo se permite +, -, *, /

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

Other Input types we will use

• Radio: only one element between various alternatives can be chosen

• Select: like radiobutton but with puldown menu

• TextArea: like text but can contain many lines.

• Password: like text but does not show the content (***** instead of what you really input)

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

Radio<h2> Elija una laternativa </h2>

<HTML>

<input type=radio name=radio1 value=valor1> Alternativa 1 <br>

<input type=radio name=radio1 value=valor2> Alternativa 2 <br>

<input type=radio name=radio1 value=valor3> Alternativa 3 <br>

<input type=radio name=radio1 value=valor4> Alternativa 3 <br>

</HTML>

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

Radio<h2> Elija una laternativa </h2>

<HTML>

<input type=radio name=radio1 value=“valor1”> Uvas <br>

<input type=radio name=radio1 value=“valor2”> Peras <br>

<input type=radio name=radio1 value=“valor3”> Higos <br>

<input type=radio name=radio1 value=“valor4”> Mangos <br>

</HTML>

String alt = request.getParameter(“radio1”);

if (alt.equals(“valor1”)) out.println(“Ud. Eligió Uvas”);

else if (alt.equals(“valor2”)) out.println(“Ud. Eligió Peras”);

else if(alt.equals(“valor3”) out.println(“Ud. Eligió Higos”);

else out.println(“Ud. Eligió Mangos”);

Código para chequear cuálalternativa se seleccionó

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

HTMLSERVLET

Preview

Select(Elección entre varias alternativas con pul-down menu)

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

HTML

SERVLET<h2>Ingrese aqui su opinion </h2><TEXTAREA NAME=“Ta1" ROWS=10 COLS=40> lo que se excriba aca saldra en el area pero se puede editar</TEXTAREA>

String texto;texto = request.getParameter(“Ta1”);

Text Area