1. Realize user login function in pure JSP mode
(1) Implementation ideas
- The login page login.jsp, after entering the user name and password, jumps to the login processing page doLogin.jsp for business logic processing, if the login is successful, jumps to the login success page success.jsp, otherwise jumps to the login failure page failure.jsp.
(2) Implementation steps
1. Create a Web project
- start by creatingJava Enterpriseitem, addWeb ApplicationFunction
-
Set the project name to LoginDemo, and set the save location
-
Click the [Finish] button
-
In the project structure window, modify the Artifact name - LoginDemo01, and delete the suffix
-
Edit the server configuration, redeploy the project
-
First the [Server] tab
-
Then switch to the [Deployment] tab
2. Create a login page
- Create a login page −login.jsp
- code show as below
<%@ 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. Create a login processing page
- Create login processing page −doLogin.jsp
- code show as below
<%
// 获取登录表单数据
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. Create a successful login page
- Login success page -success.jsp
- code show as below
<%@ 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. Create a login failure page
- Create login failure page −failure.jsp
- code show as below
<%@ 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. Edit the project home page
- Project Home -index.jsp
- code show as below
<%@ 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) Test results
- Start the server and display the home page
- Click the [Jump to login page] hyperlink
- Enter the correct username and password (Wuxinjian: 903213)
- Click the [Login] button to jump to the login success page
- Go back to the login page, enter wrong username or password
- Recording operation