Pure JSP method to implement user login function

1. Pure JSP method to implement user login function

(1) Implementation ideas

  • In the login page login.jsp, after entering the user name and password, it jumps to the login processing page doLogin.jsp for business logic processing. If the login is successful, it jumps to the login success page success.jsp. Otherwise, it jumps to the login failure page failure.jsp.

(2) Implementation steps

1. Create a Web project
  • Start by creatingJava Enterpriseproject, addWeb ApplicationFunction
    Insert image description here

Insert image description here

  • Set the project name to LoginDemo and set the save location
    Insert image description here

  • Click the [Finish] button
    Insert image description here

  • Modify the Artifact name in the project structure window - LoginDemo01 and delete the suffix.
    Insert image description here
    Insert image description here

  • Edit the server configuration and redeploy the project
    Insert image description here

  • First, the [Server] tab
    Insert image description here

  • Then switch to the [Deployment] tab
    Insert image description here

2. Create a login page
  • Create login page -login.jsp
    Insert image description here
  • 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
    Insert image description here
  • 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 login success page
  • Login success page -success.jsp
    Insert image description here
  • 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
    Insert image description here
  • 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 project homepage
  • Project Home Page -index.jsp
    Insert image description here
  • 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
    Insert image description here
  • Click the [Jump to login page] hyperlink
    Insert image description here
  • Enter the correct username and password (Wuxinjian: 903213)
    Insert image description here
  • Click the [Login] button to jump to the login success page
    Insert image description here
  • Return to the login page and enter the wrong username or password
    Insert image description here
  • Screen recording operation
    Insert image description here

Guess you like

Origin blog.csdn.net/qq_41301333/article/details/131202057