{"id":49627,"date":"2025-07-30T18:47:40","date_gmt":"2025-07-30T10:47:40","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/49627.html"},"modified":"2025-07-30T18:47:40","modified_gmt":"2025-07-30T10:47:40","slug":"%e8%af%a6%e8%a7%a3java%e4%b9%8bspring-mvc%e7%af%87%e4%ba%8c","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/49627.html","title":{"rendered":"\u8be6\u89e3Java\u4e4bSpring MVC\u7bc7\u4e8c"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730104735-6889f84754080.gif\" \/><\/p>\n<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"-toc\" style=\"margin-left:120px\">\n<p id=\"%E8%8E%B7%E5%8F%96Cookie%2FSession-toc\" style=\"margin-left:120px\">\u83b7\u53d6Cookie\/Session<\/p>\n<p id=\"%E7%90%86%E8%A7%A3Cookie-toc\" style=\"margin-left:160px\">\u7406\u89e3Cookie<\/p>\n<p id=\"%E7%90%86%E8%A7%A3Session-toc\" style=\"margin-left:160px\">\u7406\u89e3Session<\/p>\n<p id=\"Cookie%E5%92%8CSession%E7%9A%84%E5%8C%BA%E5%88%AB-toc\" style=\"margin-left:160px\">Cookie\u548cSession\u7684\u533a\u522b<\/p>\n<p id=\"%E8%8E%B7%E5%8F%96Cookie-toc\" style=\"margin-left:160px\">\u83b7\u53d6Cookie<\/p>\n<p id=\"%E8%8E%B7%E5%8F%96Session-toc\" style=\"margin-left:160px\">\u83b7\u53d6Session<\/p>\n<p id=\"%E8%8E%B7%E5%8F%96Header-toc\" style=\"margin-left:120px\">\u83b7\u53d6Header<\/p>\n<p id=\"%E8%8E%B7%E5%8F%96User-Agent-toc\" style=\"margin-left:160px\">\u83b7\u53d6User-Agent<\/p>\n<hr id=\"hr-toc\" \/>\n<h5 id=\"%E8%8E%B7%E5%8F%96Cookie%2FSession\">\u83b7\u53d6Cookie\/Session<\/h5>\n<h6 id=\"%E7%90%86%E8%A7%A3Cookie\">\u7406\u89e3Cookie<\/h6>\n<p>HTTP\u534f\u8bae\u81ea\u8eab\u662f\u201c\u65e0\u72b6\u6001\u201d\u534f\u8bae&#xff0c;\u4f46\u662f\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d&#xff0c;\u6211\u4eec\u5f88\u591a\u65f6\u5019\u662f\u9700\u8981\u77e5\u9053\u8bf7\u6c42\u4e4b\u95f4\u7684\u5173\u8054\u5173\u7cfb\u7684\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"254\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730104735-6889f847df8d8.png\" width=\"504\" \/><\/p>\n<p>\u4e0a\u8ff0\u56fe\u4e2d\u7684 &#034;\u4ee4\u724c&#034; \u901a\u5e38\u5c31\u5b58\u50a8\u5728 Cookie \u5b57\u6bb5\u4e2d. \u6b64\u65f6\u5728\u670d\u52a1\u5668\u8fd9\u8fb9\u5c31\u9700\u8981\u8bb0\u5f55&#034;\u4ee4\u724c&#034;\u4fe1\u606f, \u4ee5\u53ca\u4ee4\u724c\u5bf9\u5e94\u7684\u2f64\u2f3e\u4fe1\u606f, \u8fd9\u4e2a\u5c31\u662f Session \u673a\u5236\u6240\u505a\u7684\u2f2f\u4f5c.<\/p>\n<h6 id=\"%E7%90%86%E8%A7%A3Session\">\u7406\u89e3Session<\/h6>\n<p>\u6211\u4eec\u5148\u6765\u4e86\u89e3\u2f00\u4e0b\u4ec0\u4e48\u662f\u4f1a\u8bdd. \u4f1a\u8bdd: \u5bf9\u8bdd\u7684\u610f\u601d<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"267\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730104736-6889f8483dcc9.png\" width=\"552\" \/><\/p>\n<p>\u5728\u8ba1\u7b97\u673a\u9886\u57df, \u4f1a\u8bdd\u662f\u2f00\u4e2a\u5ba2\u2f3e\u4e0e\u670d\u52a1\u5668\u4e4b\u95f4\u7684\u4e0d\u4e2d\u65ad\u7684\u8bf7\u6c42\u54cd\u5e94. \u5bf9\u5ba2\u2f3e\u7684\u6bcf\u4e2a\u8bf7\u6c42&#xff0c;\u670d\u52a1\u5668\u80fd\u591f\u8bc6\u522b\u51fa\u8bf7\u6c42\u6765\u2f83\u4e8e\u540c\u2f00\u4e2a\u5ba2\u2f3e. \u5f53\u2f00\u4e2a\u672a\u77e5\u7684\u5ba2\u2f3e\u5411Web\u5e94\u2f64\u7a0b\u5e8f\u53d1\u9001\u7b2c\u2f00\u4e2a\u8bf7\u6c42\u65f6\u5c31\u5f00\u59cb\u4e86\u2f00\u4e2a\u4f1a\u8bdd. \u5f53\u5ba2\u2f3e\u660e\u786e\u7ed3\u675f\u4f1a\u8bdd\u6216\u670d\u52a1\u5668\u5728\u2f00\u4e2a\u65f6\u9650\u5185\u6ca1\u6709\u63a5\u53d7\u5230\u5ba2\u2f3e\u7684\u4efb\u4f55\u8bf7\u6c42\u65f6&#xff0c;\u4f1a\u8bdd\u5c31\u7ed3\u675f\u4e86. \u670d\u52a1\u5668\u540c\u2f00\u65f6\u523b\u6536\u5230\u7684\u8bf7\u6c42\u662f\u5f88\u591a\u7684. \u670d\u52a1\u5668\u9700\u8981\u6e05\u695a\u7684\u533a\u5206\u6bcf\u4e2a\u8bf7\u6c42\u662f\u4ece\u5c5e\u4e8e\u54ea\u4e2a\u2f64\u2f3e, \u4e5f\u5c31\u662f\u5c5e\u4e8e\u54ea\u4e2a\u4f1a\u8bdd, \u5c31\u9700\u8981\u5728\u670d\u52a1\u5668\u8fd9\u8fb9\u8bb0\u5f55\u6bcf\u4e2a\u4f1a\u8bdd\u4ee5\u53ca\u4e0e\u2f64\u2f3e\u7684\u4fe1\u606f\u7684\u5bf9\u5e94\u5173\u7cfb.<\/p>\n<p>Session\u662f\u670d\u52a1\u5668\u4e3a\u4e86\u4fdd\u5b58\u2f64\u2f3e\u4fe1\u606f\u2f7d\u521b\u5efa\u7684\u2f00\u4e2a\u7279\u6b8a\u7684\u5bf9\u8c61.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"258\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730104736-6889f848723c1.png\" width=\"538\" \/><\/p>\n<p><span style=\"background-color:#a2e043\">Session\u7684\u672c\u8d28\u5c31\u662f\u2f00\u4e2a &#034;\u54c8\u5e0c\u8868&#034;, \u5b58\u50a8\u4e86\u2f00\u4e9b\u952e\u503c\u5bf9\u7ed3\u6784. Key \u5c31\u662fSessionID, Value \u5c31\u662f\u2f64\u2f3e\u4fe1\u606f(\u2f64\u2f3e\u4fe1\u606f\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u7075\u6d3b\u8bbe\u8ba1).<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"234\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730104736-6889f8488b61f.png\" width=\"513\" \/><\/p>\n<p>SessionId\u662f\u7531\u670d\u52a1\u5668\u2f63\u6210\u7684\u2f00\u4e2a &#034;\u552f\u2f00\u6027\u5b57\u7b26\u4e32&#034;, \u4ece Session \u673a\u5236\u7684\u2ec6\u5ea6\u6765\u770b, \u8fd9\u4e2a\u552f\u2f00\u6027\u5b57\u7b26\u4e32\u79f0\u4e3a &#034;SessionId&#034;. \u4f46\u662f\u7ad9\u5728\u6574\u4e2a\u767b\u5f55\u6d41\u7a0b\u4e2d\u770b\u5f85, \u4e5f\u53ef\u4ee5\u628a\u8fd9\u4e2a\u552f\u2f00\u6027\u5b57\u7b26\u4e32\u79f0\u4e3a &#034;token&#034;. \u4e0a\u8ff0\u4f8b\u2f26\u4e2d\u7684\u4ee4\u724cID, \u5c31\u53ef\u4ee5\u770b\u505a\u662fSessionId, \u53ea\u4e0d\u8fc7\u4ee4\u724c\u9664\u4e86ID\u4e4b\u5916, \u8fd8\u4f1a\u5e26\u2f00\u4e9b\u5176\u4ed6\u4fe1\u606f, \u2f50\u5982\u65f6\u95f4, \u7b7e\u540d\u7b49.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"201\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730104736-6889f848a4ba4.png\" width=\"571\" \/><\/p>\n<p>1. \u5f53\u2f64\u2f3e\u767b\u9646\u7684\u65f6\u5019, \u670d\u52a1\u5668\u5728 Session \u4e2d\u65b0\u589e\u2f00\u4e2a\u65b0\u8bb0\u5f55, \u5e76\u628a sessionId\u8fd4\u56de\u7ed9\u5ba2\u2f3e\u7aef. (\u901a\u8fc7HTTP \u54cd\u5e94\u4e2d\u7684 Set-Cookie \u5b57\u6bb5\u8fd4\u56de). 2. \u5ba2\u2f3e\u7aef\u540e\u7eed\u518d\u7ed9\u670d\u52a1\u5668\u53d1\u9001\u8bf7\u6c42\u7684\u65f6\u5019, \u9700\u8981\u5728\u8bf7\u6c42\u4e2d\u5e26\u4e0a sessionId. (\u901a\u8fc7 HTTP \u8bf7\u6c42\u4e2d\u7684Cookie \u5b57\u6bb5\u5e26\u4e0a). 3. \u670d\u52a1\u5668\u6536\u5230\u8bf7\u6c42\u4e4b\u540e, \u6839\u636e\u8bf7\u6c42\u4e2d\u7684 sessionId\u5728 Session \u4fe1\u606f\u4e2d\u83b7\u53d6\u5230\u5bf9\u5e94\u7684\u2f64\u2f3e\u4fe1\u606f, \u518d\u8fdb\u2f8f\u540e\u7eed\u64cd\u4f5c.\u627e\u4e0d\u5230\u5219\u91cd\u65b0\u521b\u5efaSession, \u5e76\u628aSessionID\u8fd4\u56de.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"188\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730104736-6889f848c6118.png\" width=\"574\" \/><\/p>\n<p><span style=\"color:#fe2c24\">Session \u9ed8\u8ba4\u662f\u4fdd\u5b58\u5728\u5185\u5b58\u4e2d\u7684. \u5982\u679c\u91cd\u542f\u670d\u52a1\u5668\u5219 Session \u6570\u636e\u5c31\u4f1a\u4e22\u5931.<\/span><\/p>\n<h6 id=\"Cookie%E5%92%8CSession%E7%9A%84%E5%8C%BA%E5%88%AB\">Cookie\u548cSession\u7684\u533a\u522b<\/h6>\n<p>\u2022 Cookie \u662f\u5ba2\u2f3e\u7aef\u4fdd\u5b58\u2f64\u2f3e\u4fe1\u606f\u7684\u2f00\u79cd\u673a\u5236. Session \u662f\u670d\u52a1\u5668\u7aef\u4fdd\u5b58\u2f64\u2f3e\u4fe1\u606f\u7684\u2f00\u79cd\u673a\u5236. \u2022 Cookie \u548c Session\u4e4b\u95f4\u4e3b\u8981\u662f\u901a\u8fc7 SessionId \u5173\u8054\u8d77\u6765\u7684, SessionId \u662f Cookie \u548c Session \u4e4b\u95f4\u7684\u6865\u6881 \u2022 Cookie \u548c Session \u7ecf\u5e38\u4f1a\u5728\u2f00\u8d77\u914d\u5408\u4f7f\u2f64. \u4f46\u662f\u4e0d\u662f\u5fc5\u987b\u914d\u5408. \u2022 \u5b8c\u5168\u53ef\u4ee5\u2f64 Cookie \u6765\u4fdd\u5b58\u2f00\u4e9b\u6570\u636e\u5728\u5ba2\u2f3e\u7aef. \u8fd9\u4e9b\u6570\u636e\u4e0d\u2f00\u5b9a\u662f\u2f64\u2f3e\u2f9d\u4efd\u4fe1\u606f, \u4e5f\u4e0d\u2f00\u5b9a\u662f SessionId \u2022 Session \u4e2d\u7684sessionId \u4e5f\u4e0d\u9700\u8981\u2fae\u5f97\u901a\u8fc7 Cookie\/Set-Cookie \u4f20\u9012, \u2f50\u5982\u901a\u8fc7URL\u4f20\u9012.<\/p>\n<p>1. \u5b58\u50a8\u4f4d\u7f6e Cookie&#xff1a;\u5b58\u50a8\u5728\u5ba2\u6237\u7aef&#xff08;\u6d4f\u89c8\u5668&#xff09;\u4e0a\u3002\u5f53\u670d\u52a1\u5668\u54cd\u5e94\u4e00\u4e2aHTTP\u8bf7\u6c42\u65f6&#xff0c;\u5b83\u53ef\u4ee5\u5728\u54cd\u5e94\u5934\u4e2d\u5305\u542b\u4e00\u4e2aSet-Cookie\u5b57\u6bb5&#xff0c;\u6d4f\u89c8\u5668\u4f1a\u4fdd\u5b58\u8fd9\u4e2aCookie&#xff0c;\u5e76\u5728\u540e\u7eed\u7684\u8bf7\u6c42\u4e2d\u901a\u8fc7Cookie\u8bf7\u6c42\u5934\u5c06Cookie\u53d1\u9001\u56de\u670d\u52a1\u5668\u3002 Session&#xff1a;\u5b58\u50a8\u5728\u670d\u52a1\u5668\u7aef\u3002\u670d\u52a1\u5668\u4e3a\u6bcf\u4e2a\u7528\u6237\u4f1a\u8bdd\u521b\u5efa\u4e00\u4e2a\u552f\u4e00\u7684\u6807\u8bc6\u7b26&#xff08;\u901a\u5e38\u662fSession ID&#xff09;&#xff0c;\u8fd9\u4e2a\u6807\u8bc6\u7b26\u88ab\u53d1\u9001\u5230\u5ba2\u6237\u7aef&#xff08;\u901a\u5e38\u662f\u901a\u8fc7Cookie&#xff0c;\u4f46\u4e5f\u53ef\u4ee5\u901a\u8fc7URL\u91cd\u5199\u7b49\u65b9\u5f0f&#xff09;&#xff0c;\u5ba2\u6237\u7aef\u5728\u540e\u7eed\u7684\u8bf7\u6c42\u4e2d\u643a\u5e26\u8fd9\u4e2a\u6807\u8bc6\u7b26&#xff0c;\u670d\u52a1\u5668\u901a\u8fc7\u8fd9\u4e2a\u6807\u8bc6\u7b26\u6765\u8bc6\u522b\u7528\u6237\u4f1a\u8bdd\u3002 2. \u5b89\u5168\u6027 Cookie&#xff1a;\u7531\u4e8e\u5b58\u50a8\u5728\u5ba2\u6237\u7aef&#xff0c;\u56e0\u6b64\u76f8\u5bf9\u5bb9\u6613\u53d7\u5230\u653b\u51fb&#xff0c;\u5982\u8de8\u7ad9\u811a\u672c\u653b\u51fb&#xff08;XSS&#xff09;\u53ef\u4ee5\u8bfb\u53d6\u6216\u4fee\u6539Cookie\u3002\u4f46\u662f&#xff0c;\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6eHttpOnly\u548cSecure\u6807\u5fd7\u6765\u589e\u52a0\u5b89\u5168\u6027&#xff0c;HttpOnly\u6807\u5fd7\u53ef\u4ee5\u9632\u6b62JavaScript\u8bbf\u95eeCookie&#xff0c;Secure\u6807\u5fd7\u5219\u8981\u6c42Cookie\u4ec5\u901a\u8fc7HTTPS\u53d1\u9001\u3002 Session&#xff1a;\u5b58\u50a8\u5728\u670d\u52a1\u5668\u7aef&#xff0c;\u56e0\u6b64\u76f8\u5bf9\u66f4\u5b89\u5168\u3002\u4f46\u662f&#xff0c;\u5982\u679cSession ID\u88ab\u6cc4\u9732&#xff08;\u4f8b\u5982&#xff0c;\u901a\u8fc7URL\u91cd\u5199\u5e76\u6cc4\u9732\u5728\u65e5\u5fd7\u4e2d&#xff09;&#xff0c;\u5219\u53ef\u80fd\u9762\u4e34\u4f1a\u8bdd\u52ab\u6301\u7684\u98ce\u9669\u3002 3. \u5bb9\u91cf\u9650\u5236 Cookie&#xff1a;\u7531\u4e8e\u5b58\u50a8\u5728\u5ba2\u6237\u7aef&#xff0c;\u5176\u5927\u5c0f\u53d7\u5230\u6d4f\u89c8\u5668\u548c\u670d\u52a1\u5668\u8bbe\u7f6e\u7684\u9650\u5236\u3002\u5927\u591a\u6570\u6d4f\u89c8\u5668\u5bf9\u6bcf\u4e2aCookie\u7684\u5927\u5c0f\u548c\u6bcf\u4e2a\u57df\u540d\u4e0b\u7684Cookie\u603b\u6570\u90fd\u6709\u9650\u5236\u3002 Session&#xff1a;\u5b58\u50a8\u5728\u670d\u52a1\u5668\u7aef&#xff0c;\u56e0\u6b64\u5176\u5927\u5c0f\u9650\u5236\u4e3b\u8981\u53d6\u51b3\u4e8e\u670d\u52a1\u5668\u7684\u5185\u5b58\u548c\u914d\u7f6e&#xff0c;\u901a\u5e38\u8fdc\u5927\u4e8eCookie\u7684\u9650\u5236\u3002 4. \u751f\u547d\u5468\u671f Cookie&#xff1a;\u53ef\u4ee5\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4&#xff08;Expires\/Max-Age&#xff09;&#xff0c;\u4e5f\u53ef\u4ee5\u4e0d\u8bbe\u7f6e&#xff08;\u4f1a\u8bddCookie&#xff0c;\u6d4f\u89c8\u5668\u5173\u95ed\u65f6\u5931\u6548&#xff09;\u3002 Session&#xff1a;\u901a\u5e38\u4f9d\u8d56\u4e8e\u670d\u52a1\u5668\u7aef\u7684\u914d\u7f6e\u548cSession\u7684\u5b58\u50a8\u65b9\u5f0f&#xff08;\u5982\u5185\u5b58\u3001\u6570\u636e\u5e93\u7b49&#xff09;\u3002\u5982\u679c\u670d\u52a1\u5668\u914d\u7f6e\u4e86Session\u7684\u8d85\u65f6\u65f6\u95f4&#xff0c;\u5219Session\u5728\u8fbe\u5230\u8d85\u65f6\u65f6\u95f4\u540e\u4f1a\u88ab\u9500\u6bc1\u3002 5. \u4f7f\u7528\u573a\u666f Cookie&#xff1a;\u9002\u7528\u4e8e\u5b58\u50a8\u5c11\u91cf\u6570\u636e&#xff0c;\u5982\u7528\u6237\u504f\u597d\u8bbe\u7f6e\u3001\u767b\u5f55\u72b6\u6001\u7b49\u3002\u7531\u4e8e\u5b58\u50a8\u5728\u5ba2\u6237\u7aef&#xff0c;\u53ef\u4ee5\u8de8\u591a\u4e2a\u9875\u9762\u548c\u8bf7\u6c42\u6301\u4e45\u5316\u6570\u636e\u3002 Session&#xff1a;\u9002\u7528\u4e8e\u5b58\u50a8\u5927\u91cf\u6570\u636e&#xff0c;\u5982\u7528\u6237\u4fe1\u606f\u3001\u8d2d\u7269\u8f66\u5185\u5bb9\u7b49\u3002\u7531\u4e8e\u5b58\u50a8\u5728\u670d\u52a1\u5668\u7aef&#xff0c;\u53ef\u4ee5\u66f4\u5b89\u5168\u5730\u7ba1\u7406\u7528\u6237\u4f1a\u8bdd\u3002<\/p>\n<h6 id=\"%E8%8E%B7%E5%8F%96Cookie\">\u83b7\u53d6Cookie<\/h6>\n<p>\u9996\u5148\u5148\u8bbe\u7f6eCookie<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"523\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730104736-6889f848e400e.png\" width=\"1030\" \/><\/p>\n<p>\u518d\u83b7\u53d6&#xff1a;<\/p>\n<p>    &#064;RequestMapping(&#034;\/getC&#034;)<br \/>\n    public String getCookie(HttpServletRequest request){<br \/>\n        \/\/\u83b7\u53d6\u53c2\u6570<br \/>\n\/\/        String name &#061; request.getParameter(&#034;name&#034;);<br \/>\n        Cookie[] cookies &#061; request.getCookies();<br \/>\n        if (cookies!&#061;null){<br \/>\n            Arrays.stream(cookies).forEach(ck -&gt; System.out.println(ck.getName()&#043;&#034;:&#034;&#043;ck.getValue()));<br \/>\n        }<br \/>\n        return &#034;\u83b7\u53d6Cookie&#034;;<br \/>\n    } <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"34\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730104737-6889f8498fe22.png\" width=\"181\" \/><\/p>\n<p>\u66f4\u4e3a\u7b80\u6d01\u7684\u4ee3\u7801&#xff1a;<\/p>\n<p>&#064;RequestMapping(&#034;\/getC2&#034;)<br \/>\npublic String getCookie2(&#064;CookieValue(&#034;name&#034;) String name){<br \/>\n    return &#034;\u4eceCookie\u4e2d\u83b7\u53d6\u503c, name:&#034;&#043;name;<br \/>\n} <\/p>\n<h6 id=\"%E8%8E%B7%E5%8F%96Session\">\u83b7\u53d6Session<\/h6>\n<p>Session\u662f\u670d\u52a1\u5668\u7aef\u7684\u673a\u5236, \u6211\u4eec\u9700\u8981\u5148\u5b58\u50a8, \u624d\u80fd\u518d\u83b7\u53d6. Session \u4e5f\u662f\u57fa\u4e8eHttpServletRequest \u6765\u5b58\u50a8\u548c\u83b7\u53d6\u7684.<\/p>\n<p>\u8bbe\u7f6eSession<\/p>\n<p>    &#064;RequestMapping(&#034;\/setSess&#034;)<br \/>\n    public String setSess(HttpServletRequest request){<br \/>\n        \/\/\u4ececookie\u4e2d\u83b7\u53d6\u5230\u4e86sessionID, \u6839\u636esessionID\u83b7\u53d6Session\u5bf9\u8c61, \u5982\u679c\u6ca1\u6709\u83b7\u53d6\u5230, \u4f1a\u521b\u5efa\u4e00\u4e2asession\u5bf9\u8c61<br \/>\n        HttpSession session &#061; request.getSession();<br \/>\n        session.setAttribute(&#034;name&#034;, &#034;zhangsan&#034;);<br \/>\n        return &#034;\u8bbe\u7f6esession\u6210\u529f&#034;;<br \/>\n    } <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"407\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730104737-6889f849ae129.png\" width=\"857\" \/><\/p>\n<p>\u518d\u83b7\u53d6&#xff1a;<\/p>\n<p>    &#064;RequestMapping(&#034;\/getSess&#034;)<br \/>\n    public String getSess(HttpServletRequest request){<br \/>\n        \/\/\u4ececookie\u4e2d\u83b7\u53d6\u5230\u4e86sessionID, \u6839\u636esession\u83b7\u53d6Session\u5bf9\u8c61<br \/>\n        HttpSession session &#061; request.getSession();<br \/>\n        String name &#061; (String)session.getAttribute(&#034;name&#034;);<br \/>\n        return &#034;\u4ecesession\u4e2d\u83b7\u53d6name:&#034;&#043;name;<br \/>\n    } <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"390\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730104738-6889f84a14488.png\" width=\"835\" \/>\u00a0\u83b7\u53d6Session\u66f4\u4e3a\u7b80\u6d01\u7684\u4ee3\u7801&#xff1a;<\/p>\n<p>    &#064;RequestMapping(&#034;\/getSess2&#034;)<br \/>\n    public String getSess2(HttpSession session){<br \/>\n        String name &#061; (String)session.getAttribute(&#034;name&#034;);<br \/>\n        return &#034;\u4ecesession\u4e2d\u83b7\u53d6name:&#034;&#043;name;<br \/>\n    } <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"394\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730104738-6889f84a504ba.png\" width=\"839\" \/><\/p>\n<p>    &#064;RequestMapping(&#034;\/getSess3&#034;)<br \/>\n    public String getSess3(&#064;SessionAttribute(&#034;name&#034;) String name){<br \/>\n        return &#034;\u4ecesession\u4e2d\u83b7\u53d6name:&#034;&#043;name;<br \/>\n    } <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"371\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730104738-6889f84a9d20c.png\" width=\"854\" \/><\/p>\n<h5 id=\"%E8%8E%B7%E5%8F%96Header\">\u83b7\u53d6Header<\/h5>\n<h6 id=\"%E8%8E%B7%E5%8F%96User-Agent\">\u83b7\u53d6User-Agent<\/h6>\n<p>    &#064;RequestMapping(&#034;\/getHeader&#034;)<br \/>\n    public String getHeader(HttpServletRequest request){<br \/>\n        String userAgent &#061; request.getHeader(&#034;User-Agent&#034;);<br \/>\n        return &#034;\u4eceheader\u4e2d\u83b7\u53d6\u4fe1\u606f, userAgent:&#034;&#043;userAgent;<\/p>\n<p>    } <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"169\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730104739-6889f84b00f1c.png\" width=\"1200\" \/><\/p>\n<p>\u66f4\u4e3a\u7b80\u6d01\u7684\u4ee3\u7801&#xff1a;<\/p>\n<p>    &#064;RequestMapping(&#034;\/getHeader2&#034;)<br \/>\n    public String getHeader2(&#064;RequestHeader(&#034;User-Agent&#034;) String userAgent){<br \/>\n        return &#034;\u4eceheader\u4e2d\u83b7\u53d6\u4fe1\u606f, userAgent:&#034;&#043;userAgent;<\/p>\n<p>    } <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"170\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730104739-6889f84b597e7.png\" width=\"1200\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb2.9w\u6b21\uff0c\u70b9\u8d5e198\u6b21\uff0c\u6536\u85cf136\u6b21\u3002\u8be6\u89e3\u83b7\u53d6Cookie\u548cSession\uff0c\u83b7\u53d6Header\u3002_springmvc \u4f1a\u8bdd\u7ba1\u7406<\/p>\n","protected":false},"author":2,"featured_media":49612,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[99,1423,101],"topic":[],"class_list":["post-49627","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-java","tag-mvc","tag-spring"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u8be6\u89e3Java\u4e4bSpring MVC\u7bc7\u4e8c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/49627.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u8be6\u89e3Java\u4e4bSpring MVC\u7bc7\u4e8c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb2.9w\u6b21\uff0c\u70b9\u8d5e198\u6b21\uff0c\u6536\u85cf136\u6b21\u3002\u8be6\u89e3\u83b7\u53d6Cookie\u548cSession\uff0c\u83b7\u53d6Header\u3002_springmvc \u4f1a\u8bdd\u7ba1\u7406\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/49627.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-07-30T10:47:40+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730104735-6889f84754080.gif\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/49627.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/49627.html\",\"name\":\"\u8be6\u89e3Java\u4e4bSpring MVC\u7bc7\u4e8c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-07-30T10:47:40+00:00\",\"dateModified\":\"2025-07-30T10:47:40+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/49627.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/49627.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/49627.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u8be6\u89e3Java\u4e4bSpring MVC\u7bc7\u4e8c\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u8be6\u89e3Java\u4e4bSpring MVC\u7bc7\u4e8c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/49627.html","og_locale":"zh_CN","og_type":"article","og_title":"\u8be6\u89e3Java\u4e4bSpring MVC\u7bc7\u4e8c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb2.9w\u6b21\uff0c\u70b9\u8d5e198\u6b21\uff0c\u6536\u85cf136\u6b21\u3002\u8be6\u89e3\u83b7\u53d6Cookie\u548cSession\uff0c\u83b7\u53d6Header\u3002_springmvc \u4f1a\u8bdd\u7ba1\u7406","og_url":"https:\/\/www.wsisp.com\/helps\/49627.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-07-30T10:47:40+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730104735-6889f84754080.gif"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"2 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/49627.html","url":"https:\/\/www.wsisp.com\/helps\/49627.html","name":"\u8be6\u89e3Java\u4e4bSpring MVC\u7bc7\u4e8c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-07-30T10:47:40+00:00","dateModified":"2025-07-30T10:47:40+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/49627.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/49627.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/49627.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u8be6\u89e3Java\u4e4bSpring MVC\u7bc7\u4e8c"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/49627","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=49627"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/49627\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/49612"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=49627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=49627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=49627"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=49627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}