1 / 21

Multivalued parameters

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

Download Presentation

Multivalued parameters

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 333 123 lapiz 444 224 goma 555 322 papel OrderPage ProcessPage

  8. Auxiliary classes: Product 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; } } • First, a class for storing th information about the products

  9. Auxiliary classes: Item import java.util.Hashtable; import java.io.*; import java.util.*; public class Item { static public Hashtable getItems() throws Exception { Hashtable v = new Hashtable(); String l; Product p; p = new Product("1111","Honda",250); v.put("1111",p); p = new Product("2222","Mitsubishi",340); v.put("2222",p); p = new Product("4444","BMW",280); v.put("4444",p); p = new Product("5555","Mercedes",400); v.put("5555",p); p = new Product("6666","Ferrari",405); v.put("6666",p); return v; } }

  10. doGet of OrderPage Gets the hashtable with the products by calling the getItems method of the Item class protected void processRequest(HttpServletRequest request, …. PrintWriter out = response.getWriter(); Hashtable h = Item.getItems(); response.setContentType("text/html"); 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 keys = h.keys(); out.print("<form action=ProcessPage method='POST'>"); while(keys.hasMoreElements()) { String k = (String)keys.nextElement(); Product e = (Product)h.get(k); out.print("<TR>"); out.print("<TD>" + e.code); out.print("<TD>" + e.desc ); out.print("<TD>" + e.price+"<TD>"); out.print("<input type=text name="+e.code+" value=0 >"); } out.println("</TABLE>"); out.println("<INPUT TYPE='SUBMIT' VALUE='Process'>"); out.close(); }

  11. doGet of OrderPage protected void processRequest(HttpServletRequest request, …. PrintWriter out = response.getWriter(); Hashtable h = Item.getItems(); response.setContentType("text/html"); 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 keys = h.keys(); out.print("<form action=ProcessPage method='POST'>"); while(keys.hasMoreElements()) { String k = (String)keys.nextElement(); Product e = (Product)h.get(k); out.print("<TR>"); out.print("<TD>" + e.code); out.print("<TD>" + e.desc ); out.print("<TD>" + e.price+"<TD>"); out.print("<input type=text name="+e.code+" value=0 >"); } out.println("</TABLE>"); out.println("<INPUT TYPE='SUBMIT' VALUE='Process'>"); out.close(); } Title and header of the table

  12. doGet of OrderPage protected void processRequest(HttpServletRequest request, …. PrintWriter out = response.getWriter(); Hashtable h = Item.getItems(); response.setContentType("text/html"); 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 keys = h.keys(); out.print("<form action=ProcessPage method='POST'>"); while(keys.hasMoreElements()) { String k = (String)keys.nextElement(); Product e = (Product)h.get(k); out.print("<TR>"); out.print("<TD>" + e.code); out.print("<TD>" + e.desc ); out.print("<TD>" + e.price+"<TD>"); out.print("<input type=text name="+e.code+" value=0 >"); } out.println("</TABLE>"); out.println("<INPUT TYPE='SUBMIT' VALUE='Process'>"); out.close(); } Get the keys from the hashtable (codes of products)

  13. doGet of OrderPage public void doGet( .. request, ... response) throws . . . { protected void processRequest(HttpServletRequest request, …. PrintWriter out = response.getWriter(); Hashtable h = Item.getItems(); response.setContentType("text/html"); 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 keys = h.keys(); out.print("<form action=ProcessPage method='POST'>"); while(keys.hasMoreElements()) { String k = (String)keys.nextElement(); Product e = (Product)h.get(k); out.print("<TR>"); out.print("<TD>" + e.code); out.print("<TD>" + e.desc ); out.print("<TD>" + e.price+"<TD>"); out.print("<input type=text name="+e.code+" value=0 >"); } out.println("</TABLE>"); out.println("<INPUT TYPE='SUBMIT' VALUE='Process'>"); out.close(); } Iterate over the Enumeration object to obtain each element (a Product object)

  14. doGet of OrderPage protected void processRequest(HttpServletRequest request, …. PrintWriter out = response.getWriter(); Hashtable h = Item.getItems(); response.setContentType("text/html"); 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 keys = h.keys(); out.print("<form action=ProcessPage method='POST'>"); while(keys.hasMoreElements()) { String k = (String)keys.nextElement(); Product e = (Product)h.get(k); out.print("<TR>"); out.print("<TD>" + e.code); out.print("<TD>" + e.desc ); out.print("<TD>" + e.price+"<TD>"); out.print("<input type=text name="+e.code+" value=0 >"); } out.println("</TABLE>"); out.println("<INPUT TYPE='SUBMIT' VALUE='Process'>"); out.close(); } Show each field (number, name, price) Of the product in a table row

  15. doGet of OrderPage public void doGet( .. request, ... response) throws . . . { out.print("<input type=text name="+e.code+" value=0 >"); } This puts at the end of the row a text input Its namewill 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 . . . { PrintWriter out = response.getWriter(); Hashtable h = Item.getItems(); response.setContentType("text/html"); 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 nqtty = Integer.parseInt(qtty); Product e = (Product)h.get(number); out.print("<TR> <TD>" + e.code+"<TD>" + e.desc ); out.print("<TD>" + e.price+"<TD>"+e.price*nqtty); total = total+e.price*nqtty; } out.println("<TR> <TD> FINAL TOTAL<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=checkbox SIZE=3 "+ out.print(" name=selection value="+e.number+" >"); } out.println("</TABLE>"); out.println("<INPUT TYPE='SUBMIT' VALUE='Process'>"); }

  20. Selection 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> 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 >

  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(“selection”); 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'>"); }

More Related