jsp源碼實(shí)例5(cookie)
更新時(shí)間: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>");
}
}
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)文章
Servlet與JSP使用簡(jiǎn)介及區(qū)別詳解
這篇文章主要為大家介紹了Servlet與JSP使用簡(jiǎn)介及區(qū)別示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
jsp中文顯示問(wèn)號(hào)問(wèn)題解決方法
jsp中想要輸出的中文被顯示成問(wèn)號(hào)?在eclipse-windows- preferences中搜索jsp,Encoding選項(xiàng)中選擇 Chinese,此問(wèn)題便可解決2014-03-03
JSP實(shí)用教程之簡(jiǎn)易圖片驗(yàn)證碼的實(shí)現(xiàn)方法(附源碼)
圖片驗(yàn)證碼對(duì)大家來(lái)說(shuō)應(yīng)該再熟悉不過(guò)了,而圖片驗(yàn)證碼的實(shí)現(xiàn)主要的技術(shù)點(diǎn)是如何生成一個(gè)圖片,下面這篇文章主要跟大家介紹了關(guān)于JSP實(shí)用教程之實(shí)現(xiàn)簡(jiǎn)易圖片驗(yàn)證碼的方法,文中介紹的非常詳細(xì),需要的朋友們下面來(lái)一起看看吧。2017-07-07
JSP學(xué)習(xí)經(jīng)驗(yàn)小結(jié)分享
本文介紹的是JSP的學(xué)習(xí)經(jīng)驗(yàn)總結(jié),希望對(duì)你有幫助,一起來(lái)看。2015-09-09
SSM框架JSP使用Layui實(shí)現(xiàn)layer彈出層效果
這篇文章主要介紹了SSM框架JSP使用Layui實(shí)現(xiàn)layer彈出層效果,文章通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
JSP實(shí)現(xiàn)的簡(jiǎn)單分頁(yè)示例
這篇文章主要介紹了JSP實(shí)現(xiàn)的簡(jiǎn)單分頁(yè),涉及jsp簡(jiǎn)單數(shù)據(jù)庫(kù)查找及遍歷實(shí)現(xiàn)分頁(yè)的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
JSP+Servlet實(shí)現(xiàn)文件上傳到服務(wù)器功能
這篇文章主要為大家詳細(xì)介紹了JSP+Servlet實(shí)現(xiàn)文件上傳到服務(wù)器功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09

