SunQuest
 
       Java
  Home arrow Java arrow Page 8 - The JSP Files (part 4): The Red Pill
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
Moblin 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

The JSP Files (part 4): The Red Pill
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 6
    2001-03-07

    Table of Contents:
  • The JSP Files (part 4): The Red Pill
  • The Last Action Hero
  • Entering The Matrix
  • Requesting More
  • Taking Some Medication
  • What's For Dessert?
  • A Chocolate Addiction
  • Couch Potato
  • Beating It Into Submission

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    The JSP Files (part 4): The Red Pill - Couch Potato


    (Page 8 of 9 )

    Arrays come in particularly handy when dealing with form elements like checkboxes and multiple select list boxes. Take a look at the following form, which includes a bunch of checkboxes:


    <html> <head> <basefont face="Arial"> </head> <body> Pick your favourite shows: <br> <form action="potato.jsp" method="POST"> <input type="checkbox" name="shows" value="Ally McBeal">Ally McBeal <input type="checkbox" name="shows" value="Buffy The Vampire Slayer">Buffy The Vampire Slayer <input type="checkbox" name="shows" value="The Practice">The Practice <input type="checkbox" name="shows" value="Sex And The City">Sex And The City <input type="checkbox" name="shows" value="The Sopranos">The Sopranos <input type="checkbox" name="shows" value="Survivor">Survivor <br> <input type="submit" name="submit" value="Select"> </form> </body> </html>
    Now, once the form is submitted, the JSP script "potato.jsp" is responsible for processing the states of the various checkboxes. Data from the checkboxes is assigned to an array, and then this array is used to recreate a list of the selected items. Take a look.

    <html> <head><basefont face="Arial"></head><body>So your favourite shows are:<br><%// potato.jsp - process list of selected TV shows// define variablesString[] Shows;// assign values to variablesShows = request.getParameterValues("shows");out.println("<ul>");// print by iterating through arrayfor(int counter = 0; counter < Shows.length; counter++){ out.println("<li>" + Shows[counter]);}out.println("</ul>");%></body></html>
    As you can see, the first order of business is to create an array to hold the checkbox data - in this case, the array "Shows". Then, the Request object is used to obtain the values of the selected items via the getParameterValues() method (similar to the getParameter() method, but returns a list of values, rather than a single value) and these values are then assigned to the "Shows" array. A "for" loop is then used to create a list of the selected items.

    This technique can also be used with multiple select list boxes - here's the same example, rewritten to use a list box instead of a series of checkboxes.

    <html> <head><basefont face="Arial"></head><body>Pick your favourite shows:<br><form action="potato.jsp" method="POST"><select name="shows" multiple> <option>Ally McBeal</option> <option>Buffy The Vampire Slayer</option> <option>The Practice</option> <option>Sex And The City</option> <option>The Sopranos</option> <option>Survivor</option></select><br><input type="submit" name="submit" value="Select"></form></body></html>
    Obviously, the server-side script "potato.jsp" requires no changes at all.

    More Java Articles
    More By Vikram Vaswani, (c) Melonfire


     

       

    JAVA ARTICLES

    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...
    - Java Operators
    - Primitive Data Types and Basic Language Rule...
    - Java and Object-Oriented Programming
    - Java Beginning Programming
    - Gaming Development Setup




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway