第二:保存cookie
已经获得了cookie之后,就需要把cookie存储下来,存储分为两个部分,首先需要解析cookie,我们定义一个JavaBean来代表cookie.
packagecom.j2medev.lomol.model;
importcom.j2medev.lomol.util.StringUtil;
importjava.io.DataInputStream;
importjava.io.DataOutputStream;
importjava.io.IOException;
importjava.util.Date;
/** *acookiestoredonthemobiledevice,
cookieisusedtomaintainthestatesbetweenclientandserver
*@authormingjava
*@version0.105/06/2006
*/
publicclassCookie{
privateStringpath="";
privateStringname="";
privateStringvalue="";
privatelongexpire=SESSION_COOKIE;
publicstaticlongSESSION_COOKIE=0;
//sessioncookie,onlyvalidthissession
publicCookie(){ }
publicStringgetPath(){ returnpath; }
publicvoidsetPath(Stringpath){ this.path=path; }
publicStringgetName(){ returnname; }
publicvoidsetName(Stringname){ this.name=name; }
publicStringgetValue(){ returnvalue; }
publicvoidsetValue(Stringvalue){ this.value=value; }
publicvoidserialize(DataOutputStreamdos)
throwsIOException{
dos.writeUTF(name);
dos.writeUTF(value);
dos.writeUTF(path);
dos.writeLong(expire);
}
publicstaticCookiedeserialize(DataInputStreamdis)throwsIOException{
Cookiecookie=newCookie();
cookie.name=dis.readUTF();
cookie.value=dis.readUTF();
cookie.path=dis.readUTF();
cookie.expire=dis.readLong();
returncookie;
}
publiclonggetExpire(){
returnexpire;
}
publicvoidsetExpire(longexpire){ this.expire=expire;
}
//fordebug
publicStringtoString(){
returnname+"="+value+";expires="+newDate(expire)。
toString()+";path="+path;
}
publicbooleanisExpired(longnow){
returnexpire-now<0;
}
publicbooleanisExpired(){
returnexpire-(newDate()。getTime())<0;
}
publicstaticCookieparseCookie(Strings,Stringuri){
Cookiecookie=newCookie();
StringUtilsu=newStringUtil(s,";");
while(su.hasMoreTokens()){
Stringstr=su.nextToken()。trim();
inti=str.indexOf("=");
if(i==-1){
//securedonothing continue;
}else{
Stringname=str.substring(0,i);
Stringvalue=str.substring(i+1,str.length());
if("path".equals(name)){
cookie.setPath(value);
}elseif("expires".equals(name)){
cookie.setExpire(StringUtil.getData(value));
}elseif("domain".equals(name)){
//donothing }else{
cookie.setName(name);
cookie.setValue(value);
} }
if(cookie.getPath()。equals(""))
cookie.setPath(uri); } returncookie; }
publicbooleanequals(Objectobj){
if(objinstanceofCookie){ Cookieo=(Cookie)obj;
if(o.getName()。equals(name)&&o.getPath()。equals(path))
returntrue; } returnfalse; }
publicinthashCode(){ intresult=17;
resultresult=result*37+path.hashCode();
resultresult=result*37+name.hashCode();
returnresult; } }
importcom.j2medev.lomol.util.StringUtil;
importjava.io.DataInputStream;
importjava.io.DataOutputStream;
importjava.io.IOException;
importjava.util.Date;
/** *acookiestoredonthemobiledevice,
cookieisusedtomaintainthestatesbetweenclientandserver
*@authormingjava
*@version0.105/06/2006
*/
publicclassCookie{
privateStringpath="";
privateStringname="";
privateStringvalue="";
privatelongexpire=SESSION_COOKIE;
publicstaticlongSESSION_COOKIE=0;
//sessioncookie,onlyvalidthissession
publicCookie(){ }
publicStringgetPath(){ returnpath; }
publicvoidsetPath(Stringpath){ this.path=path; }
publicStringgetName(){ returnname; }
publicvoidsetName(Stringname){ this.name=name; }
publicStringgetValue(){ returnvalue; }
publicvoidsetValue(Stringvalue){ this.value=value; }
publicvoidserialize(DataOutputStreamdos)
throwsIOException{
dos.writeUTF(name);
dos.writeUTF(value);
dos.writeUTF(path);
dos.writeLong(expire);
}
publicstaticCookiedeserialize(DataInputStreamdis)throwsIOException{
Cookiecookie=newCookie();
cookie.name=dis.readUTF();
cookie.value=dis.readUTF();
cookie.path=dis.readUTF();
cookie.expire=dis.readLong();
returncookie;
}
publiclonggetExpire(){
returnexpire;
}
publicvoidsetExpire(longexpire){ this.expire=expire;
}
//fordebug
publicStringtoString(){
returnname+"="+value+";expires="+newDate(expire)。
toString()+";path="+path;
}
publicbooleanisExpired(longnow){
returnexpire-now<0;
}
publicbooleanisExpired(){
returnexpire-(newDate()。getTime())<0;
}
publicstaticCookieparseCookie(Strings,Stringuri){
Cookiecookie=newCookie();
StringUtilsu=newStringUtil(s,";");
while(su.hasMoreTokens()){
Stringstr=su.nextToken()。trim();
inti=str.indexOf("=");
if(i==-1){
//securedonothing continue;
}else{
Stringname=str.substring(0,i);
Stringvalue=str.substring(i+1,str.length());
if("path".equals(name)){
cookie.setPath(value);
}elseif("expires".equals(name)){
cookie.setExpire(StringUtil.getData(value));
}elseif("domain".equals(name)){
//donothing }else{
cookie.setName(name);
cookie.setValue(value);
} }
if(cookie.getPath()。equals(""))
cookie.setPath(uri); } returncookie; }
publicbooleanequals(Objectobj){
if(objinstanceofCookie){ Cookieo=(Cookie)obj;
if(o.getName()。equals(name)&&o.getPath()。equals(path))
returntrue; } returnfalse; }
publicinthashCode(){ intresult=17;
resultresult=result*37+path.hashCode();
resultresult=result*37+name.hashCode();
returnresult; } }
提供了一个parseCookie方法来解析cookie,具体的原理就不再介绍了。然后需要把这个Cookie对象存储到RMS中。cookie并不大,所以不会占用太多的空间,在RMS中存储非常合适。注意对于会话期间的cookie没有必要存储在rms中,因为会话结束后就失效了,不如在内存中声明一个Map来存储会话类型的cookie。