52,778
社区成员
发帖
与我相关
我的任务
分享
<form>
<!-- <form action="login.jsp" method="post" onsubmit="return validate_form(this)"> -->
<div id=div1>
<table id=login height="100%" cellspacing=0 cellpadding=0 width=800
align=center>
<tbody>
<tr id=main>
<td>
<table height="100%" cellspacing=0 cellpadding=0 width="100%">
<tbody>
<tr>
<td colspan=4> </td>
</tr>
<tr height=30>
<td width=380> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr height=40>
<td rowspan=4> </td>
<td>用户名:</td>
<td><input class=textbox id="txtusername" name="username"></td>
<td width=120 id="usernameTd"> </td>
</tr>
<tr height=40>
<td>密 码:</td>
<td><input class=textbox id="txtuserpassword" type=password name="userpsw"></td>
<td width=120 id="passwordTd"> </td>
</tr>
<tr height=40>
<td></td>
<td align=left>
<input id="btnlogin" type="button" value=" 登 录 ">
<input id="btnreset" type="reset" value=" 重 填 " name="btnreset">
</td>
<td id="result" width=120> </td>
</tr>
<tr height=110>
<td colspan=4> </td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr id=root height=104>
<td> </td>
</tr>
</tbody>
</table>
</div>
<div align="center"> </div>
<div id=div2 style="display: none"></div>
</form>
$(document).ready(function(){
// 检测用户是否输入用户名
$("#txtusername").blur(function() {
if ($("#txtusername").val() == "") {
$("#usernameTd").html("<span style=\"color:red;\">请输入用户名</span>");
}else{
$("#usernameTd").hide();
}
});
// 检测用户是否输入密码
$("#txtuserpassword").blur(function() {
if ($("#txtuserpassword").val() == "") {
$("#passwordTd").html("<span style=\"color:red;\">请输入密码</span>");
}else{
$("#passwordTd").hide();
}
});
// 提交验证
$("#btnlogin").click(function(){
var userName = $("#txtusername").val();
var userPassword = $("#txtuserpassword").val();
if(userName == ""){
alert("用户名不能为空");
}else if(userPassword == ""){
alert("密码不能为空");
}
else{
$.post("http://127.0.0.1:8080/QMSystem/MyServlet",
{
username:userName,
userpsw:userPassword
},
function(data,status){
$("#result").html("<span style=\"color:red;\">用户名或密码错误</span>");
alert("用户名不存在或密码错误" + data);
}
);
}
});
});
//2.操作数据库:导入数据库驱动
//注册数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//获取数据库连接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/QMSystem","root","root");
//拼写SQL语句
String sql ="select * from user";
//准备SQL语句
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
Boolean lable = false;
PrintWriter out = response.getWriter();
while(rs.next()){
this.log("sql is execute."+ rs.getString("name") + " " + rs.getString("password"));
if(userName.equals(rs.getString("name")) && userPassword.equals(rs.getString("password"))){
String userPower = rs.getString("power");
lable = true;
// session.setAttribute("name", userN ame);//保存当前登录的用户名
this.log("conn success is execute.");
// String result = stationService.queryStationByName(name);
response.sendRedirect("http://127.0.0.1:8080/QMSystem/mainIndex.html");
out.println("true");
out.flush();
out.close();
break;
}
}
if(lable == false){
this.log("conn false is execute.");
// response.setContentType("text/x-json;charset=UTF-8");
String result = "false";
out.println(result);
out.flush();
out.close();
}
//关闭数据库连接
conn.close();
function(data,status){
if(data == "false") // response响应我设置的text/string,为什么到这里匹配不上,进不去if
{
$("#result").html("<span style=\"color:red;\">用户名或密码错误</span>");
}else{
// 跳转页面
self.location='/QMSystem/mainIndex.html';
}
}
$.post("/QMSystem/MyServlet",
{
username:userName,
userpsw:userPassword
},
function(data,status){
if(data == "false") // response响应我设置的text/string,为什么到这里匹配不上,进不去if
{
$("#result").html("<span style=\"color:red;\">用户名或密码错误</span>");
}else{
// 这里跳转页面怎么写
}
}
);

