JSP 中的 Cookie 及示例
什么是 Cookie?
- Cookie 是存储在客户端机器上的文本文件。
- 它们用于跟踪各种目的的信息。
- 它支持使用 Servlet 技术处理 HTTP Cookie
- Cookie 设置在 HTTP Header 中。
- 如果浏览器配置为存储 Cookie,它会保存信息直到过期日期。
JSP 中的 Cookie 类型
- 持久性 Cookie:持久性 Cookie 会在设定的时间内保存在您的设备上,帮助网站记住您的偏好和登录详细信息。
- 非持久性 Cookie:非持久性 Cookie 是临时的,一旦关闭浏览器就会被删除,主要用于会话跟踪。
JSP Cookie 方法
以下是 Cookie 的方法
-
Public void setDomain(String domain)
此 JSP set cookie 用于设置 Cookie 所适用的域
-
Public String getDomain()
此 JSP get cookie 用于获取 Cookie 所适用的域
-
Public void setMaxAge(int expiry)
它设置了 Cookie 过期前的最长时间
-
Public int getMaxAge()
它返回 JSP 中 Cookie 的最大年龄
-
Public String getName()
它返回 Cookie 的名称
-
Public void setValue(String value)
设置与 Cookie 相关联的值
-
Public String getValue()
获取与 Cookie 相关联的值
-
Public void setPath(String path)
此 JSP set cookie 设置 Cookie 所适用的路径
-
Public String getPath()
它获取 Cookie 所适用的路径
-
Public void setSecure(Boolean flag)
它是否应通过加密连接发送。
-
Public void setComment(String cmt)
它描述了 Cookie 的目的
-
Public String getComment()
它返回已描述的 Cookie 注释。
如何在 JSP 中处理 Cookie
- 创建 Cookie 对象
- 设置最大年龄
- 在 HTTP 响应头中发送 Cookie
示例
在此 JSP Cookie 示例中,我们将学习如何在 JSP 中调用 Cookie 构造函数,创建用户名和电子邮件的 Cookie,并将年龄设置为 10 小时,然后在 action_cookie.jsp 中尝试获取变量名。
Action_cookie.jsp。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Guru Cookie</title> </head> <body> <form action="action_cookie_main.jsp" method="GET"> Username: <input type="text" name="username"> <br /> Email: <input type="text" name="email" /> <input type="submit" value="Submit" /> </form> </body> </html>
Action_cookie_main.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <% Cookie username = new Cookie("username", request.getParameter("username")); Cookie email = new Cookie("email", request.getParameter("email")); username.setMaxAge(60*60*10); email.setMaxAge(60*60*10); // Add both the cookies in the response header. response.addCookie( username ); response.addCookie( email ); %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Guru Cookie JSP</title> </head> <body> <b>Username:</b> <%= request.getParameter("username")%> <b>Email:</b> <%= request.getParameter("email")%> </body> </html>
代码解释
Action_cookie.jsp
代码第 10-15 行:这里我们有一个表单,将在 action_cookie_main.jsp 中处理。此外,我们还提供了两个字段“username”和“email”,需要用户输入,并有一个提交按钮。
Action_cookie_main.jsp
代码第 6-9 行:使用 request.getParameter 创建了两个 Cookie 对象:“username”和“email”。
代码第 12-13 行:这里我们为两个 Cookie 添加了年龄,设置为 10 小时,即 Cookie 将在此年龄后过期。
代码第 16-17 行:将用户名和电子邮件 Cookie 添加到会话中,这两个 Cookie 可以在 getParameter() 请求时被获取。
输出
当您执行上述代码时,您会得到以下输出
当我们执行 action_cookie.jsp 时,我们会看到 username 和 email 两个字段,它接受用户输入,然后我们单击提交按钮。我们从 action_cookie_main.jsp 中获取输出,其中变量被存储在客户端的 Cookie JSP 中。