Servlet校验密码之Mariadb篇
先放图~~
数据库:
效果图:
整体来说与上一篇差距不大,这次主要是采用数据库来进行校验,我使用的是Mariadb,安装与配置不用我说
主要有一点,导入连接器jar包时,不仅要在项目中导入,还要在Tomcat/lib中导入,不然会报ClassNotFoundException
1.第一步:简单的连接器实现
package pers.eddy.login;
import java.sql.*; public class MySqlOperator {
private static final String driver = "org.mariadb.jdbc.Driver";
private static final String url = "jdbc:mariadb://localhost:3306/hello";
private static final String user = "root";
private static final String pswd = "admin"; private static Connection conn; static {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} public static boolean check(String usr, String psd) {
try {
conn = DriverManager.getConnection(url, user, pswd);
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery("select * from users where usr=\""+usr+"\" and psd=\""+psd+"\";");
conn.close();
return rs.next() ? true:false;
} catch (Exception e) {
e.printStackTrace();
}
return false;
} }
2.修改doGet()
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String usr = req.getParameter("usr");
String psd = req.getParameter("psd");
if (MySqlOperator.check(usr, psd)) {
req.setAttribute("usr", usr);
req.setAttribute("psd", psd);
req.getRequestDispatcher("/welcome.jsp").forward(req, resp);
} else {
resp.sendRedirect("/web_war_exploded/");
}
}
源码链接:https://pan.baidu.com/s/1DXCUybmk3tg4i2tlYkNyVQ 提取码: uwdv