📄 1007402.xml
字号:
<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>你看看我理解的对不对
<%
  String action = (String)request.getParameter("action") ;
  if ( action != null && action.equals("userlogin") ) 
  {
      String timeZone  = (String)request.getParameter("timeZone") ;
      String userName  = (String)request.getParameter("userName") ;
//取提交过来的变量
      Cookie cookie2=new Cookie("USERNAME", userName);
      Cookie cookie3=new Cookie("TIMEZONE", timeZone);
//设定了2个cookie
      cookie2.setMaxAge(365*24*60*60);
      cookie3.setMaxAge(365*24*60*60);
//设定cookie的有效时间是一年
      response.addCookie(cookie2);
      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 cookies[]=request.getCookies();
//取出cookies值,并放到数组cookies里
   int j = (cookies==null )?0:cookies.length ;
//着句我就不明白了
   for ( int i = 0 ; i < j ;i++ )
   {
     if ( cookies[i].getName().equals("USERNAME") ) 
        userName = cookies[i].getValue();
     else if ( cookies[i].getName().equals("TIMEZONE") ) 
        timeZone = cookies[i].getValue();
//根据cookie的名字在数组中查找其数值.还有那个else是干什么的?
   }
下面是我自己写的一段放在另一个页面。如果不登陆直接访问就会出错!
再次刷新就会显示,可是显示结果总一些乱七八糟的数据。奇怪的是if(myCookie.length > 0)没起作用!
<%
Cookie myCookie[]=request.getCookies();//本人认为可能是着句出的问题
if(myCookie.length > 0){
	for (int i =0; i < myCookie.length; i++){
Cookie newCookie= myCookie[i];
//username = newCookie.getValue();
out.print(newCookie.getName());
out.print(newCookie.getValue());
	}
	}
%>
该页面位置: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 coreservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/** Sets six cookies: three that apply only to the current
 *  session (regardless of how long that session lasts)
 *  and three that persist for an hour (regardless of
 *  whether the browser is restarted).
 *  <P>
 *  Taken from Core Servlets and JavaServer Pages
 *  from Prentice Hall and Sun Microsystems Press,
 *  http://www.coreservlets.com/.
 *  &copy; 2000 Marty Hall; may be freely used or adapted.
 */
public class SetCookies extends HttpServlet {
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {
    for(int i=0; i<3; i++) {
      // Default maxAge is -1, indicating cookie
      // applies only to current browsing session.
      Cookie cookie = new Cookie("Session-Cookie-" + i,
                                 "Cookie-Value-S" + i);
      response.addCookie(cookie);
      cookie = new Cookie("Persistent-Cookie-" + i,
                          "Cookie-Value-P" + i);
      // Cookie is valid for an hour, regardless of whether
      // user quits browser, reboots computer, or whatever.
      cookie.setMaxAge(3600);
      response.addCookie(cookie);
    } 
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String title = "Setting Cookies";
    out.println
      (ServletUtilities.headWithTitle(title) +
       "<BODY BGCOLOR=\"#FDF5E6\">\n" +
       "<H1 ALIGN=\"CENTER\">" + title + "</H1>\n" +
       "There are six cookies associated with this page.\n" +
       "To see them, visit the\n" +
       "<A HREF=\"/servlet/coreservlets.ShowCookies\">\n" +
       "<CODE>ShowCookies</CODE> servlet</A>.\n" +
       "<P>\n" +
       "Three of the cookies are associated only with the\n" +
       "current session, while three are persistent.\n" +
       "Quit the browser, restart, and return to the\n" +
       "<CODE>ShowCookies</CODE> servlet to verify that\n" +
       "the three long-lived ones persist across sessions.\n" +
       "</BODY></HTML>");
  }
}</Content>
<PostDateTime>2002-9-9 19:19:11</PostDateTime>
</Reply>
</Replys>
</Topic>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -