簡體   English   中英

如何使用數據庫檢查用戶名和密碼以顯示錯誤消息?

[英]How to check username and password with database to display error message?

我在動態項目中創建了一個登錄頁面(HTML)。 我想用mysql數據庫檢查用戶名和密碼,如果用戶名或密碼不正確,則在同一頁面上顯示錯誤消息。 我不想在這里使用servlet來顯示錯誤頁面,因為我想顯示錯誤消息而不是錯誤頁面。 如何在動態Web項目中實現這一目標。 我可以使用javascript,jQuery,JSP或這些的任意組合嗎?

我不熟悉JSP,但是這里是您需要的概述:

的index.html

<!DOCTYPE HTML>
<html>
    <head>
        <title>Web Form</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script>
            $(document).ready(function() {

                $('form').on('submit', function(e) {
                    // don't let the browser submit the form
                    e.preventDefault();

                    // send form data to JSP page
                    $.ajax({
                        url: 'process.jsp',
                        async: true,
                        cache: false,
                        type: 'POST',
                        data: $(this).serialize(),
                        dataType: 'html',
                        success: function(data) {
                            // place message in error box
                            $('#error_box').html(data);

                            // if "Success" then redirect if you would like
                            if(data === 'Success!'){
                                // window.location = 'some other page/website';
                            }
                        }
                    });
                });

            });
        </script>
    </head>

    <body>
        <form method="POST" action="process.jsp">
            <input type="text" name="username" />
            <input type="password" name="password" />
            <input type="submit" value="submit" />
        </form>
        <div id="error_box"></div>
    </body>
</html>

process.jsp

// I don't know JSP
// use the POST values and check your DB
// Upon success/failure just echo the message
// <%='Success!'%>
// <%='Failed!'%>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM