Cookie & Session
1、Cookie
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | /** * Creates a cookie, a small amount of information sent by a servlet to a Web browser, saved by the browser, and later sent back to the server. A cookie's value can uniquely identify a client, so cookies are commonly used for * session management. * <p> * A cookie has a name, a single value, and optional attributes such as a comment, path and domain qualifiers, a maximum age, and a version number. Some Web browsers have bugs in how they handle the optional attributes, so * use them sparingly to improve the interoperability of your servlets. * <p> * The servlet sends cookies to the browser by using the {@link HttpServletResponse#addCookie} method, which adds fields to HTTP response headers to send cookies to the browser, one at a time. The browser is expected to <br> * support 20 cookies for each Web server, 300 cookies total, and may limit cookie size to 4 KB each. * <p> * The browser returns cookies to the servlet by adding fields to HTTP request headers. Cookies can be retrieved from a request by using the {@link HttpServletRequest#getCookies} method. Several cookies might have the * same name but different path attributes. * <p> * Cookies affect the caching of the Web pages that use them. HTTP 1.0 does not cache pages that use cookies created with this class. This class does not support the cache control defined with HTTP 1.1. * <p> * This class supports both the RFC 2109 and the RFC 6265 specifications. By default, cookies are created using RFC 6265. */ public class Cookie implements Cloneable, Serializable {<br>} |
2、Session
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | /** * Provides a way to identify a user across more than one page request or visit to a Web site and to store information about that user. * <p> * The servlet container uses this interface to create a session between an HTTP client and an HTTP server. The session persists for a specified time period, across more than one connection or page request from the user. A session * usually corresponds to one user, who may visit a site many times. The server can maintain a session in many ways such as using cookies or rewriting URLs. * <p> * This interface allows servlets to * <ul> * <li>View and manipulate information about a session, such as the session identifier, creation time, and last accessed time Bind objects to sessions, allowing user information to persist across multiple user connections * </ul> * <p> * When an application stores an object in or removes an object from a session, the session checks whether the object implements {@link HttpSessionBindingListener}. If it does, the servlet notifies the object that it has been bound to or unbound from the session. Notifications * are sent after the binding methods complete. For session that are invalidated or expire, notifications are sent after the session has been invalidated or expired. * <p> * When container migrates a session between VMs in a distributed container * setting, all session attributes implementing the * {@link HttpSessionActivationListener} interface are notified. * <p> * A servlet should be able to handle cases in which the client does not choose to join a session, such as when cookies are intentionally turned off. Until the client joins the session, <code>isNew</code> returns <code>true</code>. * If the client chooses not to join the session, <code>getSession</code> will return a different session on each request, and <code>isNew</code> will always return <code>true</code>. * <p> * Session information is scoped only to the current web application ( <code>ServletContext</code>), so information stored in one context will not be directly visible in another. * * @see HttpSessionBindingListener */ public interface HttpSession { /** * Returns the time when this session was created, measured in milliseconds * since midnight January 1, 1970 GMT. * * @return a <code>long</code> specifying when this session was created, * expressed in milliseconds since 1/1/1970 GMT * @exception IllegalStateException * if this method is called on an invalidated session */ public long getCreationTime(); } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通