16 April 2017

Check Register Validate database kiểm tra dữ liệu có tồn tại trong Database hay không Register Jsp & Servlet

AJAX SCRIPT VÀ JSP để kiểm tra tính khả dụng của người dùng
register.jsp
Html 2017
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
    <head>
        <script language="Javascript" type="text/javascript">
            function createRequestObject() {
                var tmpXmlHttpObject;

                if (window.XMLHttpRequest) {
                    tmpXmlHttpObject = new XMLHttpRequest();

                } else if (window.ActiveXObject) {
                    tmpXmlHttpObject = new ActiveXObject("Microsoft.XMLHTTP");
                }

                return tmpXmlHttpObject;
            }
            var http = createRequestObject();
            function makeGetRequest(wordId) {
                //var wordId = document.forms["abc"]["username"].value;
                http.open('get', 'exist.jsp?username=' + wordId);
                http.onreadystatechange = processResponse;
                http.send(null);
            }

            function processResponse() {
                if (http.readyState == 4 && http.status == 200) {
                    var response = http.responseText;
                    document.getElementById("description").style.color = "red";
                    document.getElementById('description').innerHTML = response;
                }

            }
        </script>
    </head>
    <body>

        <!-- <form name="abc" method=POST">
                Username:<input type="text" name="username" onblur="makeGetRequest()" />
        </form> -->

        <a href="javascript:makeGetRequest('antonioAAA')">AntonioAAA</a>
        <br>
        <a href="javascript:makeGetRequest('antonio')">Antonio</a>
        <br>

        <div id="description"></div>

    </body>
</html>
exist.jsp
Html 2017
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Test here</title>
    </head>
    <body>
        <%
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection con = (Connection) DriverManager.getConnection("jdbc:sqlserver://localhost;databaseName=MyDatabase", "sa", "12345678");
            PreparedStatement ps = con.prepareStatement("SELECT  * FROM users WHERE username = ?");
            ps.setString(1, request.getParameter("username"));
            ResultSet res = ps.executeQuery();
            if (res.next()) {
                out.print("User already exists");
            } else {
                out.print("User name is valid");
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        %>
    </body>
</html>

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang