jsp實現(xiàn)cookie的使用
更新時間:2006年10月13日 00:00:00 作者:
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>");
}
}
相關(guān)文章
jsp防止跨域提交數(shù)據(jù)的具體實現(xiàn)
這篇文章主要介紹了jsp防止跨域提交數(shù)據(jù)的具體實現(xiàn),需要的朋友可以參考下2014-02-02struts2+jquery實現(xiàn)ajax登陸實例詳解
這篇文章主要介紹了struts2+jquery實現(xiàn)ajax登陸,需要的朋友可以參考下2014-07-07通過Setters方式對日期屬性及日期格式進(jìn)行IOC注入
Spring不直接支持對Date類型的IOC依賴關(guān)系的注入,而提供了類似于Struts中的converte一樣的屬性編輯器,需要程序員自己寫相應(yīng)的類,并將其注冊。2009-08-08java 中Spring task定時任務(wù)的深入理解
這篇文章主要介紹了java 中Spring task定時任務(wù)的深入理解的相關(guān)資料,這里提供實例來幫助大家理解task定時任務(wù),希望大家通過本文掌握這部分內(nèi)容,需要的朋友可以參考下2017-09-09