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

深入探究cookie技术在J2ME平台的应用与实现(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(DataInputStreamdis)throwsIOException{  
  60. Cookiecookie=newCookie();  
  61. cookie.name=dis.readUTF();  
  62. cookie.value=dis.readUTF();  
  63. cookie.path=dis.readUTF();  
  64. cookie.expire=dis.readLong();  
  65. returncookie;  
  66. }  
  67.  
  68. publiclonggetExpire(){  
  69. returnexpire;  
  70. }  
  71.  
  72. publicvoidsetExpire(longexpire){  
  73. this.expire=expire;  
  74. }  
  75. //fordebug  
  76. publicStringtoString(){  
  77. returnname+"="+value+";expires="+newDate(expire).  
  78. toString()+";path="+path;  
  79. }  
  80.  
  81. publicbooleanisExpired(longnow){  
  82. returnexpire-now<0;  
  83. }  
  84.  
  85. publicbooleanisExpired(){  
  86. returnexpire-(newDate().getTime())<0;  
  87. }  
  88.  
  89. publicstaticCookieparseCookie(Strings,Stringuri){  
  90. Cookiecookie=newCookie();  
  91. StringUtilsu=newStringUtil(s,";");  
  92. while(su.hasMoreTokens()){  
  93. Stringstr=su.nextToken().trim();  
  94. inti=str.indexOf("=");  
  95. if(i==-1){  
  96. //securedonothing  
  97. continue;  
  98. }else{  
  99. Stringname=str.substring(0,i);  
  100. Stringvalue=str.substring(i+1,str.length());  
  101. if("path".equals(name)){  
  102. cookie.setPath(value);  
  103. }elseif("expires".equals(name)){  
  104. cookie.setExpire(StringUtil.getData(value));  
  105. }elseif("domain".equals(name)){  
  106. //donothing  
  107. }else{  
  108. cookie.setName(name);  
  109. cookie.setValue(value);  
  110. }  
  111. }  
  112. if(cookie.getPath().equals(""))  
  113. cookie.setPath(uri);  
  114. }  
  115. returncookie;  
  116. }  
  117.  
  118. publicbooleanequals(Objectobj){  
  119. if(objinstanceofCookie){  
  120. Cookieo=(Cookie)obj;  
  121. if(o.getName().equals(name)&&o.getPath().equals(path))  
  122. returntrue;  
  123. }  
  124. returnfalse;  
  125. }  
  126.  
  127. publicinthashCode(){  
  128. intresult=17;  
  129. resultresult=result*37+path.hashCode();  
  130. resultresult=result*37+name.hashCode();  
  131. returnresult;  
  132. }  
  133. }  
  134.  

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

精彩图集

赞助商链接