java.applet.Applet.getParameter()

Discussion in 'Java' started by blaine@worldweb.com, Jan 11, 2007.

  1. Guest

    I would like to test to see if a parameter exists in the html file
    prior to calling getParameter(<key>), however I can not find any sort
    of method to allow me to do this.

    Is there a containsKey() method or something similar so that I could
    write code like:

    String[] keys = {"key1","key2"};
    Hashtable ht = new Hashtable();

    for(int i = 0; keys.length > i; i++){
    if ( <KEY EXISTS IN HTML PARAMETER LIST> ){
    ht.put(keys, getParameter(keys);
    }
    }

    I know that I could just "try" and "catch" the getParameter, but this
    does not seem as clean as just doing a test for existence.

    Any help would be appreciated.
    , Jan 11, 2007
    #1
    1. Advertising

  2. Oliver Wong Guest

    <> wrote in message
    news:...
    >I would like to test to see if a parameter exists in the html file
    > prior to calling getParameter(<key>), however I can not find any sort
    > of method to allow me to do this.
    >
    > Is there a containsKey() method or something similar so that I could
    > write code like:
    >
    > String[] keys = {"key1","key2"};
    > Hashtable ht = new Hashtable();
    >
    > for(int i = 0; keys.length > i; i++){
    > if ( <KEY EXISTS IN HTML PARAMETER LIST> ){
    > ht.put(keys, getParameter(keys);
    > }
    > }
    >
    > I know that I could just "try" and "catch" the getParameter, but this
    > does not seem as clean as just doing a test for existence.
    >
    > Any help would be appreciated.
    >


    From the javadocs:

    @return the value of the named parameter, or null if not set.

    - Oliver
    Oliver Wong, Jan 11, 2007
    #2
    1. Advertising

  3. wrote:

    > I would like to test to see if a parameter exists in the html file
    > prior to calling getParameter(<key>), however I can not find any sort
    > of method to allow me to do this.
    >
    > Is there a containsKey() method or something similar so that I could
    > write code like:
    >
    > String[] keys = {"key1","key2"};
    > Hashtable ht = new Hashtable();
    >
    > for(int i = 0; keys.length > i; i++){
    > if ( <KEY EXISTS IN HTML PARAMETER LIST> ){
    > ht.put(keys, getParameter(keys);
    > }
    > }
    >
    > I know that I could just "try" and "catch" the getParameter, but this
    > does not seem as clean as just doing a test for existence.
    >
    > Any help would be appreciated.


    The getParameter(String) method is specified to return null, if the
    parameter does not exist. Therefore, in you for-loop you can just do:
    String value = getParameter(keys);
    if (value == null){
    ht.put(keys, value);
    }
    There is no need for try/catch.

    --
    Thomas
    Thomas Fritsch, Jan 11, 2007
    #3
  4. Guest

    Thanks Tomas.. Guess I missed that in the docs.

    Thomas Fritsch wrote:
    > wrote:
    >
    > > I would like to test to see if a parameter exists in the html file
    > > prior to calling getParameter(<key>), however I can not find any sort
    > > of method to allow me to do this.
    > >
    > > Is there a containsKey() method or something similar so that I could
    > > write code like:
    > >
    > > String[] keys = {"key1","key2"};
    > > Hashtable ht = new Hashtable();
    > >
    > > for(int i = 0; keys.length > i; i++){
    > > if ( <KEY EXISTS IN HTML PARAMETER LIST> ){
    > > ht.put(keys, getParameter(keys);
    > > }
    > > }
    > >
    > > I know that I could just "try" and "catch" the getParameter, but this
    > > does not seem as clean as just doing a test for existence.
    > >
    > > Any help would be appreciated.

    >
    > The getParameter(String) method is specified to return null, if the
    > parameter does not exist. Therefore, in you for-loop you can just do:
    > String value = getParameter(keys);
    > if (value == null){
    > ht.put(keys, value);
    > }
    > There is no need for try/catch.
    >
    > --
    > Thomas
    , Jan 11, 2007
    #4
  5. Here is the way I deal with applet parameters

    public enum Param
    {
    PARAM1( new String( "string" ) )
    { Object getValue( Applet a ) { return super.getValue( a ); } },

    PARAM2( new Integer( 1 ) )
    { Object getValue( Applet a ) { return super.getValue( a ); } },

    PARAMn( new Double( 1.2 ) )
    { Object getValue( Applet a ) { return super.getValue( a ); } };

    protected Object v = null;

    private Param( Object defValue )
    {
    this.v = defValue;
    }

    Object getValue( Applet a )
    {
    Object v2 = a.getParameter( this.toString() );
    return (v2 == null) ? this.v : v2;
    }
    }

    public MyApplet extends Applet
    {
    /* ....... */

    public void start()
    {
    /* ....... */
    string theValue = Param.PARAM1.getValue();
    /* ....... */
    }

    /* ....... */
    }

    wrote:
    > I would like to test to see if a parameter exists in the html file
    > prior to calling getParameter(<key>), however I can not find any sort
    > of method to allow me to do this.
    >
    > Is there a containsKey() method or something similar so that I could
    > write code like:
    >
    > String[] keys = {"key1","key2"};
    > Hashtable ht = new Hashtable();
    >
    > for(int i = 0; keys.length > i; i++){
    > if ( <KEY EXISTS IN HTML PARAMETER LIST> ){
    > ht.put(keys, getParameter(keys);
    > }
    > }
    >
    > I know that I could just "try" and "catch" the getParameter, but this
    > does not seem as clean as just doing a test for existence.
    >
    > Any help would be appreciated.
    >
    Laurent D.A.M. MENTEN, Jan 11, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charlie
    Replies:
    2
    Views:
    3,012
    Charlie
    Aug 13, 2003
  2. Fred
    Replies:
    2
    Views:
    4,369
  3. Steveo
    Replies:
    2
    Views:
    5,737
    Michiel Konstapel
    Sep 17, 2003
  4. KK
    Replies:
    2
    Views:
    765
  5. paramsethi
    Replies:
    6
    Views:
    6,553
    paramsethi
    Mar 24, 2010
Loading...

Share This Page