|
Access parameters passed in the URL |
|
|
This tip shows how to decode the parameters passed in the URL.
import java.applet.*;
import java.util.*;
public class SimpleApplet 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(), 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("--------");
}
}
|
Related Tips
|
Page 1 of 0 ( 0 comments )
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.