[英]How to check username and password with database to display error message?
I created a login page(HTML) in dynamic project. 我在动态项目中创建了一个登录页面(HTML)。 I want to check username and password with mysql database and display error message on the same page if username or password are incorrect.
我想用mysql数据库检查用户名和密码,如果用户名或密码不正确,则在同一页面上显示错误消息。 I don't want to use servlet here to display the error page because I want to display error message instead of error page.
我不想在这里使用servlet来显示错误页面,因为我想显示错误消息而不是错误页面。 How can I achieve this in dynamic webproject.
如何在动态Web项目中实现这一目标。 Can I use javascript, jQuery, JSP, or any combination of these?
我可以使用javascript,jQuery,JSP或这些的任意组合吗?
I am not familiar with JSP but here is an overview of what you need: 我不熟悉JSP,但是这里是您需要的概述:
index.html 的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 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.