Presentation is loading. Please wait.

Presentation is loading. Please wait.

Multivalued parameters

Similar presentations


Presentation on theme: "Multivalued parameters"— Presentation transcript:

1 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> <BR><input type=checkbox name=ide value=NB>NetBeans <BR><input type=checkbox name=ide value=EX>Eclipse <BR><input type=checkbox name=ide value=JW>JavaWorkShop <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

2 “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> <BR><input type=checkbox name=ide value=NB>NetBeans <BR><input type=checkbox name=ide value=EX>Eclipse <BR><input type=checkbox name=ide value=JW>JavaWorkShop <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

3 Retrieving the selected values
The selected values are retrieved in a String array with the following method getParameterValues(<parameter name>) applied on the request parameter of the get or put method. The signature is: String[] getParamterValues(String) String[] values = request.getParameterValues(“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

4 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 getPatameterNames() method applied to the request parameter, which will return an object of the Enumeration class Enumeration en = request.getParameterNames()

5 Using the Enumeration Object
To retrieve the value of the unknown we can iterate over the Enumeration object using the nextElement() and hasMoreElement() methods Enumeration en = request.getParameterNames(); while(en.hasMoreElements()) { String parameterName = (String)en.nextElement(); String parameterValue = request.getParameter(parameterName); out.println(“parametro “+parameterName+ “tiene valor “+parameterValue); }

6 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

7 Processing workflow Products.txt 111 34347 motores 222 760 escoba
lapiz goma papel OrderPage ProcessPage

8 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; }

9 Auxiliary classes: Item
import java.util.Hashtable; public class Item { static public Hastable getItems() { BufferedReader in = new BufferedReader( new FileReader(“Productos.txt”); Hashtable v = new Hashtable(); String l; while( (l= in.readLine()) != null) { int i = l.indexOf(“ “); int j = l.indexOf(“ “,i+1); String c = l.substring(0,i); String ps = l.substring(i+1,j); int pi = Integer.parseInt(ps); String d = l.substring(j+1); Product p = new Product(c,d,pi); v.put(code,p); } return p;

10 doGet of OrderPage Get the hashtable with the products
public void doGet( .. request, ... response) throws { Hashtable items = Item.getItems(); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.print("<H2 ALIGN=CENTER> Make your order</H2>\n" + "<TABLE BORDER=1 ALIGN=CENTER><TR BGCOLOR=#FFAD00>" + "<TH>Product Number <TH>Product Name<TH>Price <TH> Number"); Enumeration enum = h.getKeys(); 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'>"); Get the hashtable with the products by calling the getItems method of the Item class

11 doGet of OrderPage Title and header of the table
public void doGet( .. request, ... response) throws { Hashtable items = Item.getItems(); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.print("<H2 ALIGN=CENTER> Make your order</H2>\n" + "<TABLE BORDER=1 ALIGN=CENTER><TR BGCOLOR=#FFAD00>" + "<TH>Product Number <TH>Product Name<TH>Price <TH> Number"); Enumeration enum = h.getKeys(); 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'>"); Title and header of the table

12 doGet of OrderPage Get the keys from the hashtable (codes of products)
public void doGet( .. request, ... response) throws { Hashtable items = Item.getItems(); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.print("<H2 ALIGN=CENTER> Make your order</H2>\n" + "<TABLE BORDER=1 ALIGN=CENTER><TR BGCOLOR=#FFAD00>" + "<TH>Product Number <TH>Product Name<TH>Price <TH> Number"); Enumeration enum = h.getKeys(); 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'>"); Get the keys from the hashtable (codes of products)

13 doGet of OrderPage Iterate over the Enumeration object to
public void doGet( .. request, ... response) throws { Hashtable items = Item.getItems(); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.print("<H2 ALIGN=CENTER> Make your order</H2>\n" + "<TABLE BORDER=1 ALIGN=CENTER><TR BGCOLOR=#FFAD00>" + "<TH>Product Number <TH>Product Name<TH>Price <TH> Number"); Enumeration enum = h.getKeys(); 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'>"); Iterate over the Enumeration object to obtain each element (a Product object)

14 doGet of OrderPage Show each field (number, name, price)
public void doGet( .. request, ... response) throws { Hashtable items = Item.getItems(); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.print("<H2 ALIGN=CENTER> Make your order</H2>\n" + "<TABLE BORDER=1 ALIGN=CENTER><TR BGCOLOR=#FFAD00>" + "<TH>Product Number <TH>Product Name<TH>Price <TH> Number"); Enumeration enum = h.getKeys(); 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'>"); Show each field (number, name, price) Of the product in a table row

15 doGet of OrderPage This puts at the end of the row a text input
public void doGet( .. 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

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

17 Now lets generate the following page with ProcessOrder servlet

18 doGet of ProcessOrder public void doGet( .. request, ... response) throws { Hashtable items = Item.getItems(); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.print("<H2 ALIGN=CENTER> Your choice was</H2>\n" + "<TABLE BORDER=1 ALIGN=CENTER><TR BGCOLOR=#FFAD00>" + "<TH>Product Number <TH>Product Name<TH>Price <TH> Total"); 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); 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> <TD> <TD> <TD>”+total); out.println("</TABLE>"); out.println("<INPUT TYPE='SUBMIT' VALUE='Process'>");

19 The same with checkbox public void doGet( .. request, ... response) throws { Hashtable items = Item.getItems(); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.print("<H2 ALIGN=CENTER> Make your order</H2>\n" + "<TABLE BORDER=1 ALIGN=CENTER><TR BGCOLOR=#FFAD00>" + "<TH>Product Number <TH>Product Name<TH>Price <TH> Number"); Enumeration enum = h.getKeys(); 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=chackbox SIZE=3 "+ out.print(" name=selection value="+e.number+" >"); } out.println("</TABLE>"); out.println("<INPUT TYPE='SUBMIT' VALUE='Process'>");

20 Selecttion means buy only 1 item
<H2 ALIGN=CENTER> Make your order</H2> <TABLE BORDER=1 ALIGN=CENTER><TR BGCOLOR=#FFAD00> <TH>Product Number <TH>Product Name<TH>Price <TH> Number <TR><TD> 1111 <TD> motores <TD> <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 >

21 ProcessOrder for checkbox
public void doGet( .. request, ... response) throws { Hashtable items = Item.getItems(); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.print("<H2 ALIGN=CENTER> Your choice was</H2>\n" + "<TABLE BORDER=1 ALIGN=CENTER><TR BGCOLOR=#FFAD00>" + "<TH>Product Number <TH>Product Name<TH>Price <TH> Total"); String[] values = request.getParameterValues(“ide”); out.print("<form action=ProcessPayment 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> <TD> <TD> <TD>”+total); out.println("</TABLE>"); out.println("<INPUT TYPE='SUBMIT' VALUE='Process'>");


Download ppt "Multivalued parameters"

Similar presentations


Ads by Google