Java
  Home arrow Java arrow Page 5 - Developing JavaServer Pages
The Best Selling PC Migration Utility.
Dev Shed Forums 
Administration  
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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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

Developing JavaServer Pages
By: Joel Murach
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 61
    2004-05-12

    Table of Contents:
  • Developing JavaServer Pages
  • The Code for the HTML Page that Calls the JSP
  • Imitating HTML
  • How to Create a JSP
  • How to Use the Methods of the Request Object
  • Retrieving Multiple Values
  • How to Request a JSP
  • Using Get and Post Methods
  • Using the Post Method
  • Managing Java Classes
  • Class Location

  • 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
     
     
    Iron Speed
     
    ADVERTISEMENT

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    Developing JavaServer Pages - How to Use the Methods of the Request Object
    (Page 5 of 11 )

    In the last figure, you learned how to use the getParameter method to return the value that the user entered into a textbox. Now, Figure 5 summarizes that method and illustrates it in a new context. This figure also summarizes and illustrates two more methods of the implicit request object.

    Figure 5: How to use the methods of the request object

    Three methods of the request object

    Method

    Description

    getParameter(String param)Returns the value of the specified parameter as a string if it exists or null if it doesn't. Often, this is the value defined in the Value attribute of the control in the HTML page or JSP.
    getParameterValues(String param)Returns an array of String objects containing all of the values that the given request parameter has or null if the parameter doesn't have any values.
    getParameterNames()Returns an Enumeration object that contains the names of all the parameters contained in the request. If the request has no parameters, the method returns an empty Enumeration object.

    More Scriptlets

    A scriptlet that determines if a checkbox is checked


    <%
        String rockCheckBox = request.getParameter("Rock");
        // returns the value or "on" if checked, null otherwise.
        if (rockCheckBox != null){
    %>
            You checked Rock music!
    <% 
        }
    %>

    A scriptlet that reads and displays multiple values from a list box

    <%
        String[] selectedCountries = request.getParameterValues("country");
        // returns the values of items selected in list box.
        for (int i = 0; i < selectedCountries.length; i++){
    %>
           <%= selectedCountries[i] %> <br>
    <%
        }
    %>

    A scriptlet that reads and displays all request parameters and values

    <%
        Enumeration parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()){
            String parameterName = (String) parameterNames.nextElement();
            String parameterValue = request.getParameter(parameterName);
    %>
            <%= parameterName %> has value <%= parameterValue %>. <br>
    <%
         }
    %>

    Description

    You can use the getParameter method to return the value of the selected radio button in a group or the selected item in a combo box. You can also use it to return the value of a selected check box or independent radio button, but that value is null if it isn't selected.

    If an independent radio button or a checkbox doesn't have a Value attribute, this method returns "on" if the control is selected or null if it isn't.

    In most cases, the getParameter method returns the value of the parameter. For a textbox, that's usually the value entered by the user. But for a group of radio buttons or a combo box, that's the value of the button or item selected by the user.

    For checkboxes or independent radio buttons that have a Value attribute, the getParameter method returns that value if the checkbox or button is selected and a null value if it isn't. For checkboxes or independent radio buttons that don't have a Value attribute, though, the getParameter method returns an "on" value if the checkbox or button is selected and a null value if it isn't. This is illustrated by the first example in this figure.

    Remember: this is from chapter four of Joel Murach's Java Servlets and JSP (Mike Murach & Associates, ISBN 1890774189, 2003). Grab a copy at your favorite book store today!

     Buy this book now.

    More Java Articles
    More By Joel Murach


     

       

    JAVA ARTICLES

    - 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
    - Using RPC-Style Web Services with J2EE
    - Integrating XML with J2EE
    - Taming Tiger: Concurrent Collections

    Iron Speed
     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




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