{"id":5951,"date":"2024-04-17T00:42:19","date_gmt":"2024-04-16T16:42:19","guid":{"rendered":"http:\/\/zyg.luchers.cn\/?p=5951"},"modified":"2024-04-17T00:42:19","modified_gmt":"2024-04-16T16:42:19","slug":"%e5%8d%81%e3%80%81cookie%e5%92%8csession","status":"publish","type":"post","link":"http:\/\/zyg.luchers.cn\/index.php\/2024\/04\/17\/%e5%8d%81%e3%80%81cookie%e5%92%8csession\/","title":{"rendered":"\u5341\u3001Cookie\u548cSession"},"content":{"rendered":"<h1>\u5341\u56db\u3001Cookie\u548cSession<\/h1>\n<h2>1\u3001Cookie\u997c\u5e72<\/h2>\n<h3>1.1\u3001\u4ec0\u4e48\u662fCookie\uff1f<\/h3>\n<h4>1\u3001\u5185\u5bb9<\/h4>\n<p>1\u3001Cookie\u7ffb\u8bd1\u662f\u610f\u601d\u662f\u997c\u5e72<\/p>\n<p>2\u3001Cookie \u662f\u670d\u52a1\u5668\u901a\u77e5\u5ba2\u6237\u7aef\u4fdd\u5b58\u952e\u503c\u5bf9\u7684\u4e00\u79cd\u6280\u672f<\/p>\n<p>3\u3001\u5ba2\u6237\u7aef\u6709\u4e86Cookie\u540e\uff0c\u6bcf\u6b21\u8bf7\u6c42\u90fd\u4f1a\u53d1\u9001\u670d\u52a1\u5668\u3002<\/p>\n<p>4\u3001<strong>\u6bcf\u4e2aCookie\u7684\u5927\u5c0f\u4e0d\u80fd\u8d85\u8fc74kb<\/strong><\/p>\n<h3>1.2\u3001\u5982\u4f55\u521b\u5efaCookie<\/h3>\n<h4>1\u3001\u56fe\u89e3<\/h4>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/zyg.luchers.cn\/wp-content\/uploads\/2024\/04\/image-20230428153321649.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"http:\/\/zyg.luchers.cn\/wp-content\/uploads\/2024\/04\/image-20230428153321649.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20230428153321649\" \/><\/div><\/p>\n<h4>2\u3001\u4f8b\u5b50<\/h4>\n<pre><code class=\"language-java\">    protected void  creatCookie(HttpServletRequest req ,HttpServletResponse resp) throws IOException {\n\/\/        \u521b\u5efacookie\u5bf9\u8c61\n        Cookie cookie = new Cookie(&quot;key1&quot;,&quot;value1&quot;);\n\/\/        \u901a\u77e5\u5ba2\u6237\u7aef\u4fdd\u5b58\u6570\u636e\n        resp.addCookie(cookie);\n        Cookie cookie2 = new Cookie(&quot;key2&quot;,&quot;value2&quot;);\n\/\/        \u901a\u77e5\u5ba2\u6237\u7aef\u4fdd\u5b58\u6570\u636e\n        resp.addCookie(cookie2);\n\n        resp.getWriter().write(&quot;cookie\u521b\u5efa\u6210\u529f&quot;);\n\n    }<\/code><\/pre>\n<h3>1.3\u3001\u670d\u52a1\u5668\u5982\u4f55\u83b7\u53d6Cookie\u503c<\/h3>\n<h4>1\u3001\u56fe\u89e3<\/h4>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/zyg.luchers.cn\/wp-content\/uploads\/2024\/04\/image-20230428153708303.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"http:\/\/zyg.luchers.cn\/wp-content\/uploads\/2024\/04\/image-20230428153708303.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20230428153708303\" \/><\/div><\/p>\n<h4>2\u3001\u4ee3\u7801<\/h4>\n<p>1\u3001Cookie\u5de5\u5177\u7c7b\u4ee3\u7801<\/p>\n<pre><code class=\"language-java\">public class CookieUtils {\n    public static Cookie findCookie(String  name, Cookie[] cookies){\n        if(name==null || cookies==null || cookies.length==0){\n            return null;\n        }\n        for (Cookie cookie:cookies){\n            if(name.equals(cookie.getName())){\n                return cookie;\n            }\n        }\n        return null;\n    }\n}<\/code><\/pre>\n<p>2\u3001\u4f8b\u5b50\u4ee3\u7801<\/p>\n<pre><code class=\"language-java\">    protected void  getCookie(HttpServletRequest req ,HttpServletResponse resp) throws IOException {\n        Cookie [] cookies = req.getCookies();\n        for (Cookie cookie :cookies){\n\/\/            \u67e5\u8be2\u6307\u5b9a\u7684cookie\n            Cookie iwantCookie = CookieUtils.findCookie(&quot;key1&quot;,cookies);\n\n\/\/            getName\u8fd4\u56decookie\u4e2d\u7684key\n\/\/            getValue\u8fd4\u56decookie\u4e2d\u7684value\n            resp.getWriter().write(&quot;cookie:&quot;+cookie.getName()+&quot;===&quot;+cookie.getValue()+&quot;&lt;br\/&gt;&quot;);\n\n            if(iwantCookie!=null){\n                resp.getWriter().write(&quot;\u627e\u5230\u4e86\u9700\u8981\u7684Cookie:&quot;+iwantCookie.getName());\n            }\n        }\n    }<\/code><\/pre>\n<h3>1.4\u3001Cookie\u503c\u7684\u4fee\u6539<\/h3>\n<h4>1\u3001\u65b9\u6cd5\u4e00<\/h4>\n<p>1\u3001\u521b\u5efa\u4e00\u4e2a\u8981\u4fee\u6539\u7684\u540c\u540d\uff08key\uff09\u7684cookie\u5bf9\u8c61<\/p>\n<p>2\u3001\u5728\u6784\u9020\u5668\u4e2d\uff0c\u540c\u65f6\u8d4b\u4e88\u65b0\u7684Cookie\u503c<\/p>\n<p>3\u3001\u8c03\u7528response.addCookie(Cookie);<\/p>\n<p>4\u3001\u4f8b\u5b50<\/p>\n<pre><code class=\"language-java\">    protected void  updateCookie(HttpServletRequest req ,HttpServletResponse resp) throws IOException {\n\/\/        \u65b9\u6cd5\u4e00\uff1a\n\/\/        \u521b\u5efa\u4e00\u4e2a\u8981\u4fee\u6539\u7684\u540c\u540dcookie\u5bf9\u8c61\n\/\/        \u5728\u6784\u9020\u5668\uff0c\u540c\u65f6\u8d4b\u4e88\u65b0\u7684cookie\u503c\n        Cookie cookie = new Cookie(&quot;key1&quot;,&quot;newValue1&quot;);\n\/\/        \u8c03\u7528response.addCookie\n        resp.addCookie(cookie);\n        }<\/code><\/pre>\n<h4>2\u3001\u65b9\u6cd5\u4e8c<\/h4>\n<p>1\u3001\u67e5\u627e\u5230\u9700\u8981\u4fee\u6539\u7684Cookie\u5bf9\u8c61<\/p>\n<p>2\u3001\u8c03\u7528setValue()\u65b9\u6cd5\u8d4b\u4e88\u65b0\u7684Cookie\u503c<\/p>\n<p>3\u3001\u8c03\u7528response.addCookie()\u901a\u77e5\u5ba2\u6237\u7aef\u4fdd\u5b58\u4fee\u6539<\/p>\n<p>4\u3001\u4f8b\u5b50<\/p>\n<pre><code class=\"language-java\">    protected void  updateCookie(HttpServletRequest req ,HttpServletResponse resp) throws IOException {\n\n\/\/        \u65b9\u6cd5\u4e8c\uff1a\n\/\/        \u67e5\u8be2\u9700\u8981\u4fee\u6539\u7684cookie\u5bf9\u8c61\n        Cookie cookie = CookieUtils.findCookie(&quot;key1&quot;,req.getCookies());\n        if(cookie!=null){\n\/\/            \u8c03\u7528setValue()\u65b9\u6cd5\u8d4b\u4e88\u65b0\u7684Cookie\u503c\n            cookie.setValue(&quot;newValue2&quot;);\n\/\/            \u8c03\u7528response.addCookie()\u901a\u77e5\u5ba2\u6237\u7aef\u4fdd\u5b58\n            resp.addCookie(cookie);\n\n        }\n    }<\/code><\/pre>\n<h3>1.5\u3001\u6d4f\u89c8\u5668\u5982\u4f55\u67e5\u770bCookie<\/h3>\n<h4>1\u3001\u8c37\u6b4c\u4f8b\u5b50<\/h4>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/zyg.luchers.cn\/wp-content\/uploads\/2024\/04\/image-20230428154239549.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"http:\/\/zyg.luchers.cn\/wp-content\/uploads\/2024\/04\/image-20230428154239549.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20230428154239549\" \/><\/div><\/p>\n<h4>2\u3001\u706b\u72d0\u4f8b\u5b50<\/h4>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/zyg.luchers.cn\/wp-content\/uploads\/2024\/04\/image-20230428154247829.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"http:\/\/zyg.luchers.cn\/wp-content\/uploads\/2024\/04\/image-20230428154247829.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20230428154247829\" \/><\/div><\/p>\n<h3>1.6\u3001Cookie\u751f\u547d\u63a7\u5236<\/h3>\n<h4>1\u3001\u4ec0\u4e48\u662f\u751f\u547d\u63a7\u5236<\/h4>\n<p>1\u3001Cookie\u7684\u751f\u547d\u63a7\u5236\uff1a\u5982\u4f55\u7ba1\u7406Cookie\u4ec0\u4e48\u65f6\u5019\u88ab\u9500\u6bc1\uff08\u5220\u9664\uff09<\/p>\n<h4>2\u3001\u65b9\u6cd5\uff1asetMaxAge()<\/h4>\n<p>1\u3001setMaxAge()\u65b9\u6cd5\uff1a\u8bbe\u7f6e\u751f\u547d\u5468\u671f<\/p>\n<p>\uff081\uff09\u5f53\u91cc\u9762\u53c2\u6570\u4e3a\u6b63\u6570\uff1a\u8868\u793a\u6307\u5b9a\u79d2\u540e\u8fc7\u671f<\/p>\n<p>\uff082\uff09\u5f53\u53c2\u6570\u4e3a\u8d1f\u6570\uff1a\u8868\u793a\u6d4f\u89c8\u5668\u5173\u95ed\uff0cCookie\u5c31\u4f1a\u88ab\u5220\u9664\uff08\u9ed8\u8ba4\u4e3a-1\uff09<\/p>\n<p>\uff083\uff09\u5f53\u53c2\u6570\u4e3a0\u65f6\uff1a\u8868\u793a\u9a6c\u4e0a\u5220\u9664Cookie<\/p>\n<h4>3\u3001\u4f8b\u5b50\uff1a\u8bbe\u7f6e\u5b58\u8d27\u8fd0\u884c\u65f6\u7684Cookie<\/h4>\n<pre><code class=\"language-java\">protected void  life3600(HttpServletRequest req ,HttpServletResponse resp) throws IOException {\n    Cookie cookie = new Cookie(&quot;life3600&quot;,&quot;life3600&quot;);\n    cookie.setMaxAge(60*60);\/\/\u8bbe\u7f6eCookie\u4e00\u4e2a\u5c0f\u65f6\u540e\u88ab\u5220\u9664\uff0c\u65e0\u6548\n    resp.addCookie(cookie);\n    resp.getWriter().write(&quot;\u5df2\u7ed3\u521b\u5efa\u4e86\u5b58\u6d3b\u4e00\u5c0f\u65f6\u7684cookie&quot;);\n}<\/code><\/pre>\n<h4>4\u3001\u4f8b\u5b50\uff1a\u7acb\u9a6c\u5220\u9664Cookie<\/h4>\n<pre><code class=\"language-java\">    protected void  deleteNow(HttpServletRequest req ,HttpServletResponse resp) throws IOException {\n\/\/        \u627e\u5230\u9700\u8981\u5220\u9664\u7684cookie\u5bf9\u8c61\n        Cookie cookie = CookieUtils.findCookie(&quot;key2&quot;,req.getCookies());\n\n        if(cookie!=null){\n\n\/\/        \u8c03\u7528setMaxAge(0)\n            cookie.setMaxAge(0);\n\/\/            \u8c03\u7528response.addCookie()\u901a\u77e5\u5ba2\u6237\u7aef\u4fdd\u5b58\n            resp.addCookie(cookie);\n\n            resp.getWriter().write(&quot;key2\u5df2\u7ed3\u88ab\u5220\u9664\u4e86&quot;);\n\n        }\n\n    }<\/code><\/pre>\n<h3>1.7\u3001Cookie\u6709\u6548\u8def\u5f84\u7684path\u8bbe\u7f6e<\/h3>\n<h4>1\u3001\u5185\u5bb9<\/h4>\n<p>1\u3001Cookie\u7684path\u5c5e\u6027\u53ef\u4ee5\u6709\u6548\u7684\u8fc7\u6ee4\u54ea\u4e9bCookie\u53ef\u4ee5\u53d1\u9001\u7ed9\u670d\u52a1\u5668\u3002<\/p>\n<p>2\u3001path\u5c5e\u6027\u662f\u901a\u8fc7\u8bf7\u6c42\u7684\u5730\u5740\u8fdb\u884c\u6709\u6548\u7684\u8fc7\u6ee4<\/p>\n<p>3\u3001\u4f8b\u5b50<\/p>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/zyg.luchers.cn\/wp-content\/uploads\/2024\/04\/image-20230428154823981.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"http:\/\/zyg.luchers.cn\/wp-content\/uploads\/2024\/04\/image-20230428154823981.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20230428154823981\" \/><\/div><\/p>\n<h4>2\u3001\u4f8b\u5b50<\/h4>\n<pre><code class=\"language-java\">    protected void  testPath(HttpServletRequest req ,HttpServletResponse resp) throws IOException {\n        Cookie cookie = new Cookie(&quot;path1&quot;,&quot;path1&quot;);\n\/\/        getContextPath()\u5f97\u5230\u5de5\u7a0b\u8def\u5f84\n        cookie.setPath(req.getContextPath()+&quot;\/abc&quot;);\n        resp.addCookie(cookie);\n        resp.getWriter().write(&quot;\u521b\u5efa\u4e86\u4e00\u4e2a\u5e26\u6709\u5de5\u7a0b\u8def\u5f84\u7684cookie&quot;);\n    }<\/code><\/pre>\n<h3>1.8\u3001Cookie\u7ec3\u4e60\uff1a\u514d\u8f93\u5165\u7528\u6237\u540d\u767b\u5f55<\/h3>\n<h4>1\u3001\u56fe\u89e3<\/h4>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/zyg.luchers.cn\/wp-content\/uploads\/2024\/04\/image-20230428154932955.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"http:\/\/zyg.luchers.cn\/wp-content\/uploads\/2024\/04\/image-20230428154932955.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20230428154932955\" \/><\/div><\/p>\n<h4>2\u3001\u4ee3\u7801\u5b9e\u73b0<\/h4>\n<p>1\u3001login.jsp\u9875\u9762<\/p>\n<pre><code class=\"language-jsp\">&lt;%@ page contentType=&quot;text\/html;charset=UTF-8&quot; language=&quot;java&quot; %&gt;\n&lt;html&gt;\n&lt;head&gt;\n    &lt;title&gt;Title&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;form action=&quot;http:\/\/localhost:8080\/13_cookie_session\/loginServlet&quot; method=&quot;get&quot;&gt;\n        \u7528\u6237\u540d\uff1a&lt;input type=&quot;text&quot; name=&quot;username&quot; value=&quot;${cookie.username.value}&quot;&gt;&lt;br\/&gt;\n        \u5bc6\u7801\uff1a&lt;input type=&quot;password&quot; name=&quot;password&quot;&gt;&lt;br\/&gt;\n        &lt;input type=&quot;submit&quot; name=&quot;sub&quot; value=&quot;\u70b9\u51fb\u767b\u5f55&quot;&gt;\n    &lt;\/form&gt;\n\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<p>2\u3001LoginServlet.java<\/p>\n<pre><code class=\"language-java\">public class LoginServlet extends HttpServlet {\n\n    @Override\n    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {\n        String username =req.getParameter(&quot;username&quot;);\n        String password = req.getParameter(&quot;password&quot;);\n        if(&quot;123456&quot;.equals(username) &amp;&amp;&quot;123456&quot;.equals(password)) {\n\/\/            \u6210\u529f\n            Cookie cookie = new Cookie(&quot;username&quot; ,username);\n            cookie.setMaxAge(60*60*24);\n            resp.addCookie(cookie);\n            System.out.println(&quot;\u767b\u5f55\u6210\u529f&quot;);\n        }else {\n\/\/            \u5931\u8d25\n            System.out.println(&quot;\u767b\u5f55\u5931\u8d25&quot;);\n        }\n\n    }\n}<\/code><\/pre>\n<h2>2\u3001Session\u4f1a\u8bdd<\/h2>\n<h3>2.1\u3001\u4ec0\u4e48\u662fSession\u4f1a\u8bdd<\/h3>\n<h4>1\u3001\u5185\u5bb9<\/h4>\n<p>1\u3001Session\u662f\u4e00\u4e2a\u63a5\u53e3\uff08HTTPSession\uff09<\/p>\n<p>2\u3001Session\u5c31\u662f\u4e00\u4e2a\u4f1a\u8bdd\uff0c\u5b83\u662f\u7528\u6765\u7ef4\u62a4\u4e00\u4e2a\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4e4b\u95f4\u5173\u8054\u7684\u6280\u672f<\/p>\n<p>3\u3001\u6bcf\u4e2a\u5ba2\u6237\u7aef\u90fd\u6709\u81ea\u5df1\u7684\u4e00\u4e2aSession\u4f1a\u8bdd\u3002<\/p>\n<p>4\u3001Session\u4f1a\u8bdd\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u7528\u6765\u4fdd\u5b58\u7528\u6237\u767b\u5f55\u4e4b\u540e\u7684\u4fe1\u606f\u3002<\/p>\n<h3>2.2\u3001\u521b\u5efaSession\u548c\u83b7\u53d6\uff08id\u53f7\uff0c\u662f\u5426\u66f4\u65b0\uff09<\/h3>\n<p>1\u3001\u521b\u5efa\u548c\u83b7\u53d6\u7684API\u90fd\u662f\u4e00\u6837\u7684<\/p>\n<h4>1\u3001\u521b\u5efaSession\u548c\u83b7\u53d6<\/h4>\n<p>1\u3001request.getSession()<\/p>\n<p>\uff081\uff09\u7b2c\u4e00\u6b21\u8c03\u7528\uff1a\u521b\u5efaSession\u4f1a\u8bdd<\/p>\n<p>\uff082\uff09\u4e4b\u540e\u8c03\u7528\u90fd\u662f\uff1a\u83b7\u53d6\u524d\u9762\u521b\u5efa\u597d\u7684Session\u4f1a\u8bdd\u5bf9\u8c61<\/p>\n<h4>2\u3001\u83b7\u53d6id\u53f7<\/h4>\n<p>1\u3001\u901a\u8fc7session.getId()\u65b9\u6cd5\uff0c\u53ef\u4ee5\u83b7\u53d6\u552f\u4e00\u7684id<\/p>\n<p>2\u3001\u6bcf\u4e2a\u4f1a\u8bdd\u90fd\u6709\u4e00\u4e2aid\u503c\uff0c\u8fd9\u4e2aid\u662f\u552f\u4e00\u7684<\/p>\n<h4>3\u3001\u5224\u65adsession\u662f\u83b7\u53d6\u8fd8\u662f\u5224\u65ad<\/h4>\n<p>1\u3001isNew()\uff1a\u65b9\u6cd5\u5224\u65adsession\u662f\u4e0d\u662f\u65b0\u521b\u5efa\u7684<\/p>\n<p>\uff081\uff09true\uff1a\u662f<\/p>\n<p>\uff082\uff09false\uff1a\u4e0d\u662f<\/p>\n<p>\uff083\uff09\u8fd4\u56de\u503c\u662f\uff1aboolean<\/p>\n<h4>4\u3001\u4f8b\u5b50<\/h4>\n<pre><code class=\"language-java\">    protected void creatOrGetSession(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {\n\/\/        \u521b\u5efasession\u5bf9\u8c61\n        HttpSession session = req.getSession();\n\/\/        \u5224\u65ad\u662f\u4e0d\u662f\u7b2c\u4e00\u6b21\u521b\u5efa\n        boolean isNew = session.isNew();\n\/\/        \u83b7\u53d6session\u4f1a\u8bdd\u7684\u552f\u4e00id\n        String  id = session.getId();\n\n        resp.getWriter().write(&quot;\u5f97\u5230session\uff0c\u5979\u7684id\u4e3a\uff1a&quot;+id);\n        resp.getWriter().write(&quot;\u5f97\u5230\u7684session\u662f\u4e0d\u662f\u7b2c\u4e00\u6b21\u521b\u5efa\uff1a&quot;+isNew);\n    }<\/code><\/pre>\n<h3>2.3\u3001Session\u57df\u6570\u636e\u7684\u5b58\u53d6<\/h3>\n<h4>1\u3001\u53d6\u51faSession\u57df\u4e2d\u7684\u6570\u636e<\/h4>\n<p>1\u3001\u901a\u8fc7req.getSession().getAttribute()\uff1a\u65b9\u6cd5\u83b7\u53d6<\/p>\n<h4>2\u3001\u8bbe\u7f6eSession\u57df\u4e2d\u7684\u6570\u636e<\/h4>\n<p>1\u3001\u901a\u8fc7req.getSession().setAttribute()\uff1a\u65b9\u6cd5\u83b7\u53d6<\/p>\n<h4>3\u3001\u4f8b\u5b50<\/h4>\n<pre><code class=\"language-java\">\/**\n * \u5f80session\u8bbe\u7f6e\u6570\u636e\n * @param req\n * @param resp\n * @throws ServletException\n * @throws IOException\n *\/\nprotected void setAttribute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {\n    req.getSession().setAttribute(&quot;key1&quot;,&quot;value1&quot;);\n    resp.getWriter().write(&quot;\u5df2\u7ecf\u5728Session\u4fdd\u5b58\u4e86\u6570\u636e&quot;);\n}\n\n\/**\n * \u5f80session\u5f97\u5230\u6570\u636e\n * @param req\n * @param resp\n * @throws ServletException\n * @throws IOException\n *\/\nprotected void getAttribute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {\n    Object attribute = req.getSession().getAttribute(&quot;key1&quot;);\n    resp.getWriter().write(&quot;\u83b7\u53d6\u7684\u6570\u636e\u4e3a:&quot;+attribute);\n}<\/code><\/pre>\n<h3>2.4\u3001Session\u751f\u547d\u5468\u671f\u63a7\u5236<\/h3>\n<h4>1\u3001\u5185\u5bb9<\/h4>\n<p>1\u3001public void setMaxInactiveInterval(int interval)\uff1a\u8bbe\u7f6e Session \u7684\u8d85\u65f6\u65f6\u95f4\uff08\u4ee5\u79d2\u4e3a\u5355\u4f4d\uff09<\/p>\n<p>2\u3001\u8d85\u8fc7\u6307\u5b9a\u7684\u65f6\u957f\uff0cSession \u5c31\u4f1a\u88ab\u9500\u6bc1\u3002<\/p>\n<p>3\u3001\u8bbe\u7f6e\u7684\u503cinterval\u4e3a\u6b63\u6570\uff1a\u8bbe\u5b9a\u7684\u65f6\u957f\u5230\u8fbe\u540e\u8d85\u65f6<\/p>\n<p>4\u3001\u8bbe\u7f6e\u7684\u503cinterval\u4e3a\u8d1f\u6570\uff1a\u6c38\u4e0d\u8d85\u65f6<\/p>\n<p>5\u3001Session\u7684\u9ed8\u8ba4\u8d85\u65f6\u65f6\u957f\u4e3a30\u5206\u949f<\/p>\n<h4>2\u3001\u8d85\u65f6\u7684\u6982\u5ff5<\/h4>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/zyg.luchers.cn\/wp-content\/uploads\/2024\/04\/image-20230428160606922.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"http:\/\/zyg.luchers.cn\/wp-content\/uploads\/2024\/04\/image-20230428160606922.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20230428160606922\" \/><\/div><\/p>\n<h4>3\u3001\u4fee\u6539tomcat\u4e2dSession\u7684\u9ed8\u8ba4\u8d85\u65f6<\/h4>\n<p>1\u3001\u5728tomcat\u670d\u52a1\u5668\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684web.xml\u9ed8\u8ba4\u6709\u4ee5\u4e0b\u914d\u7f6e\u8fdb\u884c\u4fee\u6539<\/p>\n<p>2\u3001<\/p>\n<p>\u200b       \\<session-config> <\/p>\n<p>\u200b       \\<session-timeout>30\\<\/session-timeout> <\/p>\n<p>\u200b       \\<\/session-config><\/p>\n<h4>4\u3001\u4fee\u6539\u4e2a\u4eba\u7684web\u5de5\u7a0b\u4e2dSession\u7684\u9ed8\u8ba4\u8d85\u65f6<\/h4>\n<p>1\u3001\u5728\u4e2a\u4eba\u7684web\u5de5\u7a0b\u4e0b\u7684web.xml\u914d\u7f6e\u4e2d\u4fee\u6539<\/p>\n<p>\u200b       \\<session-config> <\/p>\n<p>\u200b       \\<session-timeout>30\\<\/session-timeout> <\/p>\n<p>\u200b       \\<\/session-config><\/p>\n<h4>5\u3001\u4fee\u6539\u4e2a\u522b\u7684Session\u8d85\u65f6\u65f6\u957f<\/h4>\n<p>1\u3001\u4f7f\u7528API\u4e2d\u7684public void setMaxInactiveInterval(int interval)\u65b9\u6cd5\u6765\u8fdb\u884c\u8bbe\u7f6e<\/p>\n<p>2\u3001\u4f8b\u5b50<\/p>\n<pre><code class=\"language-java\">protected void life3(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {\n    HttpSession session =req.getSession();\n    session.setMaxInactiveInterval(3);\n    resp.getWriter().write(&quot;\u9ed8\u8ba4\u65f6\u957f\u4e3a\uff1a3\u79d2\u540e\u8d85\u65f6&quot;);\n\n}<\/code><\/pre>\n<h3>2.5\u3001\u6d4f\u89c8\u5668\u548cSession\u4e4b\u95f4\u5173\u8054\u7684\u6280\u672f\u5185\u5e55<\/h3>\n<h4>1\u3001\u5185\u5bb9<\/h4>\n<p>1\u3001Session\u6280\u672f\uff0c\u5e95\u5c42\u5176\u5b9e\u662f\u57fa\u4e8eCookie\u6280\u672f\u6765\u5b9e\u73b0\u7684<\/p>\n<h4>2\u3001\u56fe\u89e3<\/h4>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/zyg.luchers.cn\/wp-content\/uploads\/2024\/04\/image-20230428160910511.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"http:\/\/zyg.luchers.cn\/wp-content\/uploads\/2024\/04\/image-20230428160910511.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20230428160910511\" \/><\/div><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5341\u56db\u3001Cookie\u548cSession 1\u3001Cookie\u997c\u5e72 1.1\u3001\u4ec0\u4e48\u662fCookie\uff1f 1\u3001\u5185\u5bb9 1\u3001Cook [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[],"class_list":["post-5951","post","type-post","status-publish","format-standard","hentry","category-javaweb"],"_links":{"self":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts\/5951","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/comments?post=5951"}],"version-history":[{"count":1,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts\/5951\/revisions"}],"predecessor-version":[{"id":5952,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts\/5951\/revisions\/5952"}],"wp:attachment":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/media?parent=5951"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/categories?post=5951"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/tags?post=5951"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}