⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 0080.htm

📁 JspServlet教程专栏 对javaservlet讲述的非常详细
💻 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>
 *&nbsp;&nbsp;session (regardless of how long that session lasts)<br>
 *&nbsp;&nbsp;and three that persist for an hour (regardless of<br>
 *&nbsp;&nbsp;whether the browser is restarted).<br>
 *&nbsp;&nbsp;&lt;P&gt;<br>
 *&nbsp;&nbsp;Taken from Core Servlets and JavaServer Pages<br>
 *&nbsp;&nbsp;from Prentice Hall and Sun Microsystems Press,<br>
 *&nbsp;&nbsp;http://www.coreservlets.com/.<br>
 *&nbsp;&nbsp;&amp;copy; 2000 Marty Hall; may be freely used or adapted.<br>
 */<br>
<br>
public class SetCookies extends HttpServlet {<br>
&nbsp;&nbsp;public void doGet(HttpServletRequest request,<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HttpServletResponse response)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throws ServletException, IOException {<br>
&nbsp;&nbsp;&nbsp;&nbsp;for(int i=0; i&lt;3; i++) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Default maxAge is -1, indicating cookie<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// applies only to current browsing session.<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Cookie cookie = new Cookie(&quot;Session-Cookie-&quot; + i,<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;Cookie-Value-S&quot; + i);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;response.addCookie(cookie);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cookie = new Cookie(&quot;Persistent-Cookie-&quot; + i,<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;Cookie-Value-P&quot; + i);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Cookie is valid for an hour, regardless of whether<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// user quits browser, reboots computer, or whatever.<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cookie.setMaxAge(3600);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;response.addCookie(cookie);<br>
&nbsp;&nbsp;&nbsp;&nbsp;} <br>
&nbsp;&nbsp;&nbsp;&nbsp;response.setContentType(&quot;text/html&quot;);<br>
&nbsp;&nbsp;&nbsp;&nbsp;PrintWriter out = response.getWriter();<br>
&nbsp;&nbsp;&nbsp;&nbsp;String title = &quot;Setting Cookies&quot;;<br>
&nbsp;&nbsp;&nbsp;&nbsp;out.println<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(ServletUtilities.headWithTitle(title) +<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;&lt;BODY BGCOLOR=\&quot;#FDF5E6\&quot;&gt;\n&quot; +<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;&lt;H1 ALIGN=\&quot;CENTER\&quot;&gt;&quot; + title + &quot;&lt;/H1&gt;\n&quot; +<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;There are six cookies associated with this page.\n&quot; +<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;To see them, visit the\n&quot; +<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;&lt;A HREF=\&quot;/servlet/coreservlets.ShowCookies\&quot;&gt;\n&quot; +<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;&lt;CODE&gt;ShowCookies&lt;/CODE&gt; servlet&lt;/A&gt;.\n&quot; +<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;&lt;P&gt;\n&quot; +<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;Three of the cookies are associated only with the\n&quot; +<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;current session, while three are persistent.\n&quot; +<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;Quit the browser, restart, and return to the\n&quot; +<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;&lt;CODE&gt;ShowCookies&lt;/CODE&gt; servlet to verify that\n&quot; +<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;the three long-lived ones persist across sessions.\n&quot; +<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;&lt;/BODY&gt;&lt;/HTML&gt;&quot;);<br>
&nbsp;&nbsp;}<br>
}<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>
</p>

  </table>
<p align="center"><script src="../../2.js"></script></a>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -