JSP第四次(2.0)

index.jsp

复制代码
<%@ page import="java.util.Random" %>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <title>登录</title>
</head>
<body>
<form action="login.jsp" method="post">
    <p>账号:<label>
        <input type="text" name="username">
    </label></p>
    <p>密码:<label>
        <input type="password" name="password">
    </label></p>
    <p>验证码:<label>
        <input type="number" name="verify">
    </label>
    <label>
        <%
            Random rdm = new Random();
            int code1 = rdm.nextInt(100);
            int code2 = rdm.nextInt(100);
        %>
        <input type="number" name="code1" value="<%=code1%>" hidden>
        <input type="number" name="code2" value="<%=code2%>" hidden>
        <%=code1%>+<%=code2%>(输入该算式的值)
    </label></p>
    <input type="submit" value="登录">
</form>
</body>
</html>
复制代码

login.jsp

复制代码
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<html>
<head>
    <title>登录结果</title>
</head>

<body>
<%
    String loginInfo = "";
    String sign = "";
    try
    {
        String code1 = request.getParameter("code1");
        String code2 = request.getParameter("code2");
        String verify = request.getParameter("verify");
        int code1Int = Integer.parseInt(code1);
        int code2Int = Integer.parseInt(code2);
        int verifyInt = Integer.parseInt(verify);
        if (code1Int + code2Int != verifyInt)
            loginInfo = "登录失败,验证码不正确";
        else
        {
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            if (username.equals("zs") && password.equals("123"))
            {
                loginInfo = "登录成功";
                sign = "hidden";
            }
            else
                loginInfo = "登录失败,账号密码错误";
        }
    }
    catch (Exception e)
    {
        loginInfo = "登录失败,参数错误";
    }
%>
<p><%=loginInfo%></p>
<form action="index.jsp" method="get">
    <input type="submit" value="返回登录" <%=sign%>>
</form>
</body>
</html>
复制代码

 

 

 

密码错误时:

 

 验证码不正确时:

 

 

 

 

 

posted @ 2021-04-06 17:02  Study_xia  阅读(29)  评论(0编辑  收藏  举报