applet 引用jia包:import netscape.javascript.JSObject;
设置cookie的程序: (OPLOAD_FILE_COOKIE是静态变量)
JSObject mybrowser = JSObject.getWindow(obj);
JSObject mydocument=(JSObject)mybrowser.getMember("document");Date date=new Date();
Date end=new Date(date.getYear()+10,0,0);
String endstr=end.toGMTString();
mydocument.setMember("cookie",OPLOAD_FILE_COOKIE+"="+path+";expires="+endstr);
获取cookie的程序:
private String getAllCookie(){
try{
JSObject mybrowser = JSObject.getWindow(obj);
JSObject mydocument=(JSObject)mybrowser.getMember("document");
String mycookie=(String)mydocument.getMember("cookie");
System.out.println("document对象:"+mydocument);
if(mycookie.length()>0){
System.out.println(mycookie);
return mycookie;
}
}catch(Exception e){
}
return"?";
}
private String getCookie(String name){
String mycookie=getAllCookie();
String search=name+"=";
if(mycookie.length()>0){
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);
}else{
System.out.println("没有发现cookies:"+name);
}
}
return "";
}
注意:设置cookie,如果默认不设置过期时间的话,是关闭浏览器就过期了,所以要加入expires,并且设置的时间格式是CMT格式。