jsp源码实例5(cookie)

2016-01-29 12:05 7 1 收藏

jsp源码实例5(cookie),jsp源码实例5(cookie)

【 tulaoshi.com - Java 】

  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 + "</H1n" +
"There are six cookies associated with this page.n" +
"To see them, visit then" +
"<A HREF="/servlet/coreservlets.ShowCookies"n" +
"<CODEShowCookies</CODE servlet</A.n" +
"<Pn" +
"Three of the cookies are associated only with then" +
"current session, while three are persistent.n" +
"Quit the browser, restart, and return to then" +
"<CODEShowCookies</CODE servlet to verify thatn" +
"the three long-lived ones persist across sessions.n" +
"</BODY</HTML");
}
}
 

来源:http://www.tulaoshi.com/n/20160129/1484537.html

延伸阅读
标签: Java JAVA基础
 一、 前言 说起来,Cookie应该是一种应用较久的技术了。早在HTML刚刚出现的时候,在每个独立的页面之间没有办法记录和标识不同的用户。后来人们就发明了Cookie技术,当用户访问网页时,它能够在访问者的机器上创立一个文件,我们把它叫作Cookie,写一段内容进去,来标识不同的用户。如果下次用户再访问这个网页的时候,它又能够读出...
标签: Web开发
说起来,Cookie应该是一种应用较久的技术了。早在HTML刚刚出现的时候,在每个独立的页面之间没有办法记录和标识不同的用户。后来人们就发明了Cookie技术,当用户访问网页时,它能够在访问者的机器上创立一个文件,我们把它叫作Cookie,写一段内容进去,来标识不同的用户。如果下次用户再访问这个网页的时候,它又能够读出这个文件里面的内...
标签: Java JAVA基础
页面底部提供了一个按钮,用户可以用它来停止或重新启动任务: <TR             <TD ALIGN="CENTER"                 <BR           ...
标签: Java JAVA基础
  vishal_donth gave this response on 10/18/2000:   //these are the pakages to be imported from   // Java Mail   //The Java Mail PAckage either be dowloaded   //seperately   //or else is Available in the J2sdkee1.2   // (Java Enterprise Edition) &nbs...
标签: Web开发
经常看到jsp的初学者问tomcat下如何配置jsp、servlet和bean的问题,于是总结了一下如何tomcat下配置jsp、servlet和ben,希望对那些初学者有所帮助。 一、开发环境配置 第一步:下载j2sdk和tomcat:到sun官方站(http://java.sun.com/j2se/1.5.0/download.jsp)下载j2sdk,注意下载版本为Windows Offline Installation的S...

经验教程

391

收藏

41

精华推荐

微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部