+ All Categories
Home > Documents > Multivalued parameters

Multivalued parameters

Date post: 03-Jan-2016
Category:
Upload: axel-copeland
View: 32 times
Download: 0 times
Share this document with a friend
Description:
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 - PowerPoint PPT Presentation
21
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-browse
Transcript
Page 1: 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 <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

Page 2: Multivalued parameters

“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

Page 3: Multivalued parameters

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

Page 4: Multivalued parameters

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

Page 5: Multivalued parameters

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

}

Page 6: Multivalued parameters

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

Page 7: Multivalued parameters

Processing workflow

Products.txt

111 34347 motores222 760 escoba333 123 lapiz444 224 goma555 322 papel

OrderPage

ProcessPage

Page 8: Multivalued parameters

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

Page 9: Multivalued parameters

Auxiliary classes: Itemimport 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; }}

Page 10: Multivalued parameters

doGet of OrderPageprotected 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(); }

Gets the hashtable with the productsby calling the getItems method of the Item class

Page 11: Multivalued parameters

doGet of OrderPageprotected 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

Page 12: Multivalued parameters

doGet of OrderPageprotected 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)

Page 13: Multivalued parameters

doGet of OrderPagepublic 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)

Page 14: Multivalued parameters

doGet of OrderPageprotected 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

Page 15: Multivalued parameters

doGet of OrderPagepublic 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 inputIts name will be the number (code) of the product

Page 16: Multivalued parameters

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

Page 17: Multivalued parameters

Now lets generate the following page with ProcessOrder servlet

Page 18: Multivalued parameters

doGet of ProcessOrderpublic 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'>");}

Page 19: Multivalued parameters

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'>");}

Page 20: Multivalued parameters

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 >

Page 21: Multivalued parameters

ProcessOrder for checkboxpublic 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'>");}


Recommended