Presentation is loading. Please wait.

Presentation is loading. Please wait.

Retrieving All parameters when names are not known

Similar presentations


Presentation on theme: "Retrieving All parameters when names are not known"— Presentation transcript:

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

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

3 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

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

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

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

7 doGet of OrderPage Gets the hashtable with the products
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(); Gets the hashtable with the products by calling the getItems method of the Item class

8 doGet of OrderPage Title and header of the table
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

9 doGet of OrderPage Get the keys from the hashtable (codes of products)
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)

10 doGet of OrderPage Iterate over the Enumeration object to
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)

11 doGet of OrderPage Show each field (number, name, price)
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

12 doGet of OrderPage name="+e.code+" value=0 >");
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 name will be the number (code) of the product

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

14 Now lets generate the following page with ProcessOrder servlet

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

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

17 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> <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 >

18 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 "Retrieving All parameters when names are not known"

Similar presentations


Ads by Google