龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > Javascript编程 >

J2ME平台中Cookie技术的应用(1)(2)

时间:2013-03-06 14:58来源:未知 作者:admin 点击:
分享到:
第二:保存cookie 已经获得了cookie之后,就需要把cookie存储下来,存储分为两个部分,首先需要解析cookie,我们定义一个JavaBean来代表cookie. packagecom.j2medev

第二:保存cookie

已经获得了cookie之后,就需要把cookie存储下来,存储分为两个部分,首先需要解析cookie,我们定义一个JavaBean来代表cookie.

  1. packagecom.j2medev.lomol.model;  
  2.  
  3. importcom.j2medev.lomol.util.StringUtil;  
  4. importjava.io.DataInputStream;  
  5. importjava.io.DataOutputStream;  
  6. importjava.io.IOException;  
  7. importjava.util.Date;  
  8.  
  9. /**  
  10. *acookiestoredonthemobiledevice,  
  11. cookieisusedtomaintainthestatesbetweenclientandserver  
  12. *@authormingjava  
  13. *@version0.105/06/2006  
  14. */  
  15. publicclassCookie{  
  16.  
  17. privateStringpath="";  
  18. privateStringname="";  
  19. privateStringvalue="";  
  20. privatelongexpire=SESSION_COOKIE;  
  21. publicstaticlongSESSION_COOKIE=0;
  22. //sessioncookie,onlyvalidthissession  
  23.  
  24. publicCookie(){  
  25. }  
  26.  
  27. publicStringgetPath(){  
  28. returnpath;  
  29. }  
  30.  
  31. publicvoidsetPath(Stringpath){  
  32. this.path=path;  
  33. }  
  34.  
  35. publicStringgetName(){  
  36. returnname;  
  37. }  
  38.  
  39. publicvoidsetName(Stringname){  
  40. this.name=name;  
  41. }  
  42.  
  43. publicStringgetValue(){  
  44. returnvalue;  
  45. }  
  46.  
  47. publicvoidsetValue(Stringvalue){  
  48. this.value=value;  
  49. }  
  50.  
  51. publicvoidserialize(DataOutputStreamdos)
  52. throwsIOException{  
  53. dos.writeUTF(name);  
  54. dos.writeUTF(value);  
  55. dos.writeUTF(path);  
  56. dos.writeLong(expire);  
  57. }  
  58.  
  59. publicstaticCookiedeserialize
  60. (DataInputStreamdis)throwsIOException{  
  61. Cookiecookie=newCookie();  
  62. cookie.name=dis.readUTF();  
  63. cookie.value=dis.readUTF();  
  64. cookie.path=dis.readUTF();  
  65. cookie.expire=dis.readLong();  
  66. returncookie;  
  67. }  
  68.  
  69. publiclonggetExpire(){  
  70. returnexpire;  
  71. }  
  72.  
  73. publicvoidsetExpire(longexpire){  
  74. this.expire=expire;  
  75. }  
  76. //fordebug  
  77. publicStringtoString(){  
  78. returnname+"="+value+";
  79. expires="+newDate(expire).toString()+";path="+path;  
  80. }  
  81.  
  82. publicbooleanisExpired(longnow){  
  83. returnexpire-now<0;  
  84. }  
  85.  
  86. publicbooleanisExpired(){  
  87. returnexpire-(newDate().getTime())<0;  
  88. }  
  89.  
  90. publicstaticCookieparseCookie(Strings,Stringuri){  
  91. Cookiecookie=newCookie();  
  92. StringUtilsu=newStringUtil(s,";");  
  93. while(su.hasMoreTokens()){  
  94. Stringstr=su.nextToken().trim();  
  95. inti=str.indexOf("=");  
  96. if(i==-1){  
  97. //securedonothing  
  98. continue;  
  99. }else{  
  100. Stringname=str.substring(0,i);  
  101. Stringvalue=str.substring(i+1,str.length());  
  102. if("path".equals(name)){  
  103. cookie.setPath(value);  
  104. }elseif("expires".equals(name)){  
  105. cookie.setExpire(StringUtil.getData(value));  
  106. }elseif("domain".equals(name)){  
  107. //donothing  
  108. }else{  
  109. cookie.setName(name);  
  110. cookie.setValue(value);  
  111. }  
  112. }  
  113. if(cookie.getPath().equals(""))  
  114. cookie.setPath(uri);  
  115. }  
  116. returncookie;  
  117. }  
  118.  
  119. publicbooleanequals(Objectobj){  
  120. if(objinstanceofCookie){  
  121. Cookieo=(Cookie)obj;  
  122. if(o.getName().equals(name)&&o.getPath().equals(path))  
  123. returntrue;  
  124. }  
  125. returnfalse;  
  126. }  
  127.  
  128. publicinthashCode(){  
  129. intresult=17;  
  130. resultresult=result*37+path.hashCode();  
  131. resultresult=result*37+name.hashCode();  
  132. returnresult;  
  133. }  
  134. }  
  135.  

提供了一个parseCookie方法来解析cookie,具体的原理就不再介绍了。然后需要把这个Cookie对象存储到RMS中。cookie并不大,所以不会占用太多的空间,在RMS中存储非常合适。注意对于会话期间的cookie没有必要存储在rms中,因为会话结束后就失效了,不如在内存中声明一个Map来存储会话类型的cookie。

精彩图集

赞助商链接