我是 AJAX 和 JSON 的新手,我正在寻找它的教程,但似乎那个部门对我来说太重要了。
我决定做一个简单的登录来看看 AJAX 和 JSON 是如何工作的,但不知道如何开始。有人说我需要library jars
它,有人说它只是一个javasrcipt
这是我的简单登录jsp页面
<body>
<form action="LoginController" method="post">
<!-- Login body -->
<table>
<tr>
<td><label for="userName" >Username:</label></td>
<td><input type="text" name="userName" /></td>
</tr>
<tr>
<td><label for="password" >Password:</label></td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td><input type="submit" /></td>
</tr>
</table>
</form>
</body>
如果用户没有输入正确的用户名和密码,我将如何应用JSON
+AJAX
以使其不会刷新
顺便说一下,这是我的控制器:
UserDAO userDAO = new UserDAO(); // instantiate DAO class to access dummy database
String userName = request.getParameter( "userName" ); // get userName String from the Login.jsp
String password = request.getParameter( "password" ); // get password String from the Login.jsp
if( userDAO.authenticate( userName, password ) ) // validate userName and password
{
UserModel userModel = userDAO.getUserDetails( userName ); // get userModel that correspond to userName parameter
request.getSession().setAttribute( "userName", userName ); // set SESSION REQUEST to be forward to MainPage.jsp
request.setAttribute( "userDetails", userModel ); // set REQUEST to be forward to MainPage.jsp
RequestDispatcher rd = request.getRequestDispatcher( "MainPage.jsp" );
rd.forward( request, response ); // forward request to MainPage.jsp
return;
}
else
{
response.sendRedirect( "Login.jsp" );
return;
}