如何将MySql dataBase与servlet连接到登录页面? 现在在servlet中我有预定义用户名(gogikole)和密码(1234)的代码
String username = request.getParameter("username");
String password = request.getParameter("password");
if (username.equals("gogikole") && password.equals("1234")) {
response.sendRedirect("mainMenu.jsp");
return;
}
但是如何检查用户名和密码对于数据库中的其他一些用户是否正确? 例如用户名:konsta,密码:4321
我有这样的代码:
登录Servlet
package servlets;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.*;
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
if (username.equals("gogikole") && password.equals("1234")) {
response.sendRedirect("mainMenu.jsp");
return;
}
}
}
的login.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login page</title>
</head>
<body>
<form method="post" action="LoginServlet">
<table align="center">
<tr>
<td>User name</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Login"></td>
</tr>
</table>
</form>
</body>
</html>
此外,我已经在Eclipse中添加了mysqlconnector。
答案 0 :(得分:0)
首先,你应该使用MysqlConnection连接到mysql。
然后,创建一个使用sql的语句,如SELECT * FROM users WHERE username = :username AND password = :password
。然后执行它。
你会得到一个包含匹配用户结果的resultSet,当然,如果用户名或密码错误,则resultSet为空。
解决问题的关键是您应该查询username.equals("gogikole") && password.equals("1234")
示例:
final String DB_URL = "localhost:3306/db"; //Mysql Address
final String USER = "root"; //mysql username
final String PASS = "123456";//mysql password
final String SQL = "SELECT * FROM users WHERE username = ? AND password = ?"; //SQL
String username = request.getParameter("username");
String password = request.getParameter("password");
Connection conn = null; //mysql connection
Statement stmt = null; //mysql statement
Class.forName("com.mysql.jdbc.Driver"); //register driver
conn = DriverManager.getConnection(DB_URL,USER,PASS); //get connection
stmt = conn.prepareStatement(SQL); //create statement
//init param
stmt.setString(1,username);
stmt.setString(2,password);
ResultSet rs = stmt.executeQuery(); //execute query
if(rs.first()){ //finded
response.sendRedirect("mainMenu.jsp");
return;
}