{"id":97112,"date":"2024-10-20T15:06:22","date_gmt":"2024-10-20T07:06:22","guid":{"rendered":"https:\/\/server.hk\/cnblog\/97112\/"},"modified":"2024-10-20T15:06:23","modified_gmt":"2024-10-20T07:06:23","slug":"p%e5%ae%8c%e6%88%90%e5%9f%ba%e6%96%bc%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e7%99%bb%e9%8c%84%e7%b3%bb%e7%b5%b1-jsp%e7%99%bb%e9%8c%84-%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/97112\/","title":{"rendered":"P\u5b8c\u6210\u57fa\u65bc\u6578\u64da\u5eab\u7684\u767b\u9304\u7cfb\u7d71 (jsp\u767b\u9304 \u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"%e5%ae%8c%e6%88%90%e5%9f%ba%e6%96%bc%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e7%99%bb%e9%8c%84%e7%b3%bb%e7%b5%b1-jsp-%e7%99%bb%e9%8c%84-%e6%95%b8%e6%93%9a%e5%ba%ab-lAZzYfapMv\">\u5b8c\u6210\u57fa\u65bc\u6578\u64da\u5eab\u7684\u767b\u9304\u7cfb\u7d71 (JSP \u767b\u9304 \u6578\u64da\u5eab)<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0c\u767b\u9304\u7cfb\u7d71\u662f\u5e7e\u4e4e\u6240\u6709\u7db2\u7ad9\u548c\u61c9\u7528\u7a0b\u5e8f\u7684\u6838\u5fc3\u7d44\u4ef6\u4e4b\u4e00\u3002\u7121\u8ad6\u662f\u793e\u4ea4\u5a92\u9ad4\u5e73\u53f0\u3001\u96fb\u5b50\u5546\u52d9\u7db2\u7ad9\u9084\u662f\u4f01\u696d\u5167\u90e8\u7cfb\u7d71\uff0c\u5b89\u5168\u7684\u7528\u6236\u8eab\u4efd\u9a57\u8b49\u90fd\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528 JavaServer Pages (JSP) \u548c\u6578\u64da\u5eab\u4f86\u69cb\u5efa\u4e00\u500b\u57fa\u672c\u7684\u767b\u9304\u7cfb\u7d71\u3002<\/p>\n<h2 id=\"%e7%b3%bb%e7%b5%b1%e6%9e%b6%e6%a7%8b%e6%a6%82%e8%bf%b0-lAZzYfapMv\">\u7cfb\u7d71\u67b6\u69cb\u6982\u8ff0<\/h2>\n<p>\u4e00\u500b\u57fa\u65bc\u6578\u64da\u5eab\u7684\u767b\u9304\u7cfb\u7d71\u901a\u5e38\u5305\u62ec\u4ee5\u4e0b\u5e7e\u500b\u7d44\u4ef6\uff1a<\/p>\n<ul>\n<li>\u7528\u6236\u754c\u9762\uff1a\u7528\u65bc\u8f38\u5165\u7528\u6236\u540d\u548c\u5bc6\u78bc\u7684\u8868\u55ae\u3002<\/li>\n<li>\u5f8c\u7aef\u908f\u8f2f\uff1a\u8655\u7406\u7528\u6236\u7684\u767b\u9304\u8acb\u6c42\uff0c\u4e26\u8207\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\u3002<\/li>\n<li>\u6578\u64da\u5eab\uff1a\u5b58\u5132\u7528\u6236\u7684\u5e33\u6236\u4fe1\u606f\uff0c\u5305\u62ec\u7528\u6236\u540d\u548c\u5bc6\u78bc\u3002<\/li>\n<\/ul>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e8%a8%ad%e8%a8%88-lAZzYfapMv\">\u6578\u64da\u5eab\u8a2d\u8a08<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u8a2d\u8a08\u4e00\u500b\u6578\u64da\u5eab\u4f86\u5b58\u5132\u7528\u6236\u4fe1\u606f\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u7528\u6236\u8868\u7d50\u69cb\uff1a<\/p>\n<pre><code>\nCREATE TABLE users (\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    username VARCHAR(50) NOT NULL UNIQUE,\n    password VARCHAR(255) NOT NULL\n);\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u8868\u4e2d\uff0c\u6211\u5011\u6709\u4e09\u500b\u5b57\u6bb5\uff1a\u7528\u6236ID\u3001\u7528\u6236\u540d\u548c\u5bc6\u78bc\u3002\u5bc6\u78bc\u61c9\u8a72\u4f7f\u7528\u54c8\u5e0c\u7b97\u6cd5\u9032\u884c\u52a0\u5bc6\uff0c\u4ee5\u63d0\u9ad8\u5b89\u5168\u6027\u3002<\/p>\n<h2 id=\"jsp-%e7%99%bb%e9%8c%84%e8%a1%a8%e5%96%ae-lAZzYfapMv\">JSP \u767b\u9304\u8868\u55ae<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u5275\u5efa\u4e00\u500b JSP \u9801\u9762\uff0c\u8b93\u7528\u6236\u53ef\u4ee5\u8f38\u5165\u4ed6\u5011\u7684\u767b\u9304\u4fe1\u606f\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u767b\u9304\u8868\u55ae\u793a\u4f8b\uff1a<\/p>\n<pre><code>\n&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n    &lt;title&gt;\u767b\u9304\u9801\u9762&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;h2&gt;\u7528\u6236\u767b\u9304&lt;\/h2&gt;\n    &lt;form action=\"login.jsp\" method=\"post\"&gt;\n        &lt;label for=\"username\"&gt;\u7528\u6236\u540d:&lt;\/label&gt;\n        &lt;input type=\"text\" id=\"username\" name=\"username\" required&gt;&lt;br&gt;\n        &lt;label for=\"password\"&gt;\u5bc6\u78bc:&lt;\/label&gt;\n        &lt;input type=\"password\" id=\"password\" name=\"password\" required&gt;&lt;br&gt;\n        &lt;input type=\"submit\" value=\"\u767b\u9304\"&gt;\n    &lt;\/form&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n<h2 id=\"%e5%be%8c%e7%ab%af%e9%82%8f%e8%bc%af%e8%99%95%e7%90%86-lAZzYfapMv\">\u5f8c\u7aef\u908f\u8f2f\u8655\u7406<\/h2>\n<p>\u7576\u7528\u6236\u63d0\u4ea4\u767b\u9304\u8868\u55ae\u5f8c\uff0c\u5f8c\u7aef\u9700\u8981\u8655\u7406\u9019\u500b\u8acb\u6c42\u3002\u4ee5\u4e0b\u662f login.jsp \u7684\u793a\u4f8b\u4ee3\u78bc\uff1a<\/p>\n<pre><code>\n&lt;%@ page import=\"java.sql.*\" %&gt;\n&lt;% \n    String username = request.getParameter(\"username\");\n    String password = request.getParameter(\"password\");\n    \n    Connection conn = null;\n    PreparedStatement pstmt = null;\n    ResultSet rs = null;\n\n    try {\n        Class.forName(\"com.mysql.cj.jdbc.Driver\");\n        conn = DriverManager.getConnection(\"jdbc:mysql:\/\/localhost:3306\/your_database\", \"your_username\", \"your_password\");\n        \n        String sql = \"SELECT * FROM users WHERE username = ?\";\n        pstmt = conn.prepareStatement(sql);\n        pstmt.setString(1, username);\n        rs = pstmt.executeQuery();\n        \n        if (rs.next()) {\n            String storedPassword = rs.getString(\"password\");\n            if (BCrypt.checkpw(password, storedPassword)) {\n                out.println(\"\u767b\u9304\u6210\u529f\uff01\");\n            } else {\n                out.println(\"\u5bc6\u78bc\u932f\u8aa4\uff01\");\n            }\n        } else {\n            out.println(\"\u7528\u6236\u540d\u4e0d\u5b58\u5728\uff01\");\n        }\n    } catch (Exception e) {\n        e.printStackTrace();\n    } finally {\n        if (rs != null) try { rs.close(); } catch (SQLException e) {}\n        if (pstmt != null) try { pstmt.close(); } catch (SQLException e) {}\n        if (conn != null) try { conn.close(); } catch (SQLException e) {}\n    }\n%&gt;\n<\/code><\/pre>\n<h2 id=\"%e5%ae%89%e5%85%a8%e6%80%a7%e8%80%83%e9%87%8f-lAZzYfapMv\">\u5b89\u5168\u6027\u8003\u91cf<\/h2>\n<p>\u5728\u8a2d\u8a08\u767b\u9304\u7cfb\u7d71\u6642\uff0c\u5b89\u5168\u6027\u662f\u6700\u91cd\u8981\u7684\u8003\u91cf\u4e4b\u4e00\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5efa\u8b70\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528 HTTPS \u4f86\u52a0\u5bc6\u7528\u6236\u7684\u767b\u9304\u4fe1\u606f\u3002<\/li>\n<li>\u5c0d\u5bc6\u78bc\u9032\u884c\u54c8\u5e0c\u8655\u7406\uff0c\u4e26\u4f7f\u7528\u9e7d\u503c\u4f86\u589e\u52a0\u5b89\u5168\u6027\u3002<\/li>\n<li>\u5be6\u65bd\u767b\u9304\u5617\u8a66\u9650\u5236\uff0c\u4ee5\u9632\u6b62\u66b4\u529b\u7834\u89e3\u653b\u64ca\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-lAZzYfapMv\">\u7e3d\u7d50<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86\u5982\u4f55\u4f7f\u7528 JSP \u548c\u6578\u64da\u5eab\u4f86\u69cb\u5efa\u4e00\u500b\u57fa\u672c\u7684\u767b\u9304\u7cfb\u7d71\u3002\u901a\u904e\u8a2d\u8a08\u5408\u9069\u7684\u6578\u64da\u5eab\u7d50\u69cb\u3001\u5275\u5efa\u7528\u6236\u754c\u9762\u4ee5\u53ca\u5be6\u73fe\u5f8c\u7aef\u908f\u8f2f\uff0c\u6211\u5011\u53ef\u4ee5\u6709\u6548\u5730\u8655\u7406\u7528\u6236\u7684\u767b\u9304\u8acb\u6c42\u3002\u96a8\u8457\u7db2\u7d61\u5b89\u5168\u5a01\u8105\u7684\u589e\u52a0\uff0c\u78ba\u4fdd\u767b\u9304\u7cfb\u7d71\u7684\u5b89\u5168\u6027\u8b8a\u5f97\u5c24\u70ba\u91cd\u8981\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u80fd\u548c\u7a69\u5b9a\u6027\u7684\u61c9\u7528\uff0c\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u5c07\u662f\u660e\u667a\u4e4b\u8209\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5efa\u7acb\u4e00\u500b\u57fa\u65bc\u6578\u64da\u5eab\u7684JSP\u767b\u9304\u7cfb\u7d71\uff0c\u5be6\u73fe\u5b89\u5168\u3001\u9ad8\u6548\u7684\u7528\u6236\u8eab\u4efd\u9a57\u8b49\uff0c\u63d0\u5347\u7db2\u7ad9\u7684\u6578\u64da\u7ba1\u7406\u80fd\u529b\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-97112","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97112","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=97112"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97112\/revisions"}],"predecessor-version":[{"id":97113,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97112\/revisions\/97113"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=97112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=97112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=97112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}