?? 0080.htm
字號:
<html>
<head>
<title>新時代軟件教程:操作系統 主頁制作 服務器 設計軟件 網絡技術 編程語言 文字編輯</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style>
<!--
body, table {font-size: 9pt; font-family: 宋體}
a {text-decoration:none}
a:hover {color: red;text-decoration:underline}
.1 {background-color: rgb(245,245,245)}
-->
</style>
</head>
<p align="center"><script src="../../1.js"></script></a>
<p align="center"><big><strong>jsp源碼實例4(cookie)</strong></big></p>
<div align="right">(文/blackwhites)</div>
<p>
package coreservlets;<br>
<br>
import java.io.*;<br>
import javax.servlet.*;<br>
import javax.servlet.http.*;<br>
<br>
/** Sets six cookies: three that apply only to the current<br>
* session (regardless of how long that session lasts)<br>
* and three that persist for an hour (regardless of<br>
* whether the browser is restarted).<br>
* <P><br>
* Taken from Core Servlets and JavaServer Pages<br>
* from Prentice Hall and Sun Microsystems Press,<br>
* http://www.coreservlets.com/.<br>
* &copy; 2000 Marty Hall; may be freely used or adapted.<br>
*/<br>
<br>
public class SetCookies extends HttpServlet {<br>
public void doGet(HttpServletRequest request,<br>
HttpServletResponse response)<br>
throws ServletException, IOException {<br>
for(int i=0; i<3; i++) {<br>
// Default maxAge is -1, indicating cookie<br>
// applies only to current browsing session.<br>
Cookie cookie = new Cookie("Session-Cookie-" + i,<br>
"Cookie-Value-S" + i);<br>
response.addCookie(cookie);<br>
cookie = new Cookie("Persistent-Cookie-" + i,<br>
"Cookie-Value-P" + i);<br>
// Cookie is valid for an hour, regardless of whether<br>
// user quits browser, reboots computer, or whatever.<br>
cookie.setMaxAge(3600);<br>
response.addCookie(cookie);<br>
} <br>
response.setContentType("text/html");<br>
PrintWriter out = response.getWriter();<br>
String title = "Setting Cookies";<br>
out.println<br>
(ServletUtilities.headWithTitle(title) +<br>
"<BODY BGCOLOR=\"#FDF5E6\">\n" +<br>
"<H1 ALIGN=\"CENTER\">" + title + "</H1>\n" +<br>
"There are six cookies associated with this page.\n" +<br>
"To see them, visit the\n" +<br>
"<A HREF=\"/servlet/coreservlets.ShowCookies\">\n" +<br>
"<CODE>ShowCookies</CODE> servlet</A>.\n" +<br>
"<P>\n" +<br>
"Three of the cookies are associated only with the\n" +<br>
"current session, while three are persistent.\n" +<br>
"Quit the browser, restart, and return to the\n" +<br>
"<CODE>ShowCookies</CODE> servlet to verify that\n" +<br>
"the three long-lived ones persist across sessions.\n" +<br>
"</BODY></HTML>");<br>
}<br>
}<br>
<br>
</p>
</table>
<p align="center"><script src="../../2.js"></script></a>
</body>
</html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -