Presentation is loading. Please wait.

Presentation is loading. Please wait.

Multivalued parameters Some type of parameters may have more than one value. This is the case for the checkbox. What IDEs do you use? NetBeans Eclipse.

Similar presentations


Presentation on theme: "Multivalued parameters Some type of parameters may have more than one value. This is the case for the checkbox. What IDEs do you use? NetBeans Eclipse."— 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? NetBeans Eclipse JavaWorkShop J++ Cafe' This HTML code will genrate this output on the web-browser

2 “ide” will be a mutivalued parameter The parameter “ide” will receive as many values as options are selected (from 0 to 5 in this case) What IDEs do you use? NetBeans Eclipse JavaWorkShop J++ 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( ) 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 motores escoba lapiz goma 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 protected void processRequest(HttpServletRequest request, …. PrintWriter out = response.getWriter(); Hashtable h = Item.getItems(); response.setContentType("text/html"); out.print(" Make your order \n" + " " + " Product Number Product Name Price Number"); Enumeration keys = h.keys(); out.print(" "); while(keys.hasMoreElements()) { String k = (String)keys.nextElement(); Product e = (Product)h.get(k); out.print(" "); out.print(" " + e.code); out.print(" " + e.desc ); out.print(" " + e.price+" "); out.print(" "); } out.println(" "); out.close(); } Gets the hashtable with the products by calling the getItems method of the Item class

11 doGet of OrderPage protected void processRequest(HttpServletRequest request, …. PrintWriter out = response.getWriter(); Hashtable h = Item.getItems(); response.setContentType("text/html"); out.print(" Make your order \n" + " " + " Product Number Product Name Price Number"); Enumeration keys = h.keys(); out.print(" "); while(keys.hasMoreElements()) { String k = (String)keys.nextElement(); Product e = (Product)h.get(k); out.print(" "); out.print(" " + e.code); out.print(" " + e.desc ); out.print(" " + e.price+" "); out.print(" "); } out.println(" "); 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(" Make your order \n" + " " + " Product Number Product Name Price Number"); Enumeration keys = h.keys(); out.print(" "); while(keys.hasMoreElements()) { String k = (String)keys.nextElement(); Product e = (Product)h.get(k); out.print(" "); out.print(" " + e.code); out.print(" " + e.desc ); out.print(" " + e.price+" "); out.print(" "); } out.println(" "); 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(" Make your order \n" + " " + " Product Number Product Name Price Number"); Enumeration keys = h.keys(); out.print(" "); while(keys.hasMoreElements()) { String k = (String)keys.nextElement(); Product e = (Product)h.get(k); out.print(" "); out.print(" " + e.code); out.print(" " + e.desc ); out.print(" " + e.price+" "); out.print(" "); } out.println(" "); 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(" Make your order \n" + " " + " Product Number Product Name Price Number"); Enumeration keys = h.keys(); out.print(" "); while(keys.hasMoreElements()) { String k = (String)keys.nextElement(); Product e = (Product)h.get(k); out.print(" "); out.print(" " + e.code); out.print(" " + e.desc ); out.print(" " + e.price+" "); out.print(" "); } out.println(" "); 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(""); } 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... { PrintWriter out = response.getWriter(); Hashtable h = Item.getItems(); response.setContentType("text/html"); out.print(" Your choice was \n" + " " + " Product Number Product Name Price Total"); Enumeration en = request.getParameterNames(); int total = 0; out.print(" "); 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(" " + e.code+" " + e.desc ); out.print(" " + e.price+" "+e.price*nqtty); total = total+e.price*nqtty; } out.println(" FINAL TOTAL "+total); out.println(" "); }

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(" Make your order \n" + " " + " Product Number Product Name Price Number"); Enumeration enum = h.getKeys(); out.print(" "); while(enum.hasMoreElements()) { Product e = (Product)enum.nextElement(); out.print(" "); out.print(" " + e.number); out.print(" " + e.name ); out.print(" " + e.price+" "); out.print(""); } out.println(" "); }

20 Selection means buy only 1 item Make your order Product Number Product Name Price Number 1111 motores escoba lapiz goma papel 322

21 ProcessOrder for checkbox public void doGet(.. request,... response) throws... { Hashtable items = Item.getItems(); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.print(" Your choice was \n" + " " + " Product Number Product Name Price Total"); String[] values = request.getParameterValues(“selection”); out.print(" "); for (int i = 0; i < values.length; i++){ String number = values[i]; Product e = (Product)item.get(number); out.print(" " + e.number+" " + e.name ); out.print(" " + e.price+" “+e.price*nqtty); total = total*e.price*nqtty; } out.println(“ ”+total); out.println(" "); }


Download ppt "Multivalued parameters Some type of parameters may have more than one value. This is the case for the checkbox. What IDEs do you use? NetBeans Eclipse."

Similar presentations


Ads by Google