User.java
public class User { //用户姓名 private String UserName; //用户密码 private String Password; public String getUserName() { return UserName; } public void setUserName(String userName) { this.UserName = userName; } public String getPassword() { return Password; } public void setPassword(String password) { this.Password = password; } }UserDAO.java
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.sql.ResultSet; import java.util.logging.Level; import java.util.logging.Logger; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; /** * 数据库管理类,提供连接数据库和拆解链接功能 * * @author Implementist */ public class DBManager extends HttpServlet { ServletConfig config; //定义一个ServletConfig对象 private static String UserName; //定义的数据库用户名 private static String Password; //定义的数据库连接密码 private static String url="jdbc:mysql://localhost:3306/myfirstapp"; //定义数据库连接URL private static Connection connection; //定义连接 @Override public void init(ServletConfig config) throws ServletException { super.init(config); //继承父类的init()方法 this.config = config; //获取配置信息 UserName = config.getInitParameter("DBUsername"); //获取数据库用户名 Password = config.getInitParameter("DBPassword"); //获取数据库连接密码 url = config.getInitParameter("ConnectionURL"); //获取数据库连接URL } /** * 获得数据库连接对象 * * @return 数据库连接对象 */ public static Connection getConnection() { try { Class.forName("com.mysql.jdbc.Driver").newInstance(); connection = DriverManager.getConnection(url, UserName, Password); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException ex) { Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ex); } return connection; } /** * 关闭所有的数据库连接资源 * * @param connection Connection 链接 * @param statement Statement 资源 * @param resultSet ResultSet 结果集合 */ public static void closeAll(Connection connection, Statement statement, ResultSet resultSet) { try { if (resultSet != null) { resultSet.close(); } if (statement != null) { statement.close(); } if (connection != null) { connection.close(); } } catch (SQLException ex) { Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ex); } } }LoginServlet
import java.io.IOException; import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONObject; /** * 测试登录Servlet * * @author Implementist */ public class LoginServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置响应内容类型 response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); try (PrintWriter out = response.getWriter()) { //获得请求中传来的用户名和密码 String accountNumber = request.getParameter("username").trim(); String password = request.getParameter("password").trim(); //密码验证结果 Boolean verifyResult = verifyLogin(accountNumber, password); Map<String, String> params = new HashMap<>(); JSONObject jsonObject = new JSONObject(); if (verifyResult) { out.print(true); } else { out.print(false); } } } /** * 验证用户名密码是否正确 * * @param userName * @param password */ private Boolean verifyLogin(String userName, String password) { User user = UserDAO.queryUser(userName); //账户密码验证 return null != user && password.equals(user.getPassword()); } }