jsp第四周作业

1、登录(login.jsp)

 
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>登录页面</title>
    <style type="text/css">
        body{margin: 0;}
        #login_div{height: 720px;background-color: darkslategray;padding-top: 30px;}
        form{text-align: center;float: right;margin-right: 30px;}
        input{ 280px;height: 30px;margin-top: 10px;}
        img{ 280px;height: 30px;margin-top: 10px;}
        .btn{background-color: #009F95;opacity: 0.7; 280px;}
    </style>
</head>
<body>
<div id="login_div">

    <form action="Verification.jsp" method="post" >
        <div style="font-size: 25px;color: #01AAED;">欢迎登录</div>
        <input type="text" name="username" placeholder="请输入账户"/><br>
        <input type="password" name="password" placeholder="请输入密码"><br>
        <img id="captcha" src="${pageContext.request.contextPath}/captcha" onclick="refresh()"/><br>
        <input type="text" name="captcha" placeholder="请输入4位验证码"/><br>
        <input id="login" type="submit" value="登录" class="btn"/><br>
        <input id="autoLogin" type="submit" value="已登录" class="btn"/><br>
    </form>

</div>
<script>
    function refresh() {
        document.getElementById("captcha").src="${pageContext.request.contextPath}/captcha?"+new Date().getTime();
    }
</script>
</body>
</html>

2、验证(Verication.jsp)

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    String captcha = request.getParameter("captcha");
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    String realCap = (String) session.getAttribute("captcha");

    if(session.getAttribute("username")!=null&&session.getAttribute("password")!=null){
        request.getRequestDispatcher("success.jsp").forward(request,response);
    }

//    判断是否为空
    if(username!=null&&password!=null&&captcha!=null){
        //判断账户、密码、验证码是否正确
        if(captcha.equalsIgnoreCase(realCap)&&username.equals("zs")&&password.equals("123")){
            session.setAttribute("username",username);
            session.setAttribute("password",password);
            request.getRequestDispatcher("success.jsp").forward(request,response);
        }else {
            response.sendRedirect("login.jsp");
        }

    }

%>
</body>
</html>


3、首页(success.jsp)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1 style="text-align: center">
    WELCOME:<%=session.getAttribute("username")%>Mr XXX
</h1>
</body>
</html>
原文地址:https://www.cnblogs.com/Hackman/p/14590094.html