我在spring controller中有用户列表大小,如何在javascript中获取它并根据结果调用新页面

时间:2018-08-30 09:51:25

标签: javascript hibernate spring-mvc jsp

clientscript.js

function login_validation() {
var username = '';
var password = '';


username = Trim(document.getElementById("username").value);
password = Trim(document.getElementById("password").value);


if (username == "") {
    alert("Please enter username");
    document.getElementById("username").focus();
}
else if (password == "") {
    alert("Please enter password");
    document.getElementById("password").focus();              
}

else {
    checkLogin(username, password);
}

}


serverscipt.js


function checkLogin(username, password) {
var http = createHTTPObj();
http.onreadystatechange = function ()
{
    if (http.readyState == 4) {
         debugger;
        var response = JSON.parse(http.responseText);

        if(response.length>0)
        {
            window.open("https://www.w3schools.com")
        }
        else
        {
            window.open("VehicleManager/Web Pages/Navigate.jsp")
        }           
    }
    console.log(response);
};
http.open('POST', 'user/login.htm', true);
http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
http.send('username=' + encodeURIComponent(username) + '&password=' + encodeURIComponent(password));

}


Authenticateservlet.java


 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
        System.out.println("called ....");
        String action = request.getParameter("action");
        System.out.println("action : " + action);
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("username" + username);
        System.out.println("password" + password);
        boolean loginStatus = false;
        try {

            List<User> userlist = new ArrayList<User>();

            loginStatus = userService.login(username, password);

            request.setAttribute("userList", userlist);
            RequestDispatcher requestDispatcher;
            requestDispatcher = request.getRequestDispatcher("/navigate.jsp");
            requestDispatcher.forward(request, response);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        System.out.println("loginStatus: " + loginStatus);


    }finally {
        out.close();
    }

}

login.jsp


<body>
    <br>
    <form class="form-signin" id="form1" action="\VehicleManager\AuthenticationServlet?action=login" method="post">                         
        <c:if test="${empty errmsg}">
            <h4 class="error message" style="width: 900px" bgcolor="red">${errmsg}</h4>
        </c:if>
        <div class="container">
            <div class="row">
                <h1 style="color:#2494db ">Please Login</h1>                                 
                <label for="username" class="sr-only">Username</label>
                <input type="text" id="username" name="username"  value=""  class="form-control" maxlength="15" placeholder="Email Id"  autofocus /><br>                                       
                <label for="Password" class="sr-only">Password</label>
                <input type="password" name="password" id="password" value="" autocomplete="off"  class="form-control" maxlength="15" placeholder="Password"  /><br>                    
                    <input type="Submit" name="Login" value="Login" class="btn  btn-primary" onclick="login_validation()" >&nbsp;
                <input type="reset" name="reset" class="btn btn-primary" value="Reset" />
            </div>
        </div>
    </form>
</body>
<div id="searchDiv" class="container-fluid">&nbsp;</div>


我已经在login.jsp文件的开头部分提到了两个is文件。 如果有人可以通过告诉我整个过程来帮助我。

我已经获取了与字段用户名和密码都匹配的用户列表。 现在我想将该列表调用到javascript页面。并比较列表大小是否为1,然后打开下一页,否则再次重定向到该页面。

0 个答案:

没有答案