Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > java.applet.Applet.getParameter()

Reply
Thread Tools

java.applet.Applet.getParameter()

 
 
blaine@worldweb.com
Guest
Posts: n/a
 
      01-11-2007
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[i], getParameter(keys[i]);
}
}

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.

 
Reply With Quote
 
 
 
 
Oliver Wong
Guest
Posts: n/a
 
      01-11-2007

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>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[i], getParameter(keys[i]);
> }
> }
>
> 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


 
Reply With Quote
 
 
 
 
Thomas Fritsch
Guest
Posts: n/a
 
      01-11-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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[i], getParameter(keys[i]);
> }
> }
>
> 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[i]);
if (value == null){
ht.put(keys[i], value);
}
There is no need for try/catch.

--
Thomas
 
Reply With Quote
 
blaine@worldweb.com
Guest
Posts: n/a
 
      01-11-2007
Thanks Tomas.. Guess I missed that in the docs.

Thomas Fritsch wrote:
> (E-Mail Removed) 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[i], getParameter(keys[i]);
> > }
> > }
> >
> > 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[i]);
> if (value == null){
> ht.put(keys[i], value);
> }
> There is no need for try/catch.
>
> --
> Thomas


 
Reply With Quote
 
Laurent D.A.M. MENTEN
Guest
Posts: n/a
 
      01-11-2007
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();
/* ....... */
}

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

(E-Mail Removed) 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[i], getParameter(keys[i]);
> }
> }
>
> 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.
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments