java.applet.Applet.getParameter()

B

blaine

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.
 
O

Oliver Wong

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
 
T

Thomas Fritsch

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.
 
B

blaine

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

Thomas said:
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.
 
L

Laurent D.A.M. MENTEN

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();
/* ....... */
}

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top