---------------------login.html----------------------------
<script type="text/javascript">
$(function(){inEvent();});
function inEvent(){$("#regSubmit").click(formSubmit);
$("#divdialog").css("display","none");}
function formSubmit() {$.post("LoginServlet",$("#form2").serialize(),function (data) {if (data == 1) {
$("#divdialog").css("display", "");
$("#divdialog").html("登陆成功");
$("#divdialog").dialog({title:"登陆成功提示",modal:true,buttons:
{"确定并跳转":function(){$(this).dialog("close");
location.href="index.html";
},"关闭":
function(){$(this).dialog("close");}}});}else{alert("data值为0"); $("#divdialog").html("有错误发生,可能是用户名或密码错误!");$("#divdialog").dialog({title:"登陆失败提示",modal:true,buttons:{"关闭":function(){$(this).dialog("close");}}});}});}
</script>
</head>
<body>
<div class="main">
<div class="login">
<h1>学生管理系统</h1>
<div class="inset">
<form method="post" id="form2" name="form2">
<div>
<h2>登录</h2>
<span><label>用户名</label></span>
<span><input name="tbusername" type="text" id="tbusername" class="textbox" ></span>
</div>
<div>
<span><label>密码</label></span>
<span><input name="tbpassword" type="password" id="tbpassword" class="password"></span>
</div>
<div class="sign">
<input name="regSubmit" type="button" value="登录" id="regSubmit" class="submit" />
<a href='reg.html'><input type="button" value="注册" class="submit" /></a>
</div>
<div id="divdialog" title="我是JQuery,欢迎您的到来">
<p>JQuery模态对话框:确定删除用户信息?</p>
</div>
</form>
-----------------------servlet------------------------
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String tbusername= request.getParameter("tbusername");
String tbpassword= request.getParameter("tbpassword");
System.out.println(tbusername); //返回null
System.out.println(tbusername);//返回null
try {
Class.forName("com.mysql.jdbc.Driver");
Connection cn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/student","root","123456");
PreparedStatement ps=cn.prepareStatement("SELECT * FROM userreg WHERE username=? AND pwd=?");
ps.setString(1,tbusername);
ps.setString(2,tbpassword);
ResultSet rs=ps.executeQuery();
if(rs.next()){
// System.out.println("密码对");
response.getWriter().print("1");
}else{
// System.out.println("密码错");
response.getWriter().print("0");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}