1. ユーザーログイン機能を実装するための純粋な JSP メソッド
(1) 実現アイデア
- ログインページlogin.jspでは、ユーザー名とパスワードを入力後、ビジネスロジック処理のためのログイン処理ページdoLogin.jspにジャンプし、ログインに成功した場合はログイン成功ページsuccess.jspにジャンプします。ログイン失敗ページfailure.jspにジャンプします。
(2) 導入手順
1. Webプロジェクトを作成する
- まずは作成から始めましょうJava エンタープライズプロジェクト、追加ウェブアプリケーション関数
-
プロジェクト名を「LoginDemo」に設定し、保存場所を設定します。
-
[完了]ボタンをクリックします
-
プロジェクト構造ウィンドウでアーティファクト名を変更します (LoginDemo01)。サフィックスを削除します。
-
サーバー構成を編集し、プロジェクトを再デプロイします
-
まずは「サーバー」タブ
-
次に、[デプロイメント]タブに切り替えます。
2. ログインページを作成する
- ログインページの作成 -ログイン.jsp
- コードは以下のように表示されます
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用户登录</title>
</head>
<body>
<h3 style="text-align: center">用户登录</h3>
<form action="doLogin.jsp" method="post">
<table border="1" cellpadding="10" style="margin: 0px auto">
<tr>
<td align="center">账号</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td align="center">密码</td>
<td><input type="password" name="password"/></td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" value="登录"/>
<input type="reset" value="重置"/>
</td>
</tr>
</table>
</form>
</body>
</html>
3. ログイン処理ページの作成
- ログイン処理ページの作成 -doLogin.jsp
- コードは以下のように表示されます
<%
// 获取登录表单数据
String username = request.getParameter("username");
String password = request.getParameter("password");
// 判断登录是否成功
if (username.equals("无心剑") && password.equals("903213")) {
// 跳转到登录成功页面,传递用户名
response.sendRedirect("success.jsp?username=" + username);
} else {
// 跳转到登录失败页面,传递用户名
response.sendRedirect("failure.jsp?username=" + username);
}
%>
4. ログイン成功ページを作成する
- ログイン成功ページ -成功.jsp
- コードは以下のように表示されます
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录成功</title>
</head>
<body>
<h3 style="text-align: center">恭喜,<%=request.getParameter("username")%>,登录成功!</h3>
</body>
</html>
5. ログイン失敗ページを作成する
- ログイン失敗ページの作成 -失敗.jsp
- コードは以下のように表示されます
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录成功</title>
</head>
<body>
<h3 style="text-align: center">恭喜,<%=request.getParameter("username")%>,登录成功!</h3>
</body>
</html>
6. プロジェクトのホームページを編集する
- プロジェクトのホームページ -インデックス.jsp
- コードは以下のように表示されます
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用户登录</title>
</head>
<body>
<h3 style="text-align: center">用户登录</h3>
<form action="doLogin.jsp" method="post">
<table border="1" cellpadding="10" style="margin: 0px auto">
<tr>
<td align="center">账号</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td align="center">密码</td>
<td><input type="password" name="password"/></td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" value="登录"/>
<input type="reset" value="重置"/>
</td>
</tr>
</table>
</form>
</body>
</html>
(3) 試験結果
- サーバーを起動してホームページを表示する
- [ログインページにジャンプ]ハイパーリンクをクリックします。
- 正しいユーザー名とパスワードを入力してください (Wuxinjian: 903213)
- [ログイン]ボタンをクリックするとログイン成功ページにジャンプします
- ログインページに戻り、間違ったユーザー名またはパスワードを入力してください
- 画面録画操作