编程小贴士

给你的编程提供小点子


会话技术:Cookie和Session

一、Cookie操作

cookie:将服务器的数据保存到浏览器端的技术

* IE;将cookie保存到指定目录中,以文件的形式存储
* 火狐或谷歌:将cookie保存到指定的文件中
* 使用HTTP协议进行通信,而HTTP协议是采用ISO-8859-1进行编码的
* request.getCookies();获得当前web项目的所有的cookie信息,获得当前servlet所在目录,以及之上的所有目录下的cookie信息
* cookie的value可以设置多少个字符:4kb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    public void doPost(HttpServletRequest request, HttpServletResponse response)  
                throws ServletException, IOException {  
            request.setCharacterEncoding("UTF-8");  
            response.setContentType("text/html;charset=UTF-8");  
            String data = "中文"; //中文必须要进行解码才能保存成功  
            Cookie cookie = new Cookie("cn",URLEncoder.encode(data, "UTF-8"));  
            //读取Cookie时用URLDecoder.decode(c.getValue(), "UTF-8");进行解码  
 
            // * 修改cookie保存路径  
            cookie.setPath("/day07/");  
            //设置有效时间  
            cookie.setMaxAge(60*60); // 如果设置的值为零,则进行删除操作,单位是秒  
 
            //发送数据  
            response.addCookie(cookie);  
        }

二、Session
session的默认有效时间是30分钟,可以在tomcat的conf/web.xml中进行修改

1
2
3
4
5
6
7
    public void doPost(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
        request.setCharacterEncoding("UTF-8");  
        response.setContentType("text/html;charset=UTF-8");  
        HttpSession session = request.getSession(); //没有创建,有返回  
        System.out.println(session.isNew());  
    }

看下面的响应(Response)中有个Set-Cookie,创建session后会有个ID,服务器会将这个ID以Cookie响应到浏览器,浏览器关闭时这个Cookie也就消失。可以对这个cookie进行持久化操作

tips1000_2014-07-06_09-27-4

164 Responses to “ 会话技术:Cookie和Session ”

  1. best pron说道:

    iK0Ysj Way cool! Some extremely valid points! I appreciate you penning this article plus the rest of the website is really good.

  2. I really liked your article post.Thanks Again. Really Great.

  3. free iptv说道:

    It is almost not possible to find knowledgeable folks within this subject, on the other hand you sound like you realize what you are speaking about! Thanks

  4. watch movies说道:

    Major thankies for the article.Really thank you! Really Great.

  5. Only wanna tell that this is handy , Thanks for taking your time to write this.

  6. Lung Detox说道:

    Spot on with this write-up, I really think this amazing site needs much

  7. You have remarked very interesting points ! ps nice web site. Justice is the truth in action. by Jeseph Joubert.

  8. Thanks-a-mundo for the blog.Really thank you! Want more.

  9. There is definately a great deal to know about this subject. I like all of the points you have made.

  10. Click to read说道:

    It sounds like you are generating difficulties by yourself by attempting to solve this issue as an alternative of looking at why their is often a issue in the first place

  11. website说道:

    Thank you for your article. Really Cool.

  12. omlet arcade说道:

    I wanted to thank you for this abundant read!! I absolutely enjoyed each little crumb of it. I have got you bookmarked to ensure made known original stuff you post

  13. website说道:

    ppi claims ireland How can u add a flash slide show to your blogspot?

  14. w88 world说道:

    Wow, fantastic weblog structure! How long have you ever been running a blog for? you make blogging look easy. The overall glance of your web site is excellent, as well as the content material!

  15. Inspiring story there. What happened after? Take care!

  16. Wow! This could be one particular of the most useful blogs We have ever arrive across on this subject. Actually Excellent. I am also an expert in this topic so I can understand your effort.

  17. for more info说道:

    There may be noticeably a bundle to know about this. I assume you made sure nice factors in options also.

  18. Precisely what I was looking for, regards for posting.

  19. pre booked说道:

    this topic to be actually something that I think I would never understand.

  20. Thanks-a-mundo for the article.Thanks Again. Cool.

  21. Wow, great post.Much thanks again. Want more.

  22. south korea jersey ??????30????????????????5??????????????? | ????????

  23. click for more说道:

    Really informative article.Much thanks again. Really Cool.

  24. It’а†s really a nice and helpful piece of information. I’а†m satisfied that you shared this helpful info with us. Please stay us up to date like this. Thank you for sharing.

  25. Wow, amazing weblog format! How long have you ever been running a blog for? you made running a blog glance easy. The full glance of your site is fantastic, let alone the content!

  26. NIW说道:

    This blog is obviously entertaining and factual. I have picked up many useful tips out of it. I ad love to visit it again soon. Cheers!

  27. Must share说道:

    Muchos Gracias for your post.Really looking forward to read more. Awesome.

  28. Lashawn说道:

    Right now it looks like WordPress is the best blogging platform out there right now. (from what I ave read) Is that what you are using on your blog?

  29. voucher tiki说道:

    You made some decent factors there. I regarded on the web for the issue and located most people will go along with with your website.

  30. Go Here说道:

    Tumblr article I saw someone writing about this on Tumblr and it linked to

  31. Nwokolo说道:

    It as actually a great and useful piece of information. I am satisfied that you shared this useful tidbit with us. Please stay us informed like this. Thank you for sharing.

  32. cay thong noel说道:

    very good publish, i actually love this web site, keep on it

  33. Thank you for your blog article. Really Cool.

  34. Thanks-a-mundo for the article post.Really looking forward to read more. Cool.

  35. Eminem说道:

    I used to be able to find good advice from your blog articles.

  36. wonderful points altogether, you simply won a new reader. What might you suggest in regards to your submit that you just made some days ago? Any sure?

  37. With havin so much written content do you ever run into

  38. Valuable information. Lucky me I found your site by accident, and I am shocked why this accident did not happened earlier! I bookmarked it.

  39. Sorry for the off-topic, could you tell where I can get such a nice pattern for my blog ?!

  40. freight courier说道:

    There is definately a lot to find out about this topic. I love all the points you made.

  41. Way cool! Some very valid points! I appreciate you writing this post and also the rest of the site is extremely good.

  42. It as a very easy on the eyes which makes it much more enjoyable for me to

  43. This particular blog is obviously interesting as well as amusing. I have discovered a bunch of handy tips out of this source. I ad love to visit it over and over again. Cheers!

  44. It as not that I want to copy your internet site, but I really like the layout. Could you tell me which design are you using? Or was it especially designed?

  45. play说道:

    Just a smiling visitant here to share the love (:, btw great style. Individuals may form communities, but it is institutions alone that can create a nation. by Benjamin Disraeli.

  46. Vs model说道:

    Inspiring story there. What occurred after? Take care!

  47. this website说道:

    You are my inspiration, I possess few blogs and occasionally run out from post . Actions lie louder than words. by Carolyn Wells.

  48. David说道:

    They replicate the worldwide attraction of our dual Entire world Heritage sectors which have been attributed to boosting delegate figures, she said.

  49. healthy说道:

    Very good post! We are linking to this great content on our site. Keep up the good writing.

  50. Rental Dress说道:

    Thanks so much for the blog. Will read on

  51. There is evidently a bundle to realize about this. I suppose you made various good points in features also.

  52. It as not that I want to replicate your web site, but I really like the style. Could you let me know which theme are you using? Or was it tailor made?

  53. Ita??a?аАа’аАТ‚аЂ s really a cool and helpful piece of information. I am glad that you simply shared this useful info with us. Please stay us informed like this. Thank you for sharing.

  54. plastic bathroom faucets woud eaily break compared to bronze bathroom faucets-

  55. slimming drinks说道:

    I see that you simply are making use of WordPress on your blog, wordpress could be the best.~ a-~*

  56. Crochet basico说道:

    Nice post! Also visit my site about Clomid challenge test

  57. Polainas tejidas说道:

    Its hard to find good help I am regularly saying that its hard to get quality help, but here is

  58. Arctic Circle说道:

    This awesome blog is no doubt entertaining additionally informative. I have chosen helluva handy tips out of this blog. I ad love to visit it over and over again. Thanks a lot!

  59. my page说道:

    Only a smiling visitant here to share the love (:, btw outstanding style and design. Reading well is one of the great pleasures that solitude can afford you. by Harold Bloom.

  60. red shoes说道:

    Its hard to find good help I am regularly proclaiming that its difficult to procure quality help, but here is

  61. This is very interesting, You are a very skilled blogger. I ave joined your feed and look forward to seeking more of your great post. Also, I have shared your site in my social networks!

  62. School news说道:

    I visited a lot of website but I believe this one holds something extra in it in it

  63. Waec exam说道:

    You made some nice points there. I looked on the internet for the subject matter and found most individuals will approve with your website.

  64. I think this is a real great post.Thanks Again. Keep writing.

  65. Really informative article post.Really thank you! Awesome.

  66. Thanks-a-mundo for the article post.Really thank you! Much obliged.

  67. Wow, fantastic blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your website is excellent, let alone the content!

  68. oral sex说道:

    I value the blog post.Much thanks again.

  69. Pretty! This has been an extremely wonderful article. Thanks for providing these details.

  70. Open heaven说道:

    This is a beautiful shot with very good lighting

  71. I really liked your article post.Much thanks again. Keep writing.

  72. This is really interesting, You are a very skilled blogger. I ave joined your feed and look forward to seeking more of your excellent post. Also, I ave shared your web site in my social networks!

  73. the back! That was cool Once, striper were hard to find. They spend

  74. tips de ventas说道:

    Tiffany Jewelry ??????30????????????????5??????????????? | ????????

  75. Very good article! We are linking to this particularly great post on our site. Keep up the great writing.

  76. pretty handy stuff, overall I think this is well worth a bookmark, thanks

  77. Thanks for sharing this information with us.

  78. junior说道:

    Peculiar article, exactly what I needed.

  79. This awesome blog is without a doubt entertaining as well as informative. I have chosen helluva interesting stuff out of this amazing blog. I ad love to visit it again and again. Thanks!

  80. Open Heavens说道:

    Wow! Thank you! I permanently needed to write on my blog something like that. Can I include a portion of your post to my site?

  81. Some times its a pain in the ass to read what blog owners wrote but this site is very user genial!.

  82. Wow! Thank you! I continuously needed to write on my blog something like that. Can I take a part of your post to my site?

  83. this website说道:

    Wow, amazing blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your web site is great, let alone the content!

  84. Some really interesting information, well written and generally user genial.

  85. personal injury说道:

    Thank you for your blog.Really thank you! Really Cool.

  86. Thanks for the post.Really looking forward to read more. Great.

  87. This awesome blog is really awesome and besides amusing. I have discovered helluva handy advices out of this amazing blog. I ad love to return over and over again. Thanks a bunch!

  88. Chapel说道:

    Say, you got a nice article post.Really looking forward to read more. Want more.

  89. Grayce说道:

    Spot on with this write-up, I truly think this website needs much more consideration. I all probably be again to read much more, thanks for that info.

  90. well defined说道:

    Superb Article My brother suggested I might like this web site. He was totally right. This post truly made my day. You can not imagine simply how much time I had spent for this info! Thanks!

  91. find more here说道:

    Thanks-a-mundo for the blog post.Much thanks again. Keep writing.

  92. You made some decent points there. I looked on the internet for the subject matter and found most persons will agree with your site.

  93. this website说道:

    Utterly pent content, appreciate it for information. No human thing is of serious importance. by Plato.

  94. It as difficult to find knowledgeable people for this topic, but you seem like you know what you are talking about! Thanks

  95. ta, aussi je devais les indices de qu aen fait

  96. You are my function models. Many thanks for your post

  97. for details说道:

    This particular blog is really cool additionally informative. I have discovered helluva useful things out of this amazing blog. I ad love to go back again and again. Thanks a bunch!

  98. Im thankful for the article. Keep writing.

  99. light bulbs are good for lighting the home but stay away from incandescent lamps simply because they produce so substantially heat

  100. Thanks for another great article. Where else could anybody get that kind of info in such an ideal method of writing? I have a presentation subsequent week, and I am at the search for such info.

  101. Share说道:

    salaams peoplehope allah swt answers ALL YOUR RIGHTOUS duas and may all your wishes, dreams come trueameen.

  102. Really informative article post.Thanks Again. Much obliged.

  103. Free ads说道:

    Wow, wonderful blog structure! How lengthy have you ever been blogging for? you made blogging look easy. The total glance of your website is great, let alone the content material!

  104. explore说道:

    Thanks a whole lot for sharing this with all of us you essentially know what you will be speaking about! Bookmarked. Kindly also visit my web page =). We could have a link exchange contract among us!

  105. more details说道:

    What a funny blog! I actually loved watching this humorous video with my relatives as well as with my colleagues.

  106. Muchos Gracias for your blog post. Awesome.

  107. You could definitely see your skills within the work you write. The world hopes for more passionate writers like you who are not afraid to say how they believe. At all times go after your heart.

  108. posts. Stay up the great work! You recognize,

  109. There is clearly a bunch to identify about this. I think you made certain nice points in features also.

  110. Twroomtalk说道:

    The league as new contract with the womens peyton manning jersey; they can

  111. ga bo xoi chien说道:

    Thanks again for the article post.Really looking forward to read more. Cool. this link

  112. copper fit说道:

    merely achieve full lf on finished bread, and as well portion that honestly

  113. The Birch of the Shadow I feel there may possibly become a couple duplicates, but an exceedingly handy listing! I have tweeted this. Several thanks for sharing!

  114. check here说道:

    pretty helpful material, overall I believe this is really worth a bookmark, thanks

  115. go there说道:

    It as difficult to find experienced people for this topic, however, you sound like you know what you are talking about! Thanks

  116. Wow, that as what I was seeking for, what a material! existing here at this web site, thanks admin of this website.

  117. situs saya说道:

    I think you have mentioned some very interesting details , regards for the post.

  118. Thanks again for the post.Thanks Again. Will read on

  119. genuine parts说道:

    Just imagined I might remark and say fantastic concept, did you help it become on your individual? Seems to be really fantastic!

  120. good morning说道:

    moment this time I am browsing this website and reading very informative

  121. It is in reality a great and helpful piece of information. I am glad that you just shared this helpful info with us. Please keep us informed like this. Thank you for sharing.

  122. IaаАа’б‚Т€ТšаЂаŒаАа’б‚Т€ТžаБТžm glad to become a visitor in this pure web site, regards for this rare info!

  123. The strategies mentioned in this article regarding to increase traffic at you own webpage are really pleasant, thanks for such fastidious paragraph.

  124. This is a really good tip especially to those fresh to the blogosphere. Brief but very accurate info Thanks for sharing this one. A must read article!

  125. Wow, great post.Much thanks again. Cool.

  126. Really informative blog post.Thanks Again. Really Great.

  127. Way cool! Some extremely valid points! I appreciate you penning this article and also the rest of the website is extremely good.

  128. I value the article.Thanks Again. Want more.

  129. Game Online说道:

    Pretty! This has been a really wonderful article. Thank you for supplying this info.

  130. Canon c.p说道:

    Very good article post.Really looking forward to read more. Fantastic.

  131. cierre de ventas说道:

    Normally I do not read post on blogs, but I wish to say that this write-up very forced me to try and do it! Your writing style has been surprised me. Thanks, very nice article.

  132. click here说道:

    Wow, great blog.Really looking forward to read more. Fantastic.

  133. Really enjoyed this article post.Really looking forward to read more. Cool.

  134. I value the article post.Really looking forward to read more. Keep writing.

  135. hand craft说道:

    I think other web-site proprietors should take this website as an model, very clean and fantastic user genial style and design, let alone the content. You are an expert in this topic!

  136. Keep up the excellent piece of work, I read few blog posts on this web site and I conceive that your site is rattling interesting and contains circles of great info.

  137. Way cool! Some extremely valid points! I appreciate you penning this write-up and also the rest of the site is also very good.

  138. Very neat blog article.Much thanks again. Much obliged.

  139. read more说道:

    Thank you ever so for you article post.Really thank you! Great.

  140. It as difficult to find well-informed people for this topic, but you sound like you know what you are talking about! Thanks

  141. Is not it amazing whenever you discover a fantastic article? My personal web browsings seem full.. thanks. Respect the admission you furnished.. Extremely valuable perception, thanks for blogging..

  142. Wow, incredible blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your site is great, let alone the content!. Thanks For Your article about sex.

  143. Wow, fantastic blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your website is fantastic, as well as the content!

  144. Wow, incredible blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your site is fantastic, let alone the content!

  145. My brother suggested I might like this website. He was totally right. This post truly made my day. You can not imagine simply how much time I had spent for this info! Thanks!

  146. Wow, amazing blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your site is magnificent, as well as the content!

  147. Muchos Gracias for your article.Much thanks again. Really Cool.

  148. Im obliged for the article post.Really thank you! Awesome.

  149. best pron说道:

    PuL8NT I truly appreciate this article.Really thank you! Will read on

  150. i love pron说道:

    zkbsXx Thanks for the article.Thanks Again. Want more.

  151. I reckon something truly special in this internet site.

  152. Your style is very unique in comparison to other people I have read stuff from. I appreciate you for posting when you ave got the opportunity, Guess I all just book mark this web site.

  153. Very nice post. I definitely appreciate this site. Continue the good work!

  154. There is definately a lot to know about this issue. I love all the points you made.

  155. Say, you got a nice blog article.Really thank you! Will read on

  156. Woh I love your articles , saved to favorites !.

  157. Westwood realtor说道:

    You have remarked very interesting points ! ps decent site. I understand a fury in your words, But not the words. by William Shakespeare.

  158. Looking forward to reading more. Great post.Much thanks again. Great.

  159. official page说道:

    Wow! This could be one particular of the most useful blogs We have ever arrive across on this subject. Basically Wonderful. I am also a specialist in this topic so I can understand your hard work.

  160. Wow! This can be one particular of the most useful blogs We have ever arrive across on this subject. Actually Great. I am also a specialist in this topic so I can understand your effort.

  161. mens wallets说道:

    I was recommended this blog by my cousin. I am not sure whether this post is written by him as no one else know such detailed about my problem. You are amazing! Thanks!

  162. bistro masa说道:

    your associate link to your host? I want my site loaded up as fast as yours lol

  163. This is really interesting, You are a very skilled blogger. I ave joined your feed and look forward to seeking more of your wonderful post. Also, I have shared your website in my social networks!

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>