67,513
社区成员
发帖
与我相关
我的任务
分享
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>登录页面</title>
</head>
<body>
<form action="LoginVerify" method="post">
<input type="text" id="user_name" name="user_name" />
<input type="password" id="user_pwd" name="user_pwd" />
<input type="submit" value="登录" />
</form>
<p id="warning" name="warning">servlet写入的内容请放在这里</p>
</body>
</html>
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 验证用户名和密码是否正确
String userNameString = request.getParameter("user_name");
String userPwdString = request.getParameter("user_pwd");
String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=Test";
String userName="sa";
String userPwd="GnZx@801";
Connection dbConnection;
Statement statement;
ResultSet resultSet;
String sqlString = "SELECT user_name, user_pwd FROM tb_user WHERE user_name='" + userNameString + "' AND user_pwd='" +
userPwdString + "'";
try {
Class.forName(driverName);
try {
dbConnection = DriverManager.getConnection(dbURL, userName, userPwd);
statement = dbConnection.createStatement();
resultSet = statement.executeQuery(sqlString);
// 如果在数据库中找不到指定的用户名和密码,说明用户登录输入的用户名或者密码错误
if (!resultSet.next()) {
// 不知道如何写
}
resultSet.close();
statement.close();
dbConnection.close();
// 设置响应内容类型
response.setContentType("text/html;charset=UTF-8");
// 要重定向的新位置
response.sendRedirect("content/index.html");
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
}