﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-喝咖啡的企鹅-随笔分类-Servlet Jsp</title><link>http://www.blogjava.net/aczreg/category/43129.html</link><description>不管乐意不乐意，事情总得做……</description><language>zh-cn</language><lastBuildDate>Tue, 15 Dec 2009 14:23:11 GMT</lastBuildDate><pubDate>Tue, 15 Dec 2009 14:23:11 GMT</pubDate><ttl>60</ttl><item><title>servlet 数据传送</title><link>http://www.blogjava.net/aczreg/archive/2009/12/15/306069.html</link><dc:creator>咖啡企鹅</dc:creator><author>咖啡企鹅</author><pubDate>Tue, 15 Dec 2009 13:58:00 GMT</pubDate><guid>http://www.blogjava.net/aczreg/archive/2009/12/15/306069.html</guid><wfw:comment>http://www.blogjava.net/aczreg/comments/306069.html</wfw:comment><comments>http://www.blogjava.net/aczreg/archive/2009/12/15/306069.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/aczreg/comments/commentRss/306069.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/aczreg/services/trackbacks/306069.html</trackback:ping><description><![CDATA[<span style="color: rgb(0,128,0)"><span style="color: #000000">Cookie:同一用户点对点<br />
Session:同一用户一条线<br />
ServletContext:不同用户联成面</span><br />
<br />
<br />
//req: 用于获得客户端(浏览器)的信息</span><br style="color: rgb(0,128,0)" />
<span style="color: rgb(0,128,0)">//res: 用于向客户端(浏览器)返回信息</span><br />
1、session的设置：<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;<span style="color: rgb(0,128,0)">//得到和req相关联的session,如果没有就创建session</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;HttpSession hs=req.getSession(true);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;<span style="color: rgb(0,128,0)">//向session中添加一个属性(String 类型的)</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;hs.setAttribute("name","hanzhewei");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;<span style="color: rgb(0,128,0)">//如果不指定时间，那么该session的有效期是30min,在此设定为30秒</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;hs.setMaxInactiveInterval(30);<br />
&nbsp;&nbsp;&nbsp;session的获取：<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; <span style="color: rgb(0,128,0)">//得到和req相关联的session,如果没有就创建session</span><br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; HttpSession hs=req.getSession(true);<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; String name=hs.getAttribute("name");<br />
&nbsp;&nbsp;&nbsp;session的删除：<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgb(0,128,0)">//从session中删除you属性</span><br style="color: rgb(0,128,0)" />
<span style="color: rgb(0,128,0)">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //ht.removeAttribute("you");</span><br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ht.setMaxInactiveInterval(0);<br />
2、cookies的设置;<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(0,128,0)">//现在服务器端创建一个cookie</span><br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Cookie myCookie=new Cookie("color1","red");<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(0,128,0)">// 该cookie存在的时间</span><br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; myCookie.setMaxAge(30);<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;<span style="color: rgb(0,128,0)"> //如果你不设置存在时间,那么该cookie将不会保存</span><br style="color: rgb(0,128,0)" />
<span style="color: rgb(0,128,0)">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //将该cookie写回到客户端</span><br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; res.addCookie(myCookie);<br />
&nbsp;&nbsp;&nbsp;cookies的获取：<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; <span style="color: rgb(0,128,0)">//从客户端得到所有cookie信息</span><br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Cookie [] allCookies=req.getCookies();<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; int i=0;<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(0,128,0)">//如果allCookies不为空...</span><br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if(allCookies!=null){<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(0,128,0)">//从中取出cookie</span><br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for(i=0;i&lt;allCookies.length;i++){<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(0,128,0)">//依次取出</span><br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Cookie temp=allCookies[i];<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp;cookies的删除：<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; <span style="color: rgb(0,128,0)">//将该cookie删除</span><br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; temp.setMaxAge(0);<br />
3、servletcontext的设置：<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<span style="color: rgb(0,128,0)">//得到servletcontext</span><br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;ServletContext sc=this.getServletContext();<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp; <span style="color: rgb(0,128,0)">//添加属性 </span><br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;sc.setAttribute("myInfo","我是顺平");<br />
&nbsp;&nbsp;&nbsp;servletcontext的获取：<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;<span style="color: rgb(0,128,0)">//得到servlet context</span><br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp; ServletContext sc=this.getServletContext();<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(0,128,0)">//得到属性和它对应的值</span><br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; String info=(String)sc.getAttribute("myInfo");<br />
 <img src ="http://www.blogjava.net/aczreg/aggbug/306069.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/aczreg/" target="_blank">咖啡企鹅</a> 2009-12-15 21:58 <a href="http://www.blogjava.net/aczreg/archive/2009/12/15/306069.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>