欢迎光临
我们一直在努力

jsp源码实例4(cookie)-JSP教程,Jsp/Servlet

建站超值云服务器,限时71元/月

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/.
*  © 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>");
  }
}
       

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » jsp源码实例4(cookie)-JSP教程,Jsp/Servlet
分享到: 更多 (0)