import java.applet.*;
import java.util.*;
public class AppletUrlParams extends Applet {
Hashtable searchparms;
public void init() {
// dump to the console the URL, the search and search values
// the URL http://myserver.com/mypage.html?value1=x&value2=y&value3=z
// the search value1=x&value2=y&value3=z
// the values value1=x
// value2=y
// value3=z
//
// then the values are stored in a Hashtable for easy reference.
// ex. String name = searchparms.get("value2")
doit();
}
public void doit() {
int i;
String completeURL = getDocumentBase().toString();
System.out.println("Complete URL: " + completeURL);
i = completeURL.indexOf("?");
if (i > -1) {
String searchURL = completeURL.substring(completeURL.indexOf("?") + 1);
System.out.println("Search URL: " + searchURL);
StringTokenizer st =
new StringTokenizer(searchURL, "&");
while(st.hasMoreTokens()){
String searchValue=st.nextToken();
System.out.println("value :" + searchValue);
}
initHashtable(searchURL);
dumpHashtable();
}
}
public void initHashtable(String search) {
searchparms = new Hashtable();
StringTokenizer st1 =
new StringTokenizer(search, "&");
while(st1.hasMoreTokens()){
StringTokenizer st2 =
new StringTokenizer(st1.nextToken(), "=");
searchparms.put(st2.nextToken(),
java.net.URLDecoder.decode(st2.nextToken()));
}
}
public void dumpHashtable() {
Enumeration keys = searchparms.keys();
System.out.println("--------");
while( keys.hasMoreElements() ) {
String s = (String) keys.nextElement();
System.out.println("key : " + s + " value : " + searchparms.get(s));
}
System.out.println("--------");
}
}
Test it here.
The resultat in the Java console should be :
key : firsparam value : Hello key : secondparam value : World key : thirdparam value : Hello World
A note from mm300
Access parameters passed in the URL in line String completeURL = getDocumentBase().toString(); is a trap: NS won't return the whole URL, but only domain name and directory but without .html and parameters. In IE (5.5) it's ok. So: if we have www.domain.com/applets/win.html?winner=Maurice
getDocumentBase () will return:
NS: www.domain.com/applets/
IE: www.domain.com/applets/win.html?winner=Maurice
Written and compiled by Réal Gagnon ©1998-2005
[ home ]