Multivalued parameters - PowerPoint PPT Presentation

About This Presentation
Title:

Multivalued parameters

Description:

This is the case for the checkbox. What IDEs do you use? BR ... What IDEs do you use? BR BR input type=checkbox name=ide value=NB NetBeans ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 22
Provided by: dccUc
Category:

less

Transcript and Presenter's Notes

Title: Multivalued parameters


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?ltBRgt
ltBRgtltinput typecheckbox nameide
valueNBgtNetBeans ltBRgtltinput
typecheckbox nameide valueEXgtEclipse
ltBRgtltinput typecheckbox nameide
valueJWgtJavaWorkShop ltBRgtltinput
typecheckbox nameide valueJPgtJ
ltBRgtltinput typecheckbox nameide valueCFgtCafe'
This HTML code will genrate this output on the
web-browser
2
ide will be a mutivalued parameter
  • The parameter ide ltname of the inputgt will
    receive as many values as options are selected
    (from 0 to 5 in this case)

What IDEs do you use?ltBRgt
ltBRgtltinput typecheckbox nameide
valueNBgtNetBeans ltBRgtltinput
typecheckbox nameide valueEXgtEclipse
ltBRgtltinput typecheckbox nameide
valueJWgtJavaWorkShop ltBRgtltinput
typecheckbox nameide valueJPgtJ
ltBRgtltinput typecheckbox nameide valueCFgtCafe'
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(ltparameter namegt)
  • 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 lt values.length i)
out.println(i valuesi)
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
  • 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( ,i1)
  • String c l.substring(0,i)
  • String ps l.substring(i1,j)
  • int pi Integer.parseInt(ps)
  • String d l.substring(j1)
  • Product p new Product(c,d,pi)
  • v.put(code,p)
  • return p

10
doGet of OrderPage
  • public void doGet( .. request, ... response)
    throws . . .
  • Hashtable items Item.getItems()
  • response.setContentType("text/html")
  • PrintWriter out response.getWriter()
  • out.print("ltH2 ALIGNCENTERgt Make your
    orderlt/H2gt\n"
  • "ltTABLE BORDER1 ALIGNCENTERgtltTR
    BGCOLORFFAD00gt"
  • "ltTHgtProduct Number ltTHgtProduct NameltTHgtPrice
    ltTHgt Number")
  • Enumeration enum h.getKeys()
  • out.print("ltform actionProcessPage
    method'POST'gt")
  • while(enum.hasMoreElements())
  • Product e (Product)enum.nextElement()
  • out.print("ltTRgt")
  • out.print("ltTDgt" e.number)
  • out.print("ltTDgt" e.name )
  • out.print("ltTDgt" e.price"ltTDgt")
  • out.print("ltinput typetextarea SIZE3 "
  • out.print(" name"e.number" value0 gt")
  • out.println("lt/TABLEgt")

Get the hashtable with the products by calling
the getItems method of the Item class
11
doGet of OrderPage
  • public void doGet( .. request, ... response)
    throws . . .
  • Hashtable items Item.getItems()
  • response.setContentType("text/html")
  • PrintWriter out response.getWriter()
  • out.print("ltH2 ALIGNCENTERgt Make your
    orderlt/H2gt\n"
  • "ltTABLE BORDER1 ALIGNCENTERgtltTR
    BGCOLORFFAD00gt"
  • "ltTHgtProduct Number ltTHgtProduct NameltTHgtPrice
    ltTHgt Number")
  • Enumeration enum h.getKeys()
  • out.print("ltform actionProcessPage
    method'POST'gt")
  • while(enum.hasMoreElements())
  • Product e (Product)enum.nextElement()
  • out.print("ltTRgt")
  • out.print("ltTDgt" e.number)
  • out.print("ltTDgt" e.name )
  • out.print("ltTDgt" e.price"ltTDgt")
  • out.print("ltinput typetextarea SIZE3 "
  • out.print(" name"e.number" value0 gt")
  • out.println("lt/TABLEgt")

Title and header of the table
12
doGet of OrderPage
  • public void doGet( .. request, ... response)
    throws . . .
  • Hashtable items Item.getItems()
  • response.setContentType("text/html")
  • PrintWriter out response.getWriter()
  • out.print("ltH2 ALIGNCENTERgt Make your
    orderlt/H2gt\n"
  • "ltTABLE BORDER1 ALIGNCENTERgtltTR
    BGCOLORFFAD00gt"
  • "ltTHgtProduct Number ltTHgtProduct NameltTHgtPrice
    ltTHgt Number")
  • Enumeration enum h.getKeys()
  • out.print("ltform actionProcessPage
    method'POST'gt")
  • while(enum.hasMoreElements())
  • Product e (Product)enum.nextElement()
  • out.print("ltTRgt")
  • out.print("ltTDgt" e.number)
  • out.print("ltTDgt" e.name )
  • out.print("ltTDgt" e.price"ltTDgt")
  • out.print("ltinput typetextarea SIZE3 "
  • out.print(" name"e.number" value0 gt")
  • out.println("lt/TABLEgt")

Get the keys from the hashtable (codes of
products)
13
doGet of OrderPage
  • public void doGet( .. request, ... response)
    throws . . .
  • Hashtable items Item.getItems()
  • response.setContentType("text/html")
  • PrintWriter out response.getWriter()
  • out.print("ltH2 ALIGNCENTERgt Make your
    orderlt/H2gt\n"
  • "ltTABLE BORDER1 ALIGNCENTERgtltTR
    BGCOLORFFAD00gt"
  • "ltTHgtProduct Number ltTHgtProduct NameltTHgtPrice
    ltTHgt Number")
  • Enumeration enum h.getKeys()
  • out.print("ltform actionProcessPage
    method'POST'gt")
  • while(enum.hasMoreElements())
  • Product e (Product)enum.nextElement()
  • out.print("ltTRgt")
  • out.print("ltTDgt" e.number)
  • out.print("ltTDgt" e.name )
  • out.print("ltTDgt" e.price"ltTDgt")
  • out.print("ltinput typetextarea SIZE3 "
  • out.print(" name"e.number" value0 gt")
  • out.println("lt/TABLEgt")

Iterate over the Enumeration object to obtain
each element (a Product object)
14
doGet of OrderPage
  • public void doGet( .. request, ... response)
    throws . . .
  • Hashtable items Item.getItems()
  • response.setContentType("text/html")
  • PrintWriter out response.getWriter()
  • out.print("ltH2 ALIGNCENTERgt Make your
    orderlt/H2gt\n"
  • "ltTABLE BORDER1 ALIGNCENTERgtltTR
    BGCOLORFFAD00gt"
  • "ltTHgtProduct Number ltTHgtProduct NameltTHgtPrice
    ltTHgt Number")
  • Enumeration enum h.getKeys()
  • out.print("ltform actionProcessPage
    method'POST'gt")
  • while(enum.hasMoreElements())
  • Product e (Product)enum.nextElement()
  • out.print("ltTRgt")
  • out.print("ltTDgt" e.number)
  • out.print("ltTDgt" e.name )
  • out.print("ltTDgt" e.price"ltTDgt")
  • out.print("ltinput typetextarea SIZE3 "
  • out.print(" name"e.number" value0 gt")
  • out.println("lt/TABLEgt")

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("ltinput typetext SIZE3 "
  • out.print(" name"e.number" value0 gt")

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 products 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("ltH2 ALIGNCENTERgt Your choice
    waslt/H2gt\n"
  • "ltTABLE BORDER1 ALIGNCENTERgtltTR
    BGCOLORFFAD00gt"
  • "ltTHgtProduct Number ltTHgtProduct NameltTHgtPrice
    ltTHgt Total")
  • Enumeration en request.getParameterNames()
    int total 0
  • out.print("ltform actionProcessPayment
    method'POST'gt")
  • 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("ltTRgt ltTDgt" e.number"ltTDgt"
    e.name )
  • out.print("ltTDgt" e.price"ltTDgte.pricenq
    tty)
  • total totale.pricenqtty
  • out.println(ltTRgt ltTDgt ltTDgt ltTDgt ltTDgttotal)

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("ltH2 ALIGNCENTERgt Make your
    orderlt/H2gt\n"
  • "ltTABLE BORDER1 ALIGNCENTERgtltTR
    BGCOLORFFAD00gt"
  • "ltTHgtProduct Number ltTHgtProduct NameltTHgtPrice
    ltTHgt Number")
  • Enumeration enum h.getKeys()
  • out.print("ltform actionProcessPage
    method'POST'gt")
  • while(enum.hasMoreElements())
  • Product e (Product)enum.nextElement()
  • out.print("ltTRgt")
  • out.print("ltTDgt" e.number)
  • out.print("ltTDgt" e.name )
  • out.print("ltTDgt" e.price"ltTDgt")
  • out.print("ltinput typechackbox SIZE3 "
  • out.print(" nameselection
    value"e.number" gt")
  • out.println("lt/TABLEgt")

20
Selecttion means buy only 1 item
ltH2 ALIGNCENTERgt Make your orderlt/H2gt ltTABLE
BORDER1 ALIGNCENTERgtltTR BGCOLORFFAD00gt ltTHgtPro
duct Number ltTHgtProduct NameltTHgtPrice ltTHgt
Number ltTRgtltTDgt 1111 ltTDgt motores ltTDgt 34347
ltTDgt ltinput typecheckbox nameselection
value1111 gt ltTRgtltTDgt 2222 ltTDgt escoba ltTDgt 760
ltTDgt ltinput typecheckbox nameselection
value2222 gt ltTRgtltTDgt 3333 ltTDgt lapiz ltTDgt 1237
ltTDgt ltinput typecheckbox nameselection
value3333 gt ltTRgtltTDgt 4444 ltTDgt goma ltTDgt 224
ltTDgt ltinput typecheckbox nameselection
value4444 gt ltTRgtltTDgt 5555 ltTDgt papel ltTDgt 322
ltTDgt ltinput typecheckbox nameselection
value5555 gt
21
ProcessOrder for checkbox
  • public void doGet( .. request, ... response)
    throws . . .
  • Hashtable items Item.getItems()
  • response.setContentType("text/html")
  • PrintWriter out response.getWriter()
  • out.print("ltH2 ALIGNCENTERgt Your choice
    waslt/H2gt\n"
  • "ltTABLE BORDER1 ALIGNCENTERgtltTR
    BGCOLORFFAD00gt"
  • "ltTHgtProduct Number ltTHgtProduct NameltTHgtPrice
    ltTHgt Total")
  • String values request.getParameterValues(i
    de)
  • out.print("ltform actionProcessPayment
    method'POST'gt")
  • for (int i 0 i lt values.length i)
  • String number valuesi
  • Product e (Product)item.get(number)
  • out.print("ltTRgt ltTDgt" e.number"ltTDgt"
    e.name )
  • out.print("ltTDgt" e.price"ltTDgte.pricenq
    tty)
  • total totale.pricenqtty
  • out.println(ltTRgt ltTDgt ltTDgt ltTDgt ltTDgttotal)
  • out.println("lt/TABLEgt")
  • out.println("ltINPUT TYPE'SUBMIT'
    VALUE'Process'gt")
Write a Comment
User Comments (0)
About PowerShow.com