Try the following code to get the client browser cookies by using JSP/APPLET.
JSObject is required. Please download netscape jars from netscape.
import netscape.javascript.JSObject;
import netscape.javascript.JSException;
public String getCookies() { /* ** get all cookies for a document */
try {
JSObject myBrowser = (JSObject) JSObject.getWindow(this);
if (myBrowser==null) { System.err.println("## HackApplet.getCookies() No Window"); return ""; }
JSObject myDocument = (JSObject) myBrowser.getMember("document");
if (myDocument==null) { System.err.println("## HackApplet.getCookies() No Document"); return ""; }
String myCookie = (String)myDocument.getMember("cookie");
if (myCookie==null) { System.err.println("## HackApplet.getCookies() No Cookie"); return ""; }
if (myCookie.length() > 0)
return myCookie;
}
catch (JSException e){
System.err.println("HackApplet.getCookies(): "+e.getMessage());
e.printStackTrace();
Throwable t = (Throwable)e.getWrappedException();
System.err.println(" "+t.getMessage());
t.printStackTrace();
}
return "?";
}
public String getCookie(String name) {
String myCookie = getCookies();
String search = name + "=";
int offset = myCookie.indexOf(search);
if (offset != -1) {
offset += search.length();
int end = myCookie.indexOf(";", offset);
if (end == -1) end = myCookie.length();
return myCookie.substring(offset,end);
}
return "";
}
No comments:
Post a Comment