Multivalued parameters Some type of parameters may have

  • Slides: 21
Download presentation
Multivalued parameters • Some type of parameters may have more than one value. This

Multivalued parameters • Some type of parameters may have more than one value. This is the case for the checkbox. What IDEs do you use? <BR><input type=checkbox name=ide value=NB>Net. Beans <BR><input type=checkbox name=ide value=EX>Eclipse <BR><input type=checkbox name=ide value=JW>Java. Work. Shop <BR><input type=checkbox name=ide value=JP>J++ <BR><input type=checkbox name=ide value=CF>Cafe' This HTML code will genrate this output on the web-browser

“ide” will be a mutivalued parameter • The parameter “ide” <name of the input>

“ide” will be a mutivalued parameter • The parameter “ide” <name of the input> will receive as many values as options are selected (from 0 to 5 in this case) What IDEs do you use? <BR><input type=checkbox name=ide value=NB>Net. Beans <BR><input type=checkbox name=ide value=EX>Eclipse <BR><input type=checkbox name=ide value=JW>Java. Work. Shop <BR><input type=checkbox name=ide value=JP>J++ <BR><input type=checkbox name=ide value=CF>Cafe' These are the values the Parameter “ide” will receive If the option is selected

Retrieving the selected values • The selected values are retrieved in a String array

Retrieving the selected values • The selected values are retrieved in a String array with the following method get. Parameter. Values(<parameter name>) applied on the request parameter of the get or put method. The signature is: String[] get. Paramter. Values(String) String[] values = request. get. Parameter. Values(“ide”); for (int i = 0; i < values. length; i++) out. println(i+” “+values[i]); This will print 1 - NB 2 - EX and 3 - JP in this case

Retrieving All parameters when names are not known • Sometimes the programmer may not

Retrieving All parameters when names are not known • Sometimes the programmer may not know the names of all parameters which will be passed to the Servlet • For example when the page was dynamically generated by another servlet according to the results of a query in database or the content of a file • In these cases we can use the get. Patameter. Names() method applied to the request parameter, which will return an object of the Enumeration class • Enumeration en = request. get. Parameter. Names()

Using the Enumeration Object • To retrieve the value of the unknown we can

Using the Enumeration Object • To retrieve the value of the unknown we can iterate over the Enumeration object using the next. Element() and has. More. Element() methods Enumeration en = request. get. Parameter. Names(); while(en. has. More. Elements()) { String parameter. Name = (String)en. next. Element(); String parameter. Value = request. get. Parameter(parameter. Name); out. println(“parametro “+parameter. Name+ “tiene valor “+parameter. Value); }

Example: products in a file • The information about products to buy is stored

Example: products in a file • The information about products to buy is stored in a file. In each line there is a product code, price and description (separated with a space). • A first servlet should show this and allow users to specify a quantity to buy • After the user specifies the quantities, presses a button to see the total • After pressing the button, a second servlet will receive the information and process it to show the total amount the user has to pay

Processing workflow Products. txt 111 34347 motores 222 760 escoba 333 123 lapiz 444

Processing workflow Products. txt 111 34347 motores 222 760 escoba 333 123 lapiz 444 224 goma 555 322 papel Order. Page Process. Page

Auxiliary classes: Product • First, a class for storing th information about the products

Auxiliary classes: Product • First, a class for storing th information about the products public class Product { public String code; public int price; public String desc; Product(String x, String y, int z){ code = x; desc = y; price = z; } }

Auxiliary classes: Item import java. util. Hashtable; public class Item { static public Hastable

Auxiliary classes: Item import java. util. Hashtable; public class Item { static public Hastable get. Items() { Buffered. Reader in = new Buffered. Reader( new File. Reader(“Productos. txt”); Hashtable v = new Hashtable(); String l; while( (l= in. read. Line()) != null) { int i = l. index. Of(“ “); int j = l. index. Of(“ “, i+1); String c = l. substring(0, i); String ps = l. substring(i+1, j); int pi = Integer. parse. Int(ps); String d = l. substring(j+1); Product p = new Product(c, d, pi); v. put(code, p); } return p; } }

do. Get of Order. Page public void do. Get(. . request, . . .

do. Get of Order. Page public void do. Get(. . request, . . . response) throws. . . { Hashtable items = Item. get. Items(); response. set. Content. Type("text/html"); Get the hashtable with the products Print. Writer out = response. get. Writer(); by calling theyour get. Items method of+ out. print("<H 2 ALIGN=CENTER> Make order</H 2>n" "<TABLE BORDER=1 ALIGN=CENTER><TR the Item class. BGCOLOR=#FFAD 00>" + "<TH>Product Number <TH>Product Name<TH>Price <TH> Number"); Enumeration enum = h. get. Keys(); out. print("<form action=Process. Page method='POST'>"); while(enum. has. More. Elements()) { Product e = (Product)enum. next. Element(); 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'>"); }

do. Get of Order. Page public void do. Get(. . request, . . .

do. Get of Order. Page public void do. Get(. . request, . . . response) throws. . . { Hashtable items = Item. get. Items(); response. set. Content. Type("text/html"); Print. Writer out = response. get. Writer(); out. print("<H 2 ALIGN=CENTER> Make your order</H 2>n" + "<TABLE BORDER=1 ALIGN=CENTER><TR BGCOLOR=#FFAD 00>" + "<TH>Product Number <TH>Product Name<TH>Price <TH> Number"); Enumeration enum = h. get. Keys(); Title and header of the table out. print("<form action=Process. Page method='POST'>"); while(enum. has. More. Elements()) { Product e = (Product)enum. next. Element(); 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'>"); }

do. Get of Order. Page public void do. Get(. . request, . . .

do. Get of Order. Page public void do. Get(. . request, . . . response) throws. . . { Hashtable items = Item. get. Items(); response. set. Content. Type("text/html"); Print. Writer out = response. get. Writer(); out. print("<H 2 ALIGN=CENTER> Make your order</H 2>n" + "<TABLE BORDER=1 ALIGN=CENTER><TR BGCOLOR=#FFAD 00>" + "<TH>Product Number <TH>Product Name<TH>Price <TH> Number"); Enumeration enum = h. get. Keys(); out. print("<form action=Process. Page method='POST'>"); while(enum. has. More. Elements()) { Get the keys from the hashtable Product e = (Product)enum. next. Element(); out. print("<TR>"); (codes of products) 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'>"); }

do. Get of Order. Page public void do. Get(. . request, . . .

do. Get of Order. Page public void do. Get(. . request, . . . response) throws. . . { Hashtable items = Item. get. Items(); response. set. Content. Type("text/html"); Print. Writer out = response. get. Writer(); out. print("<H 2 ALIGN=CENTER> Make your order</H 2>n" + "<TABLE BORDER=1 ALIGN=CENTER><TR BGCOLOR=#FFAD 00>" + Iterate over the Enumeration object to "<TH>Product Number <TH>Product Name<TH>Price <TH> Number"); obtain each element (a Product object) Enumeration enum = h. get. Keys(); out. print("<form action=Process. Page method='POST'>"); while(enum. has. More. Elements()) { Product e = (Product)enum. next. Element(); 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'>"); }

do. Get of Order. Page public void do. Get(. . request, . . .

do. Get of Order. Page public void do. Get(. . request, . . . response) throws. . . { Hashtable items = Item. get. Items(); response. set. Content. Type("text/html"); Print. Writer out = response. get. Writer(); out. print("<H 2 ALIGN=CENTER> Make your order</H 2>n" + "<TABLE BORDER=1 ALIGN=CENTER><TR BGCOLOR=#FFAD 00>" + Show each field (number, name, price) "<TH>Product Number <TH>Product Name<TH>Price <TH> Number"); Enumeration enumin=a h. get. Keys(); Of the product table row out. print("<form action=Process. Page method='POST'>"); while(enum. has. More. Elements()) { Product e = (Product)enum. next. Element(); 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'>"); }

do. Get of Order. Page public void do. Get(. . request, . . .

do. Get of Order. Page public void do. Get(. . request, . . . response) throws. . . { out. print("<input type=text SIZE=3 "+ out. print(" name="+e. number+" value=0 >"); } This puts at the end of the row a text input Its name will be the number (code) of the product

The name of the input field is the product’s code

The name of the input field is the product’s code

Now lets generate the following page with Process. Order servlet

Now lets generate the following page with Process. Order servlet

do. Get of Process. Order public void do. Get(. . request, . . .

do. Get of Process. Order public void do. Get(. . request, . . . response) throws. . . { Hashtable items = Item. get. Items(); response. set. Content. Type("text/html"); Print. Writer out = response. get. Writer(); out. print("<H 2 ALIGN=CENTER> Your choice was</H 2>n" + "<TABLE BORDER=1 ALIGN=CENTER><TR BGCOLOR=#FFAD 00>" + "<TH>Product Number <TH>Product Name<TH>Price <TH> Total"); Enumeration en = request. get. Parameter. Names(); int total = 0; out. print("<form action=Process. Payment method='POST'>"); while(en. has. More. Elements()) { String number = (String)en. next. Element(); String qtty = request. get. Parameter(number); int nqqty = Integer. parce. Int(qtty); Product e = (Product)item. get(number); out. print("<TR> <TD>" + e. number+"<TD>" + e. name ); out. print("<TD>" + e. price+"<TD>“+e. price*nqtty); total = total*e. price*nqtty; } out. println(“<TR> <TD>”+total); out. println("</TABLE>"); out. println("<INPUT TYPE='SUBMIT' VALUE='Process'>"); }

The same with checkbox public void do. Get(. . request, . . . response)

The same with checkbox public void do. Get(. . request, . . . response) throws. . . { Hashtable items = Item. get. Items(); response. set. Content. Type("text/html"); Print. Writer out = response. get. Writer(); out. print("<H 2 ALIGN=CENTER> Make your order</H 2>n" + "<TABLE BORDER=1 ALIGN=CENTER><TR BGCOLOR=#FFAD 00>" + "<TH>Product Number <TH>Product Name<TH>Price <TH> Number"); Enumeration enum = h. get. Keys(); out. print("<form action=Process. Page method='POST'>"); while(enum. has. More. Elements()) { Product e = (Product)enum. next. Element(); out. print("<TR>"); out. print("<TD>" + e. number); out. print("<TD>" + e. name ); out. print("<TD>" + e. price+"<TD>"); out. print("<input type=chackbox SIZE=3 "+ out. print(" name=selection value="+e. number+" >"); } out. println("</TABLE>"); out. println("<INPUT TYPE='SUBMIT' VALUE='Process'>"); }

Selecttion means buy only 1 item <H 2 ALIGN=CENTER> Make your order</H 2> <TABLE

Selecttion means buy only 1 item <H 2 ALIGN=CENTER> Make your order</H 2> <TABLE BORDER=1 ALIGN=CENTER><TR BGCOLOR=#FFAD 00> <TH>Product Number <TH>Product Name<TH>Price <TH> Number <TR><TD> 1111 <TD> motores <TD> 34347 <TD> <input type=checkbox name=selection value=1111 > <TR><TD> 2222 <TD> escoba <TD> 760 <TD> <input type=checkbox name=selection value=2222 > <TR><TD> 3333 <TD> lapiz <TD> 1237 <TD> <input type=checkbox name=selection value=3333 > <TR><TD> 4444 <TD> goma <TD> 224 <TD> <input type=checkbox name=selection value=4444 > <TR><TD> 5555 <TD> papel <TD> 322 <TD> <input type=checkbox name=selection value=5555 >

Process. Order for checkbox public void do. Get(. . request, . . . response)

Process. Order for checkbox public void do. Get(. . request, . . . response) throws. . . { Hashtable items = Item. get. Items(); response. set. Content. Type("text/html"); Print. Writer out = response. get. Writer(); out. print("<H 2 ALIGN=CENTER> Your choice was</H 2>n" + "<TABLE BORDER=1 ALIGN=CENTER><TR BGCOLOR=#FFAD 00>" + "<TH>Product Number <TH>Product Name<TH>Price <TH> Total"); String[] values = request. get. Parameter. Values(“ide”); out. print("<form action=Process. Payment method='POST'>"); for (int i = 0; i < values. length; i++){ String number = values[i]; Product e = (Product)item. get(number); out. print("<TR> <TD>" + e. number+"<TD>" + e. name ); out. print("<TD>" + e. price+"<TD>“+e. price*nqtty); total = total*e. price*nqtty; } out. println(“<TR> <TD>”+total); out. println("</TABLE>"); out. println("<INPUT TYPE='SUBMIT' VALUE='Process'>"); }