⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 1007402.xml

📁 论坛精华帖子
💻 XML
📖 第 1 页 / 共 2 页
字号:
<credit>100</credit>
<ReplyID>6491589</ReplyID>
<TopicID>1007402</TopicID>
<PostUserId>252521</PostUserId>
<PostUserName>hazeline</PostUserName>
<Point>0</Point>
<Content>session.setMaxInactiveInterval(-1);不过期。
但是我实用下来好像还是会过期的,不过时间比较长,用户如果这么长时间不动网页的话,很有可能是离开了,这样过期反而是好事,安全</Content>
<PostDateTime>2002-9-9 9:35:55</PostDateTime>
</Reply>
<Reply>
<PostUserNickName>冰风</PostUserNickName>
<rank>一级(初级)</rank>
<ranknum>user1</ranknum>
<credit>100</credit>
<ReplyID>6497528</ReplyID>
<TopicID>1007402</TopicID>
<PostUserId>261682</PostUserId>
<PostUserName>fightplane</PostUserName>
<Point>0</Point>
<Content>多谢二位的指点,小弟还有些不太明白。
希望虚竹和尚在程序上加上注释。
hazeline能否把session的时间设置写的详细些</Content>
<PostDateTime>2002-9-9 14:05:45</PostDateTime>
</Reply>
<Reply>
<PostUserNickName>)虚竹和尚(</PostUserNickName>
<rank>两星(中级)</rank>
<ranknum>star2</ranknum>
<credit>100</credit>
<ReplyID>6497952</ReplyID>
<TopicID>1007402</TopicID>
<PostUserId>62467</PostUserId>
<PostUserName>xycleo</PostUserName>
<Point>0</Point>
<Content>你那个地方不明白?现在在上班,没有时间写太多注释
</Content>
<PostDateTime>2002-9-9 14:24:32</PostDateTime>
</Reply>
<Reply>
<PostUserNickName>冰风</PostUserNickName>
<rank>一级(初级)</rank>
<ranknum>user1</ranknum>
<credit>100</credit>
<ReplyID>6498823</ReplyID>
<TopicID>1007402</TopicID>
<PostUserId>261682</PostUserId>
<PostUserName>fightplane</PostUserName>
<Point>0</Point>
<Content>你看看我理解的对不对
&lt;%
&#32;&#32;String&#32;action&#32;=&#32;(String)request.getParameter("action")&#32;;
&#32;&#32;if&#32;(&#32;action&#32;!=&#32;null&#32;&amp;&amp;&#32;action.equals("userlogin")&#32;)&#32;
&#32;&#32;{
&#32;&#32;&#32;&#32;&#32;&#32;String&#32;timeZone&#32;&#32;=&#32;(String)request.getParameter("timeZone")&#32;;
&#32;&#32;&#32;&#32;&#32;&#32;String&#32;userName&#32;&#32;=&#32;(String)request.getParameter("userName")&#32;;
//取提交过来的变量
&#32;&#32;&#32;&#32;&#32;&#32;Cookie&#32;cookie2=new&#32;Cookie("USERNAME",&#32;userName);
&#32;&#32;&#32;&#32;&#32;&#32;Cookie&#32;cookie3=new&#32;Cookie("TIMEZONE",&#32;timeZone);
//设定了2个cookie
&#32;&#32;&#32;&#32;&#32;&#32;cookie2.setMaxAge(365*24*60*60);
&#32;&#32;&#32;&#32;&#32;&#32;cookie3.setMaxAge(365*24*60*60);
//设定cookie的有效时间是一年
&#32;&#32;&#32;&#32;&#32;&#32;response.addCookie(cookie2);
&#32;&#32;&#32;&#32;&#32;&#32;response.addCookie(cookie3);
//把cookie加入到Set-Cookie应答头(是不是说cookie开始生效了呢?如果不加response.addCookie(cookie3);cookie就不生效?)
.....</Content>
<PostDateTime>2002-9-9 15:04:19</PostDateTime>
</Reply>
<Reply>
<PostUserNickName>)虚竹和尚(</PostUserNickName>
<rank>两星(中级)</rank>
<ranknum>star2</ranknum>
<credit>100</credit>
<ReplyID>6500313</ReplyID>
<TopicID>1007402</TopicID>
<PostUserId>62467</PostUserId>
<PostUserName>xycleo</PostUserName>
<Point>0</Point>
<Content>对阿</Content>
<PostDateTime>2002-9-9 16:15:04</PostDateTime>
</Reply>
<Reply>
<PostUserNickName>冰风</PostUserNickName>
<rank>一级(初级)</rank>
<ranknum>user1</ranknum>
<credit>100</credit>
<ReplyID>6501500</ReplyID>
<TopicID>1007402</TopicID>
<PostUserId>261682</PostUserId>
<PostUserName>fightplane</PostUserName>
<Point>0</Point>
<Content>Cookie&#32;cookies[]=request.getCookies();
//取出cookies值,并放到数组cookies里
&#32;&#32;&#32;int&#32;j&#32;=&#32;(cookies==null&#32;)?0:cookies.length&#32;;
//着句我就不明白了
&#32;&#32;&#32;for&#32;(&#32;int&#32;i&#32;=&#32;0&#32;;&#32;i&#32;&lt;&#32;j&#32;;i++&#32;)
&#32;&#32;&#32;{
&#32;&#32;&#32;&#32;&#32;if&#32;(&#32;cookies[i].getName().equals("USERNAME")&#32;)&#32;
&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;userName&#32;=&#32;cookies[i].getValue();
&#32;&#32;&#32;&#32;&#32;else&#32;if&#32;(&#32;cookies[i].getName().equals("TIMEZONE")&#32;)&#32;
&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;timeZone&#32;=&#32;cookies[i].getValue();
//根据cookie的名字在数组中查找其数值.还有那个else是干什么的?
&#32;&#32;&#32;}
下面是我自己写的一段放在另一个页面。如果不登陆直接访问就会出错!
再次刷新就会显示,可是显示结果总一些乱七八糟的数据。奇怪的是if(myCookie.length&#32;&gt;&#32;0)没起作用!
&lt;%
Cookie&#32;myCookie[]=request.getCookies();//本人认为可能是着句出的问题
if(myCookie.length&#32;&gt;&#32;0){
&#9;for&#32;(int&#32;i&#32;=0;&#32;i&#32;&lt;&#32;myCookie.length;&#32;i++){
Cookie&#32;newCookie=&#32;myCookie[i];
//username&#32;=&#32;newCookie.getValue();
out.print(newCookie.getName());
out.print(newCookie.getValue());
&#9;}
&#9;}
%&gt;
该页面位置:http://211.140.206.134/zxp/1.jsp</Content>
<PostDateTime>2002-9-9 17:11:39</PostDateTime>
</Reply>
<Reply>
<PostUserNickName>李南风</PostUserNickName>
<rank>二级(初级)</rank>
<ranknum>user2</ranknum>
<credit>100</credit>
<ReplyID>6504009</ReplyID>
<TopicID>1007402</TopicID>
<PostUserId>214255</PostUserId>
<PostUserName>maxpain</PostUserName>
<Point>40</Point>
<Content>package&#32;coreservlets;

import&#32;java.io.*;
import&#32;javax.servlet.*;
import&#32;javax.servlet.http.*;

/**&#32;Sets&#32;six&#32;cookies:&#32;three&#32;that&#32;apply&#32;only&#32;to&#32;the&#32;current
&#32;*&#32;&#32;session&#32;(regardless&#32;of&#32;how&#32;long&#32;that&#32;session&#32;lasts)
&#32;*&#32;&#32;and&#32;three&#32;that&#32;persist&#32;for&#32;an&#32;hour&#32;(regardless&#32;of
&#32;*&#32;&#32;whether&#32;the&#32;browser&#32;is&#32;restarted).
&#32;*&#32;&#32;&lt;P&gt;
&#32;*&#32;&#32;Taken&#32;from&#32;Core&#32;Servlets&#32;and&#32;JavaServer&#32;Pages
&#32;*&#32;&#32;from&#32;Prentice&#32;Hall&#32;and&#32;Sun&#32;Microsystems&#32;Press,
&#32;*&#32;&#32;http://www.coreservlets.com/.
&#32;*&#32;&#32;&amp;copy;&#32;2000&#32;Marty&#32;Hall;&#32;may&#32;be&#32;freely&#32;used&#32;or&#32;adapted.
&#32;*/

public&#32;class&#32;SetCookies&#32;extends&#32;HttpServlet&#32;{
&#32;&#32;public&#32;void&#32;doGet(HttpServletRequest&#32;request,
&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;HttpServletResponse&#32;response)
&#32;&#32;&#32;&#32;&#32;&#32;throws&#32;ServletException,&#32;IOException&#32;{
&#32;&#32;&#32;&#32;for(int&#32;i=0;&#32;i&lt;3;&#32;i++)&#32;{
&#32;&#32;&#32;&#32;&#32;&#32;//&#32;Default&#32;maxAge&#32;is&#32;-1,&#32;indicating&#32;cookie
&#32;&#32;&#32;&#32;&#32;&#32;//&#32;applies&#32;only&#32;to&#32;current&#32;browsing&#32;session.
&#32;&#32;&#32;&#32;&#32;&#32;Cookie&#32;cookie&#32;=&#32;new&#32;Cookie("Session-Cookie-"&#32;+&#32;i,
&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;"Cookie-Value-S"&#32;+&#32;i);
&#32;&#32;&#32;&#32;&#32;&#32;response.addCookie(cookie);
&#32;&#32;&#32;&#32;&#32;&#32;cookie&#32;=&#32;new&#32;Cookie("Persistent-Cookie-"&#32;+&#32;i,
&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;"Cookie-Value-P"&#32;+&#32;i);
&#32;&#32;&#32;&#32;&#32;&#32;//&#32;Cookie&#32;is&#32;valid&#32;for&#32;an&#32;hour,&#32;regardless&#32;of&#32;whether
&#32;&#32;&#32;&#32;&#32;&#32;//&#32;user&#32;quits&#32;browser,&#32;reboots&#32;computer,&#32;or&#32;whatever.
&#32;&#32;&#32;&#32;&#32;&#32;cookie.setMaxAge(3600);
&#32;&#32;&#32;&#32;&#32;&#32;response.addCookie(cookie);
&#32;&#32;&#32;&#32;}&#32;
&#32;&#32;&#32;&#32;response.setContentType("text/html");
&#32;&#32;&#32;&#32;PrintWriter&#32;out&#32;=&#32;response.getWriter();
&#32;&#32;&#32;&#32;String&#32;title&#32;=&#32;"Setting&#32;Cookies";
&#32;&#32;&#32;&#32;out.println
&#32;&#32;&#32;&#32;&#32;&#32;(ServletUtilities.headWithTitle(title)&#32;+
&#32;&#32;&#32;&#32;&#32;&#32;&#32;"&lt;BODY&#32;BGCOLOR=\"#FDF5E6\"&gt;\n"&#32;+
&#32;&#32;&#32;&#32;&#32;&#32;&#32;"&lt;H1&#32;ALIGN=\"CENTER\"&gt;"&#32;+&#32;title&#32;+&#32;"&lt;/H1&gt;\n"&#32;+
&#32;&#32;&#32;&#32;&#32;&#32;&#32;"There&#32;are&#32;six&#32;cookies&#32;associated&#32;with&#32;this&#32;page.\n"&#32;+
&#32;&#32;&#32;&#32;&#32;&#32;&#32;"To&#32;see&#32;them,&#32;visit&#32;the\n"&#32;+
&#32;&#32;&#32;&#32;&#32;&#32;&#32;"&lt;A&#32;HREF=\"/servlet/coreservlets.ShowCookies\"&gt;\n"&#32;+
&#32;&#32;&#32;&#32;&#32;&#32;&#32;"&lt;CODE&gt;ShowCookies&lt;/CODE&gt;&#32;servlet&lt;/A&gt;.\n"&#32;+
&#32;&#32;&#32;&#32;&#32;&#32;&#32;"&lt;P&gt;\n"&#32;+
&#32;&#32;&#32;&#32;&#32;&#32;&#32;"Three&#32;of&#32;the&#32;cookies&#32;are&#32;associated&#32;only&#32;with&#32;the\n"&#32;+
&#32;&#32;&#32;&#32;&#32;&#32;&#32;"current&#32;session,&#32;while&#32;three&#32;are&#32;persistent.\n"&#32;+
&#32;&#32;&#32;&#32;&#32;&#32;&#32;"Quit&#32;the&#32;browser,&#32;restart,&#32;and&#32;return&#32;to&#32;the\n"&#32;+
&#32;&#32;&#32;&#32;&#32;&#32;&#32;"&lt;CODE&gt;ShowCookies&lt;/CODE&gt;&#32;servlet&#32;to&#32;verify&#32;that\n"&#32;+
&#32;&#32;&#32;&#32;&#32;&#32;&#32;"the&#32;three&#32;long-lived&#32;ones&#32;persist&#32;across&#32;sessions.\n"&#32;+
&#32;&#32;&#32;&#32;&#32;&#32;&#32;"&lt;/BODY&gt;&lt;/HTML&gt;");
&#32;&#32;}
}</Content>
<PostDateTime>2002-9-9 19:19:11</PostDateTime>
</Reply>
</Replys>
</Topic>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -