"Hello "+fname+" “+lname "Hello "+fname+" “+lname

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Parameters passed by client

Similar presentations


Presentation on theme: "Parameters passed by client"— Presentation transcript:

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 server The client can pass parameters with the request according to the following format 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)

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

3 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=firstname><BR> Apellido: <INPUT TYPE=TEXT NAME=lastname><BR> <INPUT TYPE=SUBMIT VALUE=“MANDAR”> </HTML>

4 <H1> Collecting parameters</H1>
<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> define the beginning and end of a Formulary which will be filled in order to transfer data to the server ACTION= “…” defines the action that will be taken when the input button is pressed In this case, the URL that will be called

5 <H1> Collecting parameters</H1>
<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 … defines an input (interaction) element This element will be transfered as a parameter to the server with the URL TYPE defines the type of element (TEXT) NAME defines the name of the element, which will also be transfered

6 <H1> Collecting parameters</H1>
<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 defines a button element which will trigger the ACTION defined VALUE=“……” label of the button

7 When pressing the button the URL
Shown will be sent The parameters will be automatically added

8 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)

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

10 Radio Código para chequear cuál alternativa se seleccionó
<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> Código para chequear cuál alternativa se seleccionó 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”);

11 (Elección entre varias alternativas con pul-down menu)
Select (Elección entre varias alternativas con pul-down menu) HTML SERVLET Preview

12 Text Area 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> SERVLET String texto; texto = request.getParameter(“Ta1”);


Download ppt "Parameters passed by client"

Similar presentations


Ads by Google